| 1 | module gg |
| 2 | |
| 3 | import os |
| 4 | |
| 5 | enum ScreenshotOutput { |
| 6 | file |
| 7 | stdout |
| 8 | } |
| 9 | |
| 10 | const gg_record_stdout_prefix = '__V_GG_IMAGE__' |
| 11 | |
| 12 | @[heap] |
| 13 | pub struct SSRecorderSettings { |
| 14 | pub mut: |
| 15 | stop_at_frame i64 = -1 |
| 16 | screenshot_frames []u64 |
| 17 | screenshot_folder string |
| 18 | screenshot_prefix string |
| 19 | screenshot_output ScreenshotOutput = .file |
| 20 | } |
| 21 | |
| 22 | fn parse_screenshot_frames(value string) []u64 { |
| 23 | mut frames := []u64{} |
| 24 | for raw_frame in value.split(',') { |
| 25 | frame := raw_frame.trim_space() |
| 26 | if frame == '' { |
| 27 | continue |
| 28 | } |
| 29 | frames << frame.u64() |
| 30 | } |
| 31 | return frames |
| 32 | } |
| 33 | |
| 34 | fn parse_screenshot_output(value string) ScreenshotOutput { |
| 35 | return match value.trim_space().to_lower() { |
| 36 | 'stdout' { .stdout } |
| 37 | else { .file } |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | fn recorder_env_value(env map[string]string, key string, fallback string) string { |
| 42 | value := env[key] |
| 43 | if value == '' { |
| 44 | return fallback |
| 45 | } |
| 46 | return value |
| 47 | } |
| 48 | |
| 49 | fn new_gg_recorder_settings_from_env(env map[string]string, executable string) &SSRecorderSettings { |
| 50 | mut stop_frame := recorder_env_value(env, 'VGG_STOP_AT_FRAME', '-1').i64() |
| 51 | mut frames := parse_screenshot_frames(env['VGG_SCREENSHOT_FRAMES']) |
| 52 | folder := env['VGG_SCREENSHOT_FOLDER'] |
| 53 | output := parse_screenshot_output(env['VGG_SCREENSHOT_OUTPUT']) |
| 54 | if output == .stdout && frames.len == 0 { |
| 55 | frames << u64(1) |
| 56 | } |
| 57 | if output == .stdout && stop_frame < 0 && frames.len > 0 { |
| 58 | stop_frame = i64(frames[frames.len - 1]) |
| 59 | } |
| 60 | prefix := os.join_path_single(folder, os.file_name(executable).all_before('.') + '_') |
| 61 | return &SSRecorderSettings{ |
| 62 | stop_at_frame: stop_frame |
| 63 | screenshot_frames: frames |
| 64 | screenshot_folder: folder |
| 65 | screenshot_prefix: prefix |
| 66 | screenshot_output: output |
| 67 | } |
| 68 | } |
| 69 | |