terminal-control 0.3.0

Control and test terminal applications through stable visible state
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
use std::fs;
use std::io::{self, BufReader, Read, Write};
use std::path::{Path, PathBuf};
use std::time::Duration;

use anyhow::{Context, Result, bail};
use clap::{Args, Parser, Subcommand, ValueEnum};
use terminal_control::{driver, recording, render, session, shot as shot_engine};

const HELP: &str = "\
termctrl controls and captures terminal applications for agents and tests. Start a named live
application, read its visible screen with `show`, or retain selected artifacts with `save`.";

const ROOT_EXAMPLES: &str = "\
Examples:
  termctrl show -- my-terminal-app
  termctrl save --format png --out captures/app.png -- my-terminal-app
  termctrl start demo --host opentui -- opencode
  termctrl wait demo '/connect' && termctrl send demo text:/connect enter
  termctrl show demo
  termctrl save demo --format png --out captures/provider.png
  termctrl logs demo
  termctrl restart demo
  termctrl stop demo";

const SHOW_HELP: &str = "\
Show prints a settled visible terminal screen to standard output, as text by default.

Sources:
  termctrl show NAME                    Read a named live session.
  termctrl show -- COMMAND...           Run a disposable command in a PTY.
  termctrl show --pipe -- COMMAND...    Read piped stdout/stderr.
  termctrl show --input FILE            Read ANSI/VT bytes from FILE, or use - for stdin.
  termctrl show --recording FILE        Replay the final screen of a .termctrl recording.

Use --format json, --format ansi, or --format svg for another stdout-readable representation.
Use `--at-marker NAME` or `--at-ms MS` with --recording to inspect an exact moment. Use `save`
to write files.";

const SAVE_HELP: &str = "\
Save freezes a visible terminal screen and writes exactly the requested artifact formats.

Examples:
  termctrl save demo --format png --out captures/current.png
  termctrl save demo --format png --format txt --out captures/current
  termctrl save --input debug.ansi --format png --out captures/replay.png
  termctrl save --recording captures/demo.termctrl --at-marker done --format png --out captures/done.png
  termctrl save --format png --out captures/startup.png -- my-terminal-app";

const START_HELP: &str = "\
Start creates one background PTY session and returns once its local control socket is available.
The application stays alive until `termctrl stop NAME`, so later commands interact with the
same screen and application state. Persistent sessions currently require macOS or Linux. Session
sockets are local control endpoints protected for the current user; recordings contain terminal
output plus client and automatic host input, so treat them as sensitive artifacts.

Example:
  termctrl start demo --host opentui --cols 112 --rows 34 -- opencode
  termctrl status demo
  termctrl wait demo '/connect'
  termctrl send demo text:/connect enter
  termctrl resize demo --cols 132 --rows 38
  termctrl show demo
  termctrl save demo --format png --out captures/provider.png
  termctrl stop demo";

const SEND_HELP: &str = "\
Send ordered input to a live session. Text uses `text:<value>`; named keys include `enter`,
`escape`, arrows, `tab`, `shift-tab`, `backspace`, `delete`, `home`, `end`, `page-up`, and
`page-down`. Use `ctrl-a` through `ctrl-z` for control input such as `ctrl-c` cancellation.
Add `--pace-ms 35` when producing a human-readable recording so typed text appears character by
character in the terminal instead of as one immediate paste. Use `--stdin` to send exact bytes
from standard input as one burst.

Examples:
  termctrl send demo ctrl-p text:model enter
  termctrl send demo ctrl-c
  printf '%s' 'a multiline prompt' | termctrl send demo --stdin
  termctrl send demo --pace-ms 35 'text:Write a terminal haiku.' enter";

const VIDEO_HELP: &str = "\
Replay a recording produced by `termctrl start --record` into a video artifact. Without `--edit`,
the video preserves observed timing. For a concise annotated demo, add named moments while recording
with `termctrl mark`, then pass an edit-plan JSON file with `--edit`. Each clip selects a marker range
and may set `speed`, optional visible `caption`, or optional `hold_ms`. Omit `hold_ms` for no artificial
pause between clips. Use `--tail-ms 0` if the final frame should not be held after the last clip.

`--fps` controls the maximum sampled frame rate; identical rendered screens are rasterized once and
reused. Pass `--include-startup` to retain blank startup or capability negotiation frames. The source
`.termctrl` file always retains the original timing, terminal bytes, client input, automatic host
input, and markers until the session is closed. Video export requires `ffmpeg` to be installed.
Pass `--footer` to add a bottom row with the clip caption, elapsed timecode, and TERMINAL CONTROL
branding; without it, edit-plan captions render as inline annotation rows.

Example:
  termctrl start demo --record captures/demo.termctrl -- opencode
  termctrl mark demo before-connect
  termctrl send demo text:/connect enter
  termctrl mark demo after-connect
  termctrl stop demo
  termctrl markers captures/demo.termctrl
  termctrl video captures/demo.termctrl --edit captures/demo.json --tail-ms 0 --out captures/demo.mp4";

const MARK_HELP: &str = "\
Add a named marker to the active `.termctrl` recording at the current session time. Markers do not
change the raw recording; they give later `show --recording --at-marker` and `video --edit` commands
stable names for important moments.

