par_term_scripting/protocol.rs
1//! JSON protocol types for communication between the terminal and script subprocesses.
2//!
3//! Scripts read [`ScriptEvent`] objects from stdin (one JSON object per line) and write
4//! [`ScriptCommand`] objects to stdout (one JSON object per line).
5
6use serde::{Deserialize, Serialize};
7use std::collections::HashMap;
8
9/// An event sent from the terminal to a script subprocess (via stdin).
10#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
11pub struct ScriptEvent {
12 /// Event kind name (e.g., "bell_rang", "cwd_changed", "command_complete").
13 pub kind: String,
14 /// Event-specific payload.
15 pub data: ScriptEventData,
16}
17
18/// Event-specific payload data.
19///
20/// Tagged with `data_type` so the JSON includes a discriminant field for Python scripts
21/// to easily dispatch on.
22#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
23#[serde(tag = "data_type")]
24pub enum ScriptEventData {
25 /// Empty payload for events that carry no additional data (e.g., BellRang).
26 Empty {},
27
28 /// The current working directory changed.
29 CwdChanged {
30 /// New working directory path.
31 cwd: String,
32 },
33
34 /// A command completed execution.
35 CommandComplete {
36 /// The command that completed.
37 command: String,
38 /// Exit code, if available.
39 exit_code: Option<i32>,
40 },
41
42 /// The terminal title changed.
43 TitleChanged {
44 /// New terminal title.
45 title: String,
46 },
47
48 /// The terminal size changed.
49 SizeChanged {
50 /// Number of columns.
51 cols: usize,
52 /// Number of rows.
53 rows: usize,
54 },
55
56 /// A user variable changed.
57 VariableChanged {
58 /// Variable name.
59 name: String,
60 /// New value.
61 value: String,
62 /// Previous value, if any.
63 old_value: Option<String>,
64 },
65
66 /// An environment variable changed.
67 EnvironmentChanged {
68 /// Environment variable key.
69 key: String,
70 /// New value.
71 value: String,
72 /// Previous value, if any.
73 old_value: Option<String>,
74 },
75
76 /// The badge text changed.
77 BadgeChanged {
78 /// New badge text, or None if cleared.
79 text: Option<String>,
80 },
81
82 /// A trigger pattern was matched.
83 TriggerMatched {
84 /// The trigger pattern that matched.
85 pattern: String,
86 /// The text that matched.
87 matched_text: String,
88 /// Line number where the match occurred.
89 line: usize,
90 },
91
92 /// A semantic zone event occurred.
93 ZoneEvent {
94 /// Zone identifier.
95 zone_id: u64,
96 /// Type of zone.
97 zone_type: String,
98 /// Event type (e.g., "enter", "exit").
99 event: String,
100 },
101
102 /// Fallback for unmapped events. Carries arbitrary key-value fields.
103 Generic {
104 /// Arbitrary event fields.
105 fields: HashMap<String, serde_json::Value>,
106 },
107}
108
109/// A command sent from a script subprocess to the terminal (via stdout).
110///
111/// Tagged with `type` for easy JSON dispatch.
112#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
113#[serde(tag = "type")]
114pub enum ScriptCommand {
115 /// Write text to the PTY.
116 WriteText {
117 /// Text to write.
118 text: String,
119 },
120
121 /// Show a desktop notification.
122 Notify {
123 /// Notification title.
124 title: String,
125 /// Notification body.
126 body: String,
127 },
128
129 /// Set the tab badge text.
130 SetBadge {
131 /// Badge text to display.
132 text: String,
133 },
134
135 /// Set a user variable.
136 SetVariable {
137 /// Variable name.
138 name: String,
139 /// Variable value.
140 value: String,
141 },
142
143 /// Execute a shell command.
144 RunCommand {
145 /// Command to execute.
146 command: String,
147 },
148
149 /// Change a configuration value.
150 ChangeConfig {
151 /// Configuration key.
152 key: String,
153 /// New value.
154 value: serde_json::Value,
155 },
156
157 /// Log a message.
158 Log {
159 /// Log level (e.g., "info", "warn", "error", "debug").
160 level: String,
161 /// Log message.
162 message: String,
163 },
164
165 /// Set a markdown panel.
166 SetPanel {
167 /// Panel title.
168 title: String,
169 /// Markdown content.
170 content: String,
171 },
172
173 /// Clear the markdown panel.
174 ClearPanel {},
175}