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
mod app;
mod event;
mod ui;
use crate::Result;
use crate::ipc::batch::StartOptions;
use crate::ipc::client::IpcClient;
use crate::settings::settings;
use crossterm::{
event::{DisableMouseCapture, EnableMouseCapture},
execute,
terminal::{EnterAlternateScreen, LeaveAlternateScreen, disable_raw_mode, enable_raw_mode},
};
use log::LevelFilter;
use miette::IntoDiagnostic;
use ratatui::prelude::*;
use std::io;
use std::sync::Arc;
pub use app::App;
pub async fn run() -> Result<()> {
// Suppress terminal logging while TUI is active (logs still go to file)
let prev_log_level = log::max_level();
log::set_max_level(LevelFilter::Off);
// Setup terminal
enable_raw_mode().into_diagnostic()?;
let mut stdout = io::stdout();
execute!(stdout, EnterAlternateScreen, EnableMouseCapture).into_diagnostic()?;
let backend = CrosstermBackend::new(stdout);
let mut terminal = Terminal::new(backend).into_diagnostic()?;
// Run with cleanup guaranteed
let result = run_with_cleanup(&mut terminal).await;
// Restore terminal (always runs)
let _ = disable_raw_mode();
let _ = execute!(
terminal.backend_mut(),
LeaveAlternateScreen,
DisableMouseCapture
);
let _ = terminal.show_cursor();
// Restore log level
log::set_max_level(prev_log_level);
result
}
async fn run_with_cleanup(terminal: &mut Terminal<CrosstermBackend<io::Stdout>>) -> Result<()> {
// Connect to supervisor (auto-start if needed)
let client = Arc::new(IpcClient::connect(true).await?);
// Create app state
let mut app = App::new();
app.refresh(&client).await?;
// Run main loop
run_app(terminal, &mut app, &client).await
}
async fn run_app(
terminal: &mut Terminal<CrosstermBackend<io::Stdout>>,
app: &mut App,
client: &Arc<IpcClient>,
) -> Result<()> {
let s = settings();
let tick_rate = s.tui_tick_rate();
let refresh_rate = s.tui_refresh_rate();
let mut last_refresh = std::time::Instant::now();
loop {
// Draw UI
terminal.draw(|f| ui::draw(f, app)).into_diagnostic()?;
// Handle events with timeout
if crossterm::event::poll(tick_rate).into_diagnostic()?
&& let Some(action) = event::handle_event(app)?
{
match action {
event::Action::Quit => break,
event::Action::Start(id) => {
app.start_loading(format!("Starting {id}..."));
terminal.draw(|f| ui::draw(f, app)).into_diagnostic()?;
let result = client
.start_daemons(std::slice::from_ref(&id), StartOptions::default())
.await;
app.stop_loading();
match result {
Ok(r) if r.any_failed => {
app.set_message(format!("Failed to start {id}"));
}
Ok(r) if !r.started.is_empty() => {
app.set_message(format!("Started {id}"));
}
Ok(_) => {
app.set_message(format!("No daemons were started for {id}"));
}
Err(e) => {
app.set_message(format!("Failed to start {id}: {e}"));
}
}
app.refresh(client).await?;
}
event::Action::Enable(id) => {
app.start_loading(format!("Enabling {id}..."));
terminal.draw(|f| ui::draw(f, app)).into_diagnostic()?;
client.enable(id.clone()).await?;
app.stop_loading();
app.set_message(format!("Enabled {id}"));
app.refresh(client).await?;
}
event::Action::BatchStart(ids) => {
let count = ids.len();
app.start_loading(format!("Starting {count} daemons..."));
terminal.draw(|f| ui::draw(f, app)).into_diagnostic()?;
let result = client.start_daemons(&ids, StartOptions::default()).await;
app.stop_loading();
app.clear_selection();
match result {
Ok(r) => {
let started = r.started.len();
if r.any_failed {
app.set_message(format!(
"Started {started}/{count} daemons (some failed)"
));
} else {
app.set_message(format!("Started {started} daemons"));
}
}
Err(e) => {
app.set_message(format!("Failed to start daemons: {e}"));
}
}
app.refresh(client).await?;
}
event::Action::BatchEnable(ids) => {
let count = ids.len();
app.start_loading(format!("Enabling {count} daemons..."));
terminal.draw(|f| ui::draw(f, app)).into_diagnostic()?;
for id in &ids {
let _ = client.enable(id.clone()).await;
}
app.stop_loading();
app.clear_selection();
app.set_message(format!("Enabled {count} daemons"));
app.refresh(client).await?;
}
event::Action::Refresh => {
app.start_loading("Refreshing...");
terminal.draw(|f| ui::draw(f, app)).into_diagnostic()?;
app.refresh(client).await?;
app.stop_loading();
}
event::Action::OpenEditorNew => {
app.open_file_selector();
}
event::Action::OpenEditorEdit(id) => {
app.open_editor_edit(&id);
}
event::Action::SaveConfig => {
app.start_loading("Saving...");
terminal.draw(|f| ui::draw(f, app)).into_diagnostic()?;
match app.save_editor_config() {
Ok(true) => {
// Successfully saved
app.stop_loading();
app.close_editor();
app.refresh(client).await?;
}
Ok(false) => {
// Validation or duplicate error - don't close editor
app.stop_loading();
}
Err(e) => {
app.stop_loading();
app.set_message(format!("Save failed: {e}"));
}
}
}
event::Action::DeleteDaemon { id, config_path } => {
app.confirm_action(app::PendingAction::DeleteDaemon { id, config_path });
}
event::Action::ConfirmPending => {
if let Some(pending) = app.take_pending_action() {
match pending {
app::PendingAction::Stop(id) => {
app.start_loading(format!("Stopping {id}..."));
terminal.draw(|f| ui::draw(f, app)).into_diagnostic()?;
let result = client.stop(id.clone()).await;
app.stop_loading();
match result {
Ok(true) => app.set_message(format!("Stopped {id}")),
Ok(false) => {
app.set_message(format!("Daemon {id} was not running"))
}
Err(e) => app.set_message(format!("Failed to stop {id}: {e}")),
}
}
app::PendingAction::Restart(id) => {
app.start_loading(format!("Restarting {id}..."));
terminal.draw(|f| ui::draw(f, app)).into_diagnostic()?;
// Restart is just start --force
let opts = StartOptions {
force: true,
..Default::default()
};
let result =
client.start_daemons(std::slice::from_ref(&id), opts).await;
app.stop_loading();
match result {
Ok(r) if r.any_failed => {
app.set_message(format!("Failed to restart {id}"));
}
Ok(_) => {
app.set_message(format!("Restarted {id}"));
}
Err(e) => {
app.set_message(format!("Failed to restart {id}: {e}"));
}
}
}
app::PendingAction::Disable(id) => {
app.start_loading(format!("Disabling {id}..."));
terminal.draw(|f| ui::draw(f, app)).into_diagnostic()?;
client.disable(id.clone()).await?;
app.stop_loading();
app.set_message(format!("Disabled {id}"));
}
app::PendingAction::BatchStop(ids) => {
let count = ids.len();
app.start_loading(format!("Stopping {count} daemons..."));
terminal.draw(|f| ui::draw(f, app)).into_diagnostic()?;
let result = client.stop_daemons(&ids).await;
app.stop_loading();
app.clear_selection();
match result {
Ok(r) if r.any_failed => {
app.set_message(format!(
"Stopped {count} daemons (some failed)"
));
}
Ok(_) => {
app.set_message(format!("Stopped {count} daemons"));
}
Err(e) => {
app.set_message(format!("Failed to stop daemons: {e}"));
}
}
}
app::PendingAction::BatchRestart(ids) => {
let count = ids.len();
app.start_loading(format!("Restarting {count} daemons..."));
terminal.draw(|f| ui::draw(f, app)).into_diagnostic()?;
// Restart is just start --force
let opts = StartOptions {
force: true,
..Default::default()
};
let result = client.start_daemons(&ids, opts).await;
app.stop_loading();
app.clear_selection();
match result {
Ok(r) => {
let restarted = r.started.len();
if r.any_failed {
app.set_message(format!("Restarted {restarted}/{count} daemons (some failed)"));
} else {
app.set_message(format!(
"Restarted {restarted} daemons"
));
}
}
Err(e) => {
app.set_message(format!("Failed to restart daemons: {e}"));
}
}
}
app::PendingAction::BatchDisable(ids) => {
let count = ids.len();
app.start_loading(format!("Disabling {count} daemons..."));
terminal.draw(|f| ui::draw(f, app)).into_diagnostic()?;
for id in &ids {
let _ = client.disable(id.clone()).await;
}
app.stop_loading();
app.clear_selection();
app.set_message(format!("Disabled {count} daemons"));
}
app::PendingAction::DeleteDaemon { id, config_path } => {
app.start_loading(format!("Deleting {id}..."));
terminal.draw(|f| ui::draw(f, app)).into_diagnostic()?;
match app.delete_daemon_from_config(&id, &config_path) {
Ok(true) => {
app.stop_loading();
app.close_editor();
app.set_message(format!("Deleted {id}"));
}
Ok(false) => {
app.stop_loading();
app.set_message(format!(
"Daemon '{id}' not found in config"
));
}
Err(e) => {
app.stop_loading();
app.set_message(format!("Delete failed: {e}"));
}
}
}
app::PendingAction::DiscardEditorChanges => {
app.close_editor();
}
}
app.refresh(client).await?;
}
}
}
}
// Auto-refresh daemon list
if last_refresh.elapsed() >= refresh_rate {
app.refresh(client).await?;
// Also refresh network data if viewing network view
if app.view == app::View::Network {
app.refresh_network().await;
}
last_refresh = std::time::Instant::now();
}
}
Ok(())
}