Example:
  termctrl start demo --record captures/demo.termctrl -- opencode
  termctrl wait demo \"Ask anything\"
  termctrl mark demo ready
  termctrl send demo text:/connect enter
  termctrl mark demo after-connect";

const MARKERS_HELP: &str = "\
List named markers from a .termctrl recording. Use the timestamps to audit an edit plan, or inspect
screens with `termctrl show --recording FILE --at-marker NAME` before exporting a demo video.";

const DRIVER_HELP: &str = "\
Driver mode serves isolated embedded sessions as newline-delimited JSON over standard input and
standard output. It is used by the `@kitlangton/terminal-control` package; standard output
contains protocol messages only. Driver sessions support isolated child environments, stable
captures, SVG evidence, recordings, resizing, and explicit exit waiting.

Example:
  termctrl driver";

#[derive(Parser)]
#[command(
    name = "termctrl",
    version,
    about = "Control and capture terminal applications",
    long_about = HELP,
    after_help = ROOT_EXAMPLES
)]
struct Cli {
    #[command(subcommand)]
    command: Command,
}

#[derive(Subcommand)]
enum Command {
    /// Print the visible screen of a session, command, or terminal stream.
    #[command(after_help = SHOW_HELP)]
    Show(ShowArgs),
    /// Save selected artifact formats from a session, command, or terminal stream.
    #[command(after_help = SAVE_HELP)]
    Save(SaveArgs),
    /// Start a named persistent terminal application.
    #[command(after_help = START_HELP)]
    Start(StartArgs),
    /// Wait until a named session includes visible text.
    Wait(WaitArgs),
    /// Send ordered input to a named session.
    #[command(after_help = SEND_HELP)]
    Send(SendArgs),
    /// Inspect lifecycle state and launch settings of a named session.
    Status(StatusArgs),
    /// List named local sessions and their states.
    List(ListArgs),
    /// Resize a named live session.
    Resize(ResizeArgs),
    /// Add a named moment to an active recording for later editing.
    #[command(after_help = MARK_HELP)]
    Mark(MarkArgs),
    /// List named moments in a recording.
    #[command(after_help = MARKERS_HELP)]
    Markers(MarkersArgs),
    /// Print retained readable terminal output or exact ANSI/VT bytes.
    Logs(LogsArgs),
    /// Restart a named session, reusing launch settings by default.
    Restart(RestartArgs),
    /// Terminate a named session.
    Stop(SessionArgs),
    /// Export a video from a recorded persistent session.
    #[command(after_help = VIDEO_HELP)]
    Video(VideoArgs),
    /// Serve isolated sessions for external testing clients.
    #[command(after_help = DRIVER_HELP)]
    Driver,
    #[command(name = "__serve", hide = true)]
    Serve(ServeArgs),
}

#[derive(Args)]
struct RenderArgs {
    /// Cell width used for terminal geometry and rendering.
    #[arg(long, default_value_t = 9)]
    cell_width: u16,
    /// Cell height used for terminal geometry and rendering.
    #[arg(long, default_value_t = 18)]
    cell_height: u16,
    /// Outer padding around the rendered terminal in pixels.
    #[arg(long, default_value_t = 18.0)]
    padding: f32,
    /// Font family used in SVG/PNG output.
    #[arg(
        long,
        default_value = "JetBrains Mono, SFMono-Regular, Menlo, monospace"
    )]
    font_family: String,
    /// Scale PNG output for sharp HiDPI viewing; SVG output is unchanged.
    #[arg(long, default_value_t = 2.0)]
    pixel_ratio: f32,
    /// Hide the terminal cursor in rendered output.
    #[arg(long)]
    hide_cursor: bool,
}

#[derive(Args)]
struct SourceArgs {
    /// Existing named terminal session to read.
    #[arg(value_name = "NAME")]
    name: Option<String>,
    /// Terminal width in cells for command or ANSI input (default: 80).
    #[arg(long)]
    cols: Option<u16>,
    /// Terminal height in cells for command or ANSI input (default: 24).
    #[arg(long)]
    rows: Option<u16>,
    /// Observe command stdout/stderr as pipes instead of launching it in a PTY.
    #[arg(long)]
    pipe: bool,
    /// Render ANSI/VT bytes from this file; use `-` for stdin.
    #[arg(long, value_name = "FILE")]
    input: Option<PathBuf>,
    /// Replay a .termctrl recording instead of reading a live session or command.
    #[arg(long, value_name = "FILE")]
    recording: Option<PathBuf>,
    /// Replay a recording up to this named marker.
    #[arg(long, requires = "recording", conflicts_with = "at_ms")]
    at_marker: Option<String>,
    /// Replay a recording up to this timestamp in milliseconds.
    #[arg(long, requires = "recording")]
    at_ms: Option<u64>,
    /// Color environment policy for a command source (default: auto for PTY, always for pipe).
    #[arg(long, value_enum)]
    color: Option<ColorMode>,
    /// Capture after this many milliseconds without output (default: 250).
    #[arg(long)]
    settle_ms: Option<u64>,
    /// Capture or return after this deadline even if output continues (default: 5000).
    #[arg(long)]
    deadline_ms: Option<u64>,
    /// Wait this long before allowing the initial screen to settle.
    #[arg(long)]
    initial_delay_ms: Option<u64>,
    /// Wait until the visible terminal includes this text before interacting or capturing.
    #[arg(long)]
    wait_for: Option<String>,
    /// Fail if command or ANSI input exceeds this many terminal bytes (default: 16777216).
    #[arg(long)]
    max_bytes: Option<usize>,
    /// Working directory for the terminal command.
    #[arg(long)]
    cwd: Option<PathBuf>,
    /// Terminal-host compatibility response profile.
    #[arg(long, value_enum)]
    host: Option<HostProfile>,
    /// Ordered input after readiness: key name or `text:<value>` (repeatable/groupable).
    #[arg(short = 's', long, value_name = "INPUT", num_args = 1..)]
    send: Vec<String>,
    /// Command and arguments to launch, following `--`.
    #[arg(last = true, required = false, num_args = 1.., allow_hyphen_values = true)]
    command: Vec<String>,
}

#[derive(Args)]
struct ShowArgs {
    #[command(flatten)]
    render: RenderArgs,
    #[command(flatten)]
    source: SourceArgs,
    /// Standard-output representation of the visible screen.
    #[arg(long, value_enum, default_value = "txt")]
    format: ShotFormat,
}

#[derive(Args)]
struct SaveArgs {
    #[command(flatten)]
    render: RenderArgs,
    #[command(flatten)]
    source: SourceArgs,
    /// Output path for one format, or output stem for several formats.
    #[arg(short, long)]
    out: PathBuf,
    /// Artifact format to write; repeat to write several explicit formats.
    #[arg(long = "format", value_enum, required = true)]
    formats: Vec<ShotFormat>,
}

#[derive(Args)]
struct StartArgs {
    /// Stable local name used by later session commands.
    name: String,
    /// Terminal width in cells.
    #[arg(long, default_value_t = 80)]
    cols: u16,
    /// Terminal height in cells.
    #[arg(long, default_value_t = 24)]
    rows: u16,
    /// Terminal cell width in pixels.
    #[arg(long, default_value_t = 9)]
    cell_width: u16,
    /// Terminal cell height in pixels.
    #[arg(long, default_value_t = 18)]
    cell_height: u16,
    /// Maximum raw terminal bytes retained by the live session.
    #[arg(long, default_value_t = 16 * 1024 * 1024)]
    max_bytes: usize,
    /// Working directory for the terminal command.
    #[arg(long)]
    cwd: Option<PathBuf>,
    /// Write timestamped terminal output and client/host input to this private recording file.
    #[arg(long)]
    record: Option<PathBuf>,
    /// Color environment policy for the terminal command.
    #[arg(long, value_enum, default_value = "auto")]
    color: ColorMode,
    /// Terminal-host compatibility response profile.
    #[arg(long, value_enum)]
    host: Option<HostProfile>,
    /// Command and arguments to launch, following `--`.
    #[arg(required = true, trailing_var_arg = true, allow_hyphen_values = true)]
    command: Vec<String>,
}

#[derive(Args)]
struct WaitArgs {
    /// Name of a running session.
    name: String,
    /// Visible text that must appear in the session screen.
    text: String,
    /// Maximum time to wait before returning an error.
    #[arg(long, default_value_t = 5000, value_name = "MS")]
    timeout: u64,
}

#[derive(Args)]
struct SendArgs {
    /// Name of a running session.
    name: String,
    /// Delay between input atoms; text is split into characters when set.
    #[arg(long, default_value_t = 0)]
    pace_ms: u64,
    /// Send bytes read from stdin as one burst; cannot be paced or combined with INPUT.
    #[arg(long, conflicts_with = "input")]
    stdin: bool,
    /// Ordered input: key name or `text:<value>`.
    #[arg(value_name = "INPUT")]
    input: Vec<String>,
}

#[derive(Args)]
struct StatusArgs {
    /// Name of a running or inspectable exited session.
    name: String,
    /// Write structured JSON status.
    #[arg(long)]
    json: bool,
}

#[derive(Args)]
struct ListArgs {
    /// Write structured JSON entries, including stale sockets.
    #[arg(long)]
    json: bool,
}

#[derive(Args)]
struct ResizeArgs {
    /// Name of a running session.
    name: String,
    /// New terminal width in cells.
    #[arg(long)]
    cols: u16,
    /// New terminal height in cells.
    #[arg(long)]
    rows: u16,
    /// New terminal cell width in pixels; defaults to current geometry.
    #[arg(long)]
    cell_width: Option<u16>,
    /// New terminal cell height in pixels; defaults to current geometry.
    #[arg(long)]
    cell_height: Option<u16>,
}

#[derive(Args)]
struct MarkArgs {
    /// Name of a running session started with --record.
    name: String,
    /// Unique marker name referenced by video edit plans.
    marker: String,
}

#[derive(Args)]
struct MarkersArgs {
    /// Recording created by `termctrl start --record`.
    input: PathBuf,
    /// Write structured JSON marker entries.
    #[arg(long)]
    json: bool,
}

#[derive(Args)]
struct LogsArgs {
    /// Name of a running or inspectable exited session.
    name: String,
    /// Write exact retained ANSI/VT stream bytes instead of readable retained output.
    #[arg(long)]
    ansi: bool,
}

#[derive(Args)]
struct RestartArgs {
    /// Name of a session to restart using its retained launch settings.
    name: String,
    #[arg(long)]
    cols: Option<u16>,
    #[arg(long)]
    rows: Option<u16>,
    #[arg(long)]
    cell_width: Option<u16>,
    #[arg(long)]
    cell_height: Option<u16>,
    #[arg(long)]
    max_bytes: Option<usize>,
    #[arg(long)]
    cwd: Option<PathBuf>,
    #[arg(long)]
    record: Option<PathBuf>,
    #[arg(long, value_enum)]
    color: Option<ColorMode>,
    #[arg(long, value_enum)]
    host: Option<HostProfile>,
    /// Replacement command; when omitted the prior command is reused.
    #[arg(trailing_var_arg = true, allow_hyphen_values = true)]
    command: Vec<String>,
}

#[derive(Args)]
struct SessionArgs {
    /// Name of a running session.
    name: String,
}

#[derive(Args)]
struct ServeArgs {
    #[arg(long)]
    socket: PathBuf,
    #[arg(long)]
    cwd: Option<PathBuf>,
    #[arg(long)]
    record: Option<PathBuf>,
    #[arg(long)]
    opentui_host: bool,
    #[arg(long, value_enum, default_value = "auto")]
    color: ColorMode,
    #[arg(long)]
    cols: u16,
    #[arg(long)]
    rows: u16,
    #[arg(long)]
    cell_width: u16,
    #[arg(long)]
    cell_height: u16,
    #[arg(long)]
    max_bytes: usize,
    #[arg(required = true, trailing_var_arg = true, allow_hyphen_values = true)]
    command: Vec<String>,
}

#[derive(Args)]
struct VideoArgs {
    /// Recording created by `termctrl start --record`.
    input: PathBuf,
    /// Override the recorded terminal cell width in rendered pixels.
    #[arg(long)]
    cell_width: Option<u16>,
    /// Override the recorded terminal cell height in rendered pixels.
    #[arg(long)]
    cell_height: Option<u16>,
    /// Outer padding around the rendered terminal in pixels.
    #[arg(long, default_value_t = 18.0)]
    padding: f32,
    /// Font family used in video output.
    #[arg(
        long,
        default_value = "JetBrains Mono, SFMono-Regular, Menlo, monospace"
    )]
    font_family: String,
    /// Scale video frames for sharp HiDPI viewing.
    #[arg(long, default_value_t = 2.0)]
    pixel_ratio: f32,
    /// Output video file path.
    #[arg(short, long, default_value = "video.mp4")]
    out: PathBuf,
    /// Hide the terminal cursor in rendered output.
    #[arg(long)]
    hide_cursor: bool,
    /// Add a bottom footer with clip caption, elapsed timecode, and TERMINAL CONTROL branding.
    #[arg(long)]
    footer: bool,
    /// Maximum sampled frames per second (1 to 1000).
    #[arg(long, default_value_t = 20)]
    fps: u32,
    /// Marker-based JSON edit plan with clips, captions, speeds, and holds.
    #[arg(long)]
    edit: Option<PathBuf>,
    /// Hold the final frame for this duration; use 0 for no artificial final pause.
    #[arg(long, default_value_t = 1000)]
    tail_ms: u64,
    /// Include leading contentless startup/terminal negotiation frames.
    #[arg(long)]
    include_startup: bool,
}

#[derive(Clone, Copy, ValueEnum)]
enum HostProfile {
    /// Respond to OpenTUI startup terminal capability queries.
    Opentui,
}

#[derive(Clone, Copy, ValueEnum)]
enum ColorMode {
    /// Preserve the current process color environment.
    Auto,
    /// Remove NO_COLOR and set common force-color environment variables.
    Always,
    /// Set common no-color environment variables.
    Never,
}

#[derive(Clone, Copy, Debug, PartialEq, Eq, ValueEnum)]
enum ShotFormat {
    /// PNG image.
    Png,
    /// SVG image.
    Svg,
    /// Visible plain text.
    Txt,
    /// Structured terminal cells.
    Json,
    /// Original ANSI/VT terminal stream.
    Ansi,
}

impl From<ColorMode> for shot_engine::ColorMode {
    fn from(value: ColorMode) -> Self {
        match value {
            ColorMode::Auto => shot_engine::ColorMode::Auto,
            ColorMode::Always => shot_engine::ColorMode::Always,
            ColorMode::Never => shot_engine::ColorMode::Never,
        }
    }
}

