Skip to main content

command_stream/terminal/
types.rs

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: Duration,
69    pub artifact_directory: Option<PathBuf>,
70}
71
72impl Default for TerminalCaptureOptions {
73    fn default() -> Self {
74        Self {
75            file: String::new(),
76            args: Vec::new(),
77            cwd: None,
78            env: HashMap::new(),
79            cols: 80,
80            rows: 24,
81            settle_duration: Duration::from_millis(35),
82            interactions: Vec::new(),
83            stop_marker: None,
84            stop_marker_grace: Duration::from_millis(250),
85            timeout: Duration::from_secs(30),
86            artifact_directory: None,
87        }
88    }
89}
90
91#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
92pub struct TerminalCursor {
93    pub x: u16,
94    pub y: u16,
95}
96
97#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
98pub struct TerminalFrame {
99    pub time: f64,
100    pub cols: u16,
101    pub rows: u16,
102    pub cursor: TerminalCursor,
103    pub alternate: bool,
104    pub lines: Vec<String>,
105    pub screen: Vec<String>,
106}
107
108#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
109pub struct AsciicastHeader {
110    pub version: u8,
111    pub width: u16,
112    pub height: u16,
113    pub timestamp: i64,
114    pub env: HashMap<String, String>,
115}
116
117#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
118pub struct AsciicastEvent {
119    pub time: f64,
120    pub code: String,
121    pub data: String,
122}
123
124#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
125pub struct Asciicast {
126    pub header: AsciicastHeader,
127    pub events: Vec<AsciicastEvent>,
128}
129
130#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
131pub struct TerminalCapture {
132    pub exit_code: i32,
133    pub signal: Option<String>,
134    pub output: String,
135    pub transcript: String,
136    pub frames: Vec<TerminalFrame>,
137    pub interaction_count: usize,
138    pub asciicast: Asciicast,
139}
140
141#[derive(Debug)]
142pub struct TerminalCaptureError {
143    message: String,
144    partial: Option<Box<TerminalCapture>>,
145}
146
147impl TerminalCaptureError {
148    pub(crate) fn new(message: impl Into<String>, partial: Option<TerminalCapture>) -> Self {
149        Self {
150            message: message.into(),
151            partial: partial.map(Box::new),
152        }
153    }
154
155    pub fn partial_capture(&self) -> Option<&TerminalCapture> {
156        self.partial.as_deref()
157    }
158}
159
160impl fmt::Display for TerminalCaptureError {
161    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
162        formatter.write_str(&self.message)
163    }
164}
165
166impl std::error::Error for TerminalCaptureError {}