1use serde::{Deserialize, Serialize};
2use std::collections::HashMap;
3use std::fmt;
4use std::path::PathBuf;
5use std::time::Duration;
6
7#[derive(Debug, Clone, Copy, PartialEq, Eq)]
8pub struct TerminalResize {
9 pub cols: u16,
10 pub rows: u16,
11}
12
13#[derive(Debug, Clone, PartialEq, Eq)]
14pub enum TerminalKey {
15 Backspace,
16 CtrlC,
17 CtrlD,
18 Down,
19 Enter,
20 Escape,
21 Left,
22 Right,
23 Tab,
24 Up,
25 Raw(String),
26}
27
28impl TerminalKey {
29 pub(crate) fn sequence(&self) -> &str {
30 match self {
31 Self::Backspace => "\u{7f}",
32 Self::CtrlC => "\u{3}",
33 Self::CtrlD => "\u{4}",
34 Self::Down => "\u{1b}[B",
35 Self::Enter => "\r",
36 Self::Escape => "\u{1b}",
37 Self::Left => "\u{1b}[D",
38 Self::Right => "\u{1b}[C",
39 Self::Tab => "\t",
40 Self::Up => "\u{1b}[A",
41 Self::Raw(sequence) => sequence,
42 }
43 }
44}
45
46#[derive(Debug, Clone, Default)]
47pub struct TerminalInteraction {
48 pub after: Option<String>,
49 pub after_regex: Option<String>,
50 pub idle_duration: Duration,
51 pub text: Option<String>,
52 pub key: Option<TerminalKey>,
53 pub resize: Option<TerminalResize>,
54}
55
56#[derive(Debug, Clone)]
57pub struct TerminalCaptureOptions {
58 pub file: String,
59 pub args: Vec<String>,
60 pub cwd: Option<PathBuf>,
61 pub env: HashMap<String, String>,
62 pub cols: u16,
63 pub rows: u16,
64 pub settle_duration: Duration,
65 pub interactions: Vec<TerminalInteraction>,
66 pub stop_marker: Option<String>,
67 pub stop_marker_grace: Duration,
68 pub timeout: Option<Duration>,
71 pub artifact_directory: Option<PathBuf>,
72}
73
74impl Default for TerminalCaptureOptions {
75 fn default() -> Self {
76 Self {
77 file: String::new(),
78 args: Vec::new(),
79 cwd: None,
80 env: HashMap::new(),
81 cols: 80,
82 rows: 24,
83 settle_duration: Duration::from_millis(35),
84 interactions: Vec::new(),
85 stop_marker: None,
86 stop_marker_grace: Duration::from_millis(250),
87 timeout: Some(Duration::from_secs(30)),
88 artifact_directory: None,
89 }
90 }
91}
92
93#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
94pub struct TerminalCursor {
95 pub x: u16,
96 pub y: u16,
97}
98
99#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
100pub struct TerminalFrame {
101 pub time: f64,
102 pub cols: u16,
103 pub rows: u16,
104 pub cursor: TerminalCursor,
105 pub alternate: bool,
106 pub lines: Vec<String>,
107 pub screen: Vec<String>,
108}
109
110#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
111pub struct AsciicastHeader {
112 pub version: u8,
113 pub width: u16,
114 pub height: u16,
115 pub timestamp: i64,
116 pub env: HashMap<String, String>,
117}
118
119#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
120pub struct AsciicastEvent {
121 pub time: f64,
122 pub code: String,
123 pub data: String,
124}
125
126#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
127pub struct Asciicast {
128 pub header: AsciicastHeader,
129 pub events: Vec<AsciicastEvent>,
130}
131
132#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
133pub struct TerminalCapture {
134 pub exit_code: i32,
135 pub signal: Option<String>,
136 pub output: String,
137 pub transcript: String,
138 pub frames: Vec<TerminalFrame>,
139 pub interaction_count: usize,
140 pub asciicast: Asciicast,
141}
142
143#[derive(Debug)]
144pub struct TerminalCaptureError {
145 message: String,
146 partial: Option<Box<TerminalCapture>>,
147}
148
149impl TerminalCaptureError {
150 pub(crate) fn new(message: impl Into<String>, partial: Option<TerminalCapture>) -> Self {
151 Self {
152 message: message.into(),
153 partial: partial.map(Box::new),
154 }
155 }
156
157 pub fn partial_capture(&self) -> Option<&TerminalCapture> {
158 self.partial.as_deref()
159 }
160}
161
162impl fmt::Display for TerminalCaptureError {
163 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
164 formatter.write_str(&self.message)
165 }
166}
167
168impl std::error::Error for TerminalCaptureError {}