fn main() -> Result<()> {
    match Cli::parse().command {
        Command::Show(args) => show(args)?,
        Command::Save(args) => save(args)?,
        Command::Start(args) => {
            start_session(&args)?;
            println!("{}", args.name);
        }
        Command::Wait(args) => {
            session::wait(&args.name, args.text, Duration::from_millis(args.timeout))?;
        }
        Command::Send(args) => send(args)?,
        Command::Status(args) => status(args)?,
        Command::List(args) => list(args)?,
        Command::Resize(args) => {
            validate_terminal_size(args.cols, args.rows)?;
            session::resize(
                &args.name,
                args.cols,
                args.rows,
                args.cell_width,
                args.cell_height,
            )?;
        }
        Command::Mark(args) => session::mark(&args.name, args.marker)?,
        Command::Markers(args) => markers(args)?,
        Command::Logs(args) => logs(args)?,
        Command::Restart(args) => {
            restart_session(&args)?;
            println!("{}", args.name);
        }
        Command::Stop(args) => session::stop(&args.name)?,
        Command::Video(args) => {
            let out = args.out.clone();
            recording::video(
                &args.input,
                &recording::VideoOptions {
                    out: args.out,
                    cell_width: args.cell_width,
                    cell_height: args.cell_height,
                    padding: args.padding,
                    font_family: args.font_family,
                    pixel_ratio: args.pixel_ratio,
                    hide_cursor: args.hide_cursor,
                    footer: args.footer,
                    fps: args.fps,
                    tail: Duration::from_millis(args.tail_ms),
                    include_startup: args.include_startup,
                    edit: args.edit,
                },
            )?;
            println!("{}", out.display());
        }
        Command::Driver => {
            driver::serve(BufReader::new(io::stdin().lock()), io::stdout().lock())?;
        }
        Command::Serve(args) => {
            session::serve(
                args.socket,
                args.command,
                args.cwd,
                args.record,
                shot_engine::Options {
                    cols: args.cols,
                    rows: args.rows,
                    cell_width: args.cell_width,
                    cell_height: args.cell_height,
                    settle: Duration::ZERO,
                    deadline: Duration::ZERO,
                    input: Vec::new(),
                    initial_delay: Duration::ZERO,
                    wait_for: None,
                    max_bytes: args.max_bytes,
                    opentui_host: args.opentui_host,
                    color: args.color.into(),
                    env: Default::default(),
                    inherit_env: true,
                },
            )?;
        }
    }
    Ok(())
}

fn show(args: ShowArgs) -> Result<()> {
    if args.format == ShotFormat::Png {
        bail!("show does not support PNG output; use save --format png --out PATH");
    }
    let captured = read_source(&args.source, &args.render)?;
    write_stdout(&captured, &args.render, args.format)
}

fn save(args: SaveArgs) -> Result<()> {
    let captured = read_source(&args.source, &args.render)?;
    write_outputs(&captured, &args.render, &args.out, &args.formats)
}

fn read_source(args: &SourceArgs, render: &RenderArgs) -> Result<shot_engine::Shot> {
    let defaults = shot_engine::Options::default();
    let settle =
        Duration::from_millis(args.settle_ms.unwrap_or(defaults.settle.as_millis() as u64));
    let deadline = Duration::from_millis(
        args.deadline_ms
            .unwrap_or(defaults.deadline.as_millis() as u64),
    );
    if let Some(path) = args.recording.as_ref() {
        if args.name.is_some()
            || args.pipe
            || args.input.is_some()
            || !args.command.is_empty()
            || args.cols.is_some()
            || args.rows.is_some()
            || args.color.is_some()
            || args.settle_ms.is_some()
            || args.deadline_ms.is_some()
            || args.initial_delay_ms.is_some()
            || args.wait_for.is_some()
            || args.max_bytes.is_some()
            || args.cwd.is_some()
            || args.host.is_some()
            || !args.send.is_empty()
        {
            bail!(
                "--recording can only be combined with rendering options, --at-marker, or --at-ms"
            );
        }
        return recording::shot_at(path, args.at_ms, args.at_marker.as_deref());
    }
    if args.at_marker.is_some() || args.at_ms.is_some() {
        bail!("--at-marker and --at-ms require --recording");
    }
    if args.input.is_some() && (args.pipe || args.name.is_some() || !args.command.is_empty()) {
        bail!("--input cannot be combined with --pipe, NAME, or a command");
    }
    if args.name.is_some() && (args.pipe || !args.command.is_empty()) {
        bail!("NAME cannot be combined with --pipe or a command");
    }
    if let Some(name) = args.name.as_deref() {
        if args.cols.is_some()
            || args.rows.is_some()
            || args.color.is_some()
            || args.initial_delay_ms.is_some()
            || args.wait_for.is_some()
            || args.max_bytes.is_some()
            || args.cwd.is_some()
            || args.host.is_some()
            || !args.send.is_empty()
        {
            bail!("named-session reads support rendering, --settle-ms, and --deadline-ms only");
        }
        return session::show(name, settle, deadline);
    }
    let cols = args.cols.unwrap_or(defaults.cols);
    let rows = args.rows.unwrap_or(defaults.rows);
    validate_terminal_size(cols, rows)?;
    let max_bytes = args.max_bytes.unwrap_or(defaults.max_bytes);
    if let Some(path) = args.input.as_ref() {
        if args.color.is_some()
            || args.settle_ms.is_some()
            || args.deadline_ms.is_some()
            || args.initial_delay_ms.is_some()
            || args.wait_for.is_some()
            || args.cwd.is_some()
            || args.host.is_some()
            || !args.send.is_empty()
        {
            bail!("--input reads support dimensions, rendering, and --max-bytes only");
        }
        let mut input = Vec::new();
        let limit = max_bytes.saturating_add(1) as u64;
        if path.as_os_str() == "-" {
            io::stdin()
                .take(limit)
                .read_to_end(&mut input)
                .context("read ANSI input")?;
        } else {
            fs::File::open(path)
                .with_context(|| format!("open {}", path.display()))?
                .take(limit)
                .read_to_end(&mut input)
                .with_context(|| format!("read {}", path.display()))?;
        }
        return shot_engine::from_ansi(input, rows, cols, max_bytes);
    }
    if args.command.is_empty() {
        bail!("provide NAME, a command after --, or --input FILE");
    }
    if args.pipe
        && (!args.send.is_empty()
            || args.host.is_some()
            || args.initial_delay_ms.is_some()
            || args.settle_ms.is_some())
    {
        bail!("--pipe reads do not support --send, --host, --initial-delay-ms, or --settle-ms");
    }
    let color = args.color.unwrap_or(if args.pipe {
        ColorMode::Always
    } else {
        ColorMode::Auto
    });
    let options = shot_engine::Options {
        cols,
        rows,
        cell_width: render.cell_width,
        cell_height: render.cell_height,
        settle,
        deadline,
        input: input_bytes(&args.send)?,
        initial_delay: Duration::from_millis(args.initial_delay_ms.unwrap_or(0)),
        wait_for: args.wait_for.clone(),
        max_bytes,
        opentui_host: matches!(args.host, Some(HostProfile::Opentui)),
        color: color.into(),
        env: Default::default(),
        inherit_env: true,
    };
    if args.pipe {
        shot_engine::from_pipe_command(&args.command, args.cwd.as_deref(), &options)
    } else {
        shot_engine::from_command(&args.command, args.cwd.as_deref(), &options)
    }
}

