Rust · 3074 bytes Raw Blame History
1 use serde::{Deserialize, Serialize};
2
3 /// Commands sent to garterm daemon
4 #[derive(Debug, Clone, Serialize, Deserialize)]
5 #[serde(tag = "cmd", rename_all = "snake_case")]
6 pub enum Command {
7 /// Create new window
8 NewWindow {
9 cwd: Option<String>,
10 exec: Option<String>,
11 },
12 /// Create new tab in focused window
13 NewTab {
14 cwd: Option<String>,
15 /// Startup command to run after shell prompt
16 #[serde(default, rename = "exec")]
17 startup_cmd: Option<String>,
18 #[serde(default)]
19 title: Option<String>,
20 },
21 /// Close current tab
22 CloseTab,
23 /// Switch to next tab
24 NextTab,
25 /// Switch to previous tab
26 PrevTab,
27 /// Switch to specific tab
28 SwitchTab { index: usize },
29 /// Split focused pane
30 Split {
31 direction: String,
32 #[serde(default)]
33 cwd: Option<String>,
34 /// Startup command to run after shell prompt
35 #[serde(default, rename = "exec")]
36 startup_cmd: Option<String>,
37 },
38 /// Close focused pane
39 ClosePane,
40 /// Focus pane in direction
41 FocusPaneDirection { direction: String },
42 /// Resize pane
43 ResizePane { direction: String, amount: i32 },
44 /// Send text to focused terminal
45 SendText { text: String },
46 /// Load a named session from config
47 LoadSession { name: String },
48 /// Get terminal info
49 GetInfo,
50 /// Reload configuration
51 Reload,
52 /// Quit daemon
53 Quit,
54 /// Ping to check if instance is alive
55 Ping,
56 }
57
58 /// Information about a garterm window instance
59 #[derive(Debug, Clone, Serialize, Deserialize)]
60 pub struct WindowInfo {
61 pub pid: u32,
62 pub tabs: usize,
63 pub focused: bool,
64 }
65
66 /// Response from garterm daemon
67 #[derive(Debug, Clone, Serialize, Deserialize)]
68 pub struct Response {
69 pub success: bool,
70 #[serde(skip_serializing_if = "Option::is_none")]
71 pub message: Option<String>,
72 #[serde(skip_serializing_if = "Option::is_none")]
73 pub data: Option<serde_json::Value>,
74 }
75
76 impl Response {
77 pub fn ok() -> Self {
78 Self {
79 success: true,
80 message: None,
81 data: None,
82 }
83 }
84
85 pub fn ok_with_data(data: serde_json::Value) -> Self {
86 Self {
87 success: true,
88 message: None,
89 data: Some(data),
90 }
91 }
92
93 pub fn error(message: impl Into<String>) -> Self {
94 Self {
95 success: false,
96 message: Some(message.into()),
97 data: None,
98 }
99 }
100 }
101
102 /// Events broadcast to subscribers
103 #[derive(Debug, Clone, Serialize, Deserialize)]
104 #[serde(tag = "event", rename_all = "snake_case")]
105 pub enum Event {
106 /// Terminal title changed
107 TitleChanged {
108 window_id: u32,
109 pane_id: u32,
110 title: String,
111 },
112 /// Pane was created
113 PaneCreated { window_id: u32, pane_id: u32 },
114 /// Pane was closed
115 PaneClosed {
116 window_id: u32,
117 pane_id: u32,
118 exit_code: i32,
119 },
120 /// Bell received
121 Bell { window_id: u32, pane_id: u32 },
122 }
123