fn send(args: SendArgs) -> Result<()> {
    if args.stdin && args.pace_ms > 0 {
        bail!("--stdin cannot be combined with --pace-ms");
    }
    let input = if args.stdin {
        let mut bytes = Vec::new();
        io::stdin()
            .take(1024 * 1024 + 1)
            .read_to_end(&mut bytes)
            .context("read session input")?;
        if bytes.len() > 1024 * 1024 {
            bail!("session input exceeds 1 MiB");
        }
        vec![bytes]
    } else {
        if args.input.is_empty() {
            bail!("provide INPUT events or --stdin");
        }
        session_input(&args.input, args.pace_ms > 0)?
    };
    session::send(&args.name, input, Duration::from_millis(args.pace_ms))?;
    Ok(())
}

fn status(args: StatusArgs) -> Result<()> {
    let status = session::status(&args.name)?;
    if args.json {
        println!("{}", serde_json::to_string_pretty(&status)?);
    } else {
        println!("{} {}", args.name, session_state(status.state));
        println!("cwd: {}", status.launch.cwd.display());
        println!("command: {}", status.launch.command.join(" "));
        println!("viewport: {}x{}", status.cols, status.rows);
        println!(
            "recording: {}",
            status
                .launch
                .record
                .as_ref()
                .map_or_else(|| "none".to_owned(), |path| path.display().to_string())
        );
    }
    Ok(())
}

fn list(args: ListArgs) -> Result<()> {
    let sessions = session::list()?;
    if args.json {
        println!("{}", serde_json::to_string_pretty(&sessions)?);
    } else {
        for entry in sessions {
            if let Some(status) = entry.status {
                println!(
                    "{}\t{}\t{}x{}\t{}",
                    entry.name,
                    session_state(status.state),
                    status.cols,
                    status.rows,
                    if status.recording { "recording" } else { "-" }
                );
            } else {
                let reason = match entry.unavailable {
                    Some(session::UnavailableReason::IncompatibleProtocol) => "incompatible",
                    _ => "stale",
                };
                println!("{}\t{}\t-\t-", entry.name, reason);
            }
        }
    }
    Ok(())
}

fn logs(args: LogsArgs) -> Result<()> {
    let bytes = session::logs(&args.name, args.ansi)?;
    io::stdout()
        .write_all(&bytes)
        .context("write session logs")?;
    if !args.ansi && !bytes.ends_with(b"\n") {
        io::stdout()
            .write_all(b"\n")
            .context("write session logs newline")?;
    }
    Ok(())
}

fn markers(args: MarkersArgs) -> Result<()> {
    let markers = recording::markers(&recording::read(&args.input)?);
    if args.json {
        println!("{}", serde_json::to_string_pretty(&markers)?);
        return Ok(());
    }
    for marker in markers {
        println!("{}\t{}", marker.at_ms, marker.name);
    }
    Ok(())
}

fn start_session(args: &StartArgs) -> Result<()> {
    validate_terminal_size(args.cols, args.rows)?;
    let options = shot_engine::Options {
        cols: args.cols,
        rows: args.rows,
        cell_width: args.cell_width,
        cell_height: args.cell_height,
        settle: Duration::ZERO,
        deadline: Duration::ZERO,
        input: Vec::new(),
        initial_delay: Duration::ZERO,
        wait_for: None,
        max_bytes: args.max_bytes,
        opentui_host: matches!(args.host, Some(HostProfile::Opentui)),
        color: args.color.into(),
        env: Default::default(),
        inherit_env: true,
    };
    session::start(
        &args.name,
        &args.command,
        args.cwd.as_deref(),
        args.record.as_deref(),
        &options,
    )
}

fn restart_session(args: &RestartArgs) -> Result<()> {
    let previous = session::status(&args.name)?.launch;
    let cols = args.cols.unwrap_or(previous.cols);
    let rows = args.rows.unwrap_or(previous.rows);
    validate_terminal_size(cols, rows)?;
    let command = if args.command.is_empty() {
        previous.command
    } else {
        args.command.clone()
    };
    let cwd = args.cwd.clone().unwrap_or(previous.cwd);
    let record = args.record.clone().or(previous.record);
    session::restart(
        &args.name,
        &command,
        Some(&cwd),
        record.as_deref(),
        &shot_engine::Options {
            cols,
            rows,
            cell_width: args.cell_width.unwrap_or(previous.cell_width),
            cell_height: args.cell_height.unwrap_or(previous.cell_height),
            max_bytes: args.max_bytes.unwrap_or(previous.max_bytes),
            opentui_host: args.host.map_or(previous.opentui_host, |host| {
                matches!(host, HostProfile::Opentui)
            }),
            color: args.color.map_or(previous.color, Into::into),
            ..shot_engine::Options::default()
        },
    )
}

fn session_state(state: session::SessionState) -> &'static str {
    match state {
        session::SessionState::Running => "running",
        session::SessionState::Exited => "exited",
    }
}

fn input_bytes(events: &[String]) -> Result<Vec<u8>> {
    let mut input = Vec::new();
    for event in events {
        input.extend(input_event(event)?);
    }
    Ok(input)
}

fn input_event(event: &str) -> Result<Vec<u8>> {
    if let Some(text) = event.strip_prefix("text:") {
        return Ok(text.as_bytes().to_vec());
    }
    if let Some(key) = event
        .strip_prefix("ctrl-")
        .or_else(|| event.strip_prefix("ctrl:"))
        && key.len() == 1
    {
        let key = key.as_bytes()[0].to_ascii_lowercase();
        if key.is_ascii_lowercase() {
            return Ok(vec![key - b'a' + 1]);
        }
    }
    Ok(match event {
        "enter" => b"\r".to_vec(),
        "escape" | "esc" => b"\x1b".to_vec(),
        "up" => b"\x1b[A".to_vec(),
        "down" => b"\x1b[B".to_vec(),
        "left" => b"\x1b[D".to_vec(),
        "right" => b"\x1b[C".to_vec(),
        "tab" => b"\t".to_vec(),
        "shift-tab" => b"\x1b[Z".to_vec(),
        "backspace" => b"\x7f".to_vec(),
        "delete" => b"\x1b[3~".to_vec(),
        "home" => b"\x1b[H".to_vec(),
        "end" => b"\x1b[F".to_vec(),
        "page-up" => b"\x1b[5~".to_vec(),
        "page-down" => b"\x1b[6~".to_vec(),
        _ => anyhow::bail!(
            "unsupported input event {event:?}; use text:<value>, ctrl-a through ctrl-z, enter, escape, arrows, tab, shift-tab, backspace, delete, home, end, page-up, or page-down"
        ),
    })
}

fn session_input(events: &[String], paced: bool) -> Result<Vec<Vec<u8>>> {
    if !paced {
        return Ok(vec![input_bytes(events)?]);
    }
    let mut input = Vec::new();
    for event in events {
        if let Some(text) = event.strip_prefix("text:") {
            input.extend(text.chars().map(|char| char.to_string().into_bytes()));
            continue;
        }
        input.push(input_event(event)?);
    }
    Ok(input)
}

fn validate_terminal_size(cols: u16, rows: u16) -> Result<()> {
    if cols == 0 || rows == 0 {
        bail!("terminal dimensions must be greater than zero");
    }
    Ok(())
}

fn write_outputs(
    captured: &shot_engine::Shot,
    args: &RenderArgs,
    out: &Path,
    formats: &[ShotFormat],
) -> Result<()> {
    if let Some(parent) = out.parent().filter(|parent| !parent.as_os_str().is_empty()) {
        fs::create_dir_all(parent).with_context(|| format!("create {}", parent.display()))?;
    }
    let enabled = |format| formats.contains(&format);
    let svg = (enabled(ShotFormat::Svg) || enabled(ShotFormat::Png))
        .then(|| rendered_svg(captured, args));
    if let Some(svg) = svg.as_ref().filter(|_| enabled(ShotFormat::Svg)) {
        let path = out.with_extension("svg");
        fs::write(&path, svg).with_context(|| format!("write {}", path.display()))?;
        println!("{}", path.display());
    }
    if let Some(svg) = svg.as_ref().filter(|_| enabled(ShotFormat::Png)) {
        let path = out.with_extension("png");
        render::png(svg, &path, args.pixel_ratio)?;
        println!("{}", path.display());
    }
    if enabled(ShotFormat::Json) {
        let path = out.with_extension("json");
        fs::write(&path, serde_json::to_vec_pretty(&captured.frame)?)
            .with_context(|| format!("write {}", path.display()))?;
        println!("{}", path.display());
    }
    if enabled(ShotFormat::Txt) {
        let path = out.with_extension("txt");
        fs::write(&path, captured.frame.text())
            .with_context(|| format!("write {}", path.display()))?;
        println!("{}", path.display());
    }
    if enabled(ShotFormat::Ansi) {
        let path = out.with_extension("ansi");
        fs::write(&path, &captured.ansi).with_context(|| format!("write {}", path.display()))?;
        println!("{}", path.display());
    }
    Ok(())
}

fn write_stdout(captured: &shot_engine::Shot, args: &RenderArgs, format: ShotFormat) -> Result<()> {
    let bytes = match format {
        ShotFormat::Txt => captured.frame.text().into_bytes(),
        ShotFormat::Json => serde_json::to_vec_pretty(&captured.frame)?,
        ShotFormat::Ansi => captured.ansi.clone(),
        ShotFormat::Svg => rendered_svg(captured, args).into_bytes(),
        ShotFormat::Png => unreachable!("show validates PNG before reading source"),
    };
    io::stdout()
        .write_all(&bytes)
        .context("write visible screen")?;
    if format != ShotFormat::Ansi && !bytes.ends_with(b"\n") {
        io::stdout()
            .write_all(b"\n")
            .context("write visible screen newline")?;
    }
    Ok(())
}

fn rendered_svg(captured: &shot_engine::Shot, args: &RenderArgs) -> String {
    render::svg(
        &captured.frame,
        &render::Options {
            cell_width: f32::from(args.cell_width),
            cell_height: f32::from(args.cell_height),
            font_size: f32::from(args.cell_height) * 0.78,
            padding: args.padding,
            font_family: args.font_family.clone(),
            show_cursor: !args.hide_cursor,
        },
    )
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn preserves_ordered_input_events() {
        assert_eq!(
            input_bytes(&[
                "ctrl-p".to_owned(),
                "text:model".to_owned(),
                "enter".to_owned()
            ])
            .unwrap(),
            b"\x10model\r"
        );
    }

    #[test]
    fn rejects_unsupported_input_events() {
        assert!(input_bytes(&["space".to_owned()]).is_err());
    }

    #[test]
    fn encodes_control_and_navigation_input_events() {
        assert_eq!(
            input_bytes(&[
                "ctrl-c".to_owned(),
                "shift-tab".to_owned(),
                "delete".to_owned()
            ])
            .unwrap(),
            b"\x03\x1b[Z\x1b[3~"
        );
    }

    #[test]
    fn parses_one_off_show_input_sequence() {
        let cli = Cli::try_parse_from([
            "termctrl",
            "show",
            "--wait-for",
            "ready",
            "-s",
            "ctrl-p",
            "text:model",
            "enter",
            "--",
            "app",
        ])
        .unwrap();
        let Command::Show(args) = cli.command else {
            panic!("expected show command");
        };
        assert!(args.source.name.is_none());
        assert_eq!(args.source.command, ["app"]);
        assert_eq!(args.source.send, ["ctrl-p", "text:model", "enter"]);
    }

    #[test]
    fn parses_explicit_saved_formats_and_named_source() {
        let cli = Cli::try_parse_from([
            "termctrl", "save", "demo", "--out", "capture", "--format", "png", "--format", "txt",
        ])
        .unwrap();
        let Command::Save(args) = cli.command else {
            panic!("expected save command");
        };
        assert_eq!(args.source.name.as_deref(), Some("demo"));
        assert_eq!(args.formats, [ShotFormat::Png, ShotFormat::Txt]);
    }

    #[test]
    fn parses_flat_session_control_commands() {
        assert!(Cli::try_parse_from(["termctrl", "status", "demo", "--json"]).is_ok());
        assert!(
            Cli::try_parse_from([
                "termctrl", "resize", "demo", "--cols", "120", "--rows", "40"
            ])
            .is_ok()
        );
        assert!(Cli::try_parse_from(["termctrl", "send", "demo", "--stdin"]).is_ok());
        assert!(Cli::try_parse_from(["termctrl", "mark", "demo", "before-send"]).is_ok());
        assert!(Cli::try_parse_from(["termctrl", "markers", "captures/demo.termctrl"]).is_ok());
        assert!(Cli::try_parse_from(["termctrl", "logs", "demo", "--ansi"]).is_ok());
        assert!(Cli::try_parse_from(["termctrl", "restart", "demo"]).is_ok());
        assert!(
            Cli::try_parse_from(["termctrl", "wait", "demo", "ready", "--timeout", "5"]).is_ok()
        );
    }

    #[test]
    fn show_rejects_png_before_starting_a_source() {
        let cli =
            Cli::try_parse_from(["termctrl", "show", "--format", "png", "--", "app"]).unwrap();
        let Command::Show(args) = cli.command else {
            panic!("expected show command");
        };

        assert_eq!(
            show(args).unwrap_err().to_string(),
            "show does not support PNG output; use save --format png --out PATH"
        );
    }

    #[test]
    fn parses_recording_source_seek_options() {
        let cli = Cli::try_parse_from([
            "termctrl",
            "show",
            "--recording",
            "captures/demo.termctrl",
            "--at-marker",
            "done",
        ])
        .unwrap();
        let Command::Show(args) = cli.command else {
            panic!("expected show command");
        };

        assert_eq!(
            args.source.recording.as_deref(),
            Some(Path::new("captures/demo.termctrl"))
        );
        assert_eq!(args.source.at_marker.as_deref(), Some("done"));
    }

    #[test]
    fn rejects_settling_options_for_pipe_reads() {
        let cli = Cli::try_parse_from([
            "termctrl",
            "show",
            "--pipe",
            "--settle-ms",
            "100",
            "--",
            "true",
        ])
        .unwrap();
        let Command::Show(args) = cli.command else {
            panic!("expected show command");
        };

        assert!(show(args).is_err());
    }

    #[test]
    fn rejects_zero_terminal_dimensions() {
        assert!(validate_terminal_size(0, 24).is_err());
        assert!(validate_terminal_size(80, 0).is_err());
    }

    #[test]
    fn paced_session_input_splits_text_without_splitting_keys() {
        assert_eq!(
            session_input(&["text:hi".to_owned(), "enter".to_owned()], true).unwrap(),
            vec![b"h".to_vec(), b"i".to_vec(), b"\r".to_vec()]
        );
    }
}