zinit 0.3.9

Process supervisor with dependency management
Documentation
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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
//! UI rendering for the TUI.

use std::time::Duration;

use ratatui::{
    Frame,
    layout::{Constraint, Direction, Layout, Rect},
    style::{Color, Modifier, Style},
    text::{Line, Span},
    widgets::{Block, Borders, Clear, List, ListItem, Paragraph, Wrap},
};

use crate::client::client::WhyBlocked;

use super::actions::ServiceDisplayInfo;
use super::app::{App, Focus, StatusLevel};

/// Get color for service state
fn state_color(state: &str) -> Color {
    match state {
        "running" => Color::Green,
        "starting" | "blocked" => Color::Yellow,
        "stopping" => Color::Magenta,
        "exited" => Color::Cyan,
        "failed" => Color::Red,
        "inactive" => Color::DarkGray,
        _ => Color::DarkGray,
    }
}

/// Get symbol for service state
fn state_symbol(state: &str) -> &'static str {
    match state {
        "inactive" => "[-]",
        "blocked" => "[?]",
        "starting" => "[>]",
        "running" => "[+]",
        "stopping" | "exited" => "[.]",
        "failed" => "[X]",
        _ => "[?]",
    }
}

/// Main draw function
pub fn draw(frame: &mut Frame, app: &App) {
    // Create main layout
    let chunks = Layout::default()
        .direction(Direction::Vertical)
        .constraints([
            Constraint::Length(3), // Title
            Constraint::Min(10),   // Main content
            Constraint::Length(3), // Status bar
        ])
        .split(frame.area());

    draw_title(frame, app, chunks[0]);

    // Main content - split into services, deps, and logs
    let main_chunks = Layout::default()
        .direction(Direction::Horizontal)
        .constraints([
            Constraint::Percentage(30),
            Constraint::Percentage(25),
            Constraint::Percentage(45),
        ])
        .split(chunks[1]);

    draw_services(frame, app, main_chunks[0]);
    draw_info_panel(frame, app, main_chunks[1]);
    draw_logs(frame, app, main_chunks[2]);

    draw_status_bar(frame, app, chunks[2]);

    // Popups
    if app.show_help {
        draw_help_popup(frame);
    }

    if app.show_detail {
        if let Some(service) = app.selected_service() {
            draw_detail_popup(frame, service);
        }
    }

    if app.show_why {
        if let Some(why) = &app.why_info {
            draw_why_popup(frame, why);
        }
    }
}

fn draw_title(frame: &mut Frame, app: &App, area: Rect) {
    let connection_indicator = if app.connected {
        Span::styled(" [connected]", Style::default().fg(Color::Green))
    } else {
        Span::styled(
            " [DISCONNECTED]",
            Style::default().fg(Color::Red).add_modifier(Modifier::BOLD),
        )
    };

    let title = Paragraph::new(Line::from(vec![
        Span::styled(
            "zinit ",
            Style::default()
                .fg(Color::Cyan)
                .add_modifier(Modifier::BOLD),
        ),
        Span::styled("TUI", Style::default().fg(Color::White)),
        connection_indicator,
        Span::raw(" | "),
        Span::styled("?", Style::default().fg(Color::Yellow)),
        Span::raw(" Help  "),
        Span::styled("w", Style::default().fg(Color::Yellow)),
        Span::raw(" Why  "),
        Span::styled("Q", Style::default().fg(Color::Yellow)),
        Span::raw(" Quit"),
    ]))
    .block(
        Block::default()
            .borders(Borders::ALL)
            .border_style(Style::default().fg(Color::Cyan)),
    );
    frame.render_widget(title, area);
}

fn draw_services(frame: &mut Frame, app: &App, area: Rect) {
    let items: Vec<ListItem> = app
        .services
        .iter()
        .map(|service| {
            let state_style = Style::default().fg(state_color(&service.state));

            let content = Line::from(vec![
                Span::styled(format!("{} ", state_symbol(&service.state)), state_style),
                Span::styled(&service.name, Style::default().fg(Color::White)),
            ]);
            ListItem::new(content)
        })
        .collect();

    let border_color = if app.focus == Focus::Services {
        Color::Cyan
    } else {
        Color::DarkGray
    };

    let services_list = List::new(items)
        .block(
            Block::default()
                .title(format!(" Services ({}) ", app.services.len()))
                .borders(Borders::ALL)
                .border_style(Style::default().fg(border_color)),
        )
        .highlight_style(
            Style::default()
                .bg(Color::DarkGray)
                .add_modifier(Modifier::BOLD),
        )
        .highlight_symbol("> ");

    frame.render_stateful_widget(services_list, area, &mut app.selected.clone());
}

fn draw_info_panel(frame: &mut Frame, app: &App, area: Rect) {
    let border_color = if app.focus == Focus::Deps {
        Color::Cyan
    } else {
        Color::DarkGray
    };

    let info_lines: Vec<Line> = if let Some(service) = app.selected_service() {
        let mut lines = vec![Line::from(vec![
            Span::styled("State: ", Style::default().fg(Color::Yellow)),
            Span::styled(
                &service.state,
                Style::default().fg(state_color(&service.state)),
            ),
        ])];

        if service.pid > 0 {
            lines.push(Line::from(vec![
                Span::styled("PID: ", Style::default().fg(Color::Yellow)),
                Span::styled(
                    format!("{}", service.pid),
                    Style::default().fg(Color::White),
                ),
            ]));
        }

        if let Some(code) = service.exit_code {
            lines.push(Line::from(vec![
                Span::styled("Exit code: ", Style::default().fg(Color::Yellow)),
                Span::styled(format!("{}", code), Style::default().fg(Color::Cyan)),
            ]));
        }

        if let Some(ref err) = service.error {
            lines.push(Line::from(vec![
                Span::styled("Error: ", Style::default().fg(Color::Red)),
                Span::styled(err, Style::default().fg(Color::White)),
            ]));
        }

        // Show child processes if any
        if !app.children.is_empty() {
            lines.push(Line::from(""));
            lines.push(Line::from(vec![Span::styled(
                format!("Children ({}):", app.children.len()),
                Style::default().fg(Color::Magenta),
            )]));
            for child in &app.children {
                let mem_mb = child.memory_bytes as f64 / (1024.0 * 1024.0);
                lines.push(Line::from(vec![
                    Span::styled(
                        format!("  {} ", child.pid),
                        Style::default().fg(Color::White),
                    ),
                    Span::styled(&child.name, Style::default().fg(Color::Gray)),
                    Span::styled(
                        format!(" ({:.1}MB)", mem_mb),
                        Style::default().fg(Color::DarkGray),
                    ),
                ]));
            }
        }

        lines.push(Line::from(""));
        lines.push(Line::from(Span::styled(
            "Press 'w' to see blocking info",
            Style::default().fg(Color::DarkGray),
        )));

        lines
    } else {
        vec![Line::from(Span::styled(
            "No service selected",
            Style::default().fg(Color::DarkGray),
        ))]
    };

    let info_widget = Paragraph::new(info_lines)
        .block(
            Block::default()
                .title(" Service Info ")
                .borders(Borders::ALL)
                .border_style(Style::default().fg(border_color)),
        )
        .wrap(Wrap { trim: false });

    frame.render_widget(info_widget, area);
}

fn draw_logs(frame: &mut Frame, app: &App, area: Rect) {
    let border_color = if app.focus == Focus::Logs {
        Color::Cyan
    } else {
        Color::DarkGray
    };

    let service_name = app
        .selected_service()
        .map(|s| s.name.clone())
        .unwrap_or_else(|| "none".to_string());

    let visible_height = (area.height as usize).saturating_sub(2);
    let total_logs = app.logs.len();

    let scroll_offset = if app.following_logs {
        total_logs.saturating_sub(visible_height)
    } else {
        app.log_scroll
            .min(total_logs.saturating_sub(visible_height))
    };

    let visible_logs: Vec<Line> = app
        .logs
        .iter()
        .skip(scroll_offset)
        .take(visible_height)
        .map(|line| {
            let style = if line.contains("[ERR]")
                || line.contains("error")
                || line.contains("Error")
                || line.contains("ERROR")
            {
                Style::default().fg(Color::Red)
            } else if line.contains("warn") || line.contains("Warn") || line.contains("WARN") {
                Style::default().fg(Color::Yellow)
            } else {
                Style::default().fg(Color::Gray)
            };
            Line::from(Span::styled(line.clone(), style))
        })
        .collect();

    let follow_indicator = if app.following_logs {
        " [following]"
    } else {
        ""
    };
    let scroll_info = format!(
        " {}/{} ",
        scroll_offset + visible_height.min(total_logs),
        total_logs
    );

    let logs_widget = Paragraph::new(visible_logs)
        .block(
            Block::default()
                .title(format!(" Logs: {}{} ", service_name, follow_indicator))
                .title_bottom(Line::from(scroll_info).centered())
                .borders(Borders::ALL)
                .border_style(Style::default().fg(border_color)),
        )
        .wrap(Wrap { trim: false });

    frame.render_widget(logs_widget, area);
}

fn draw_status_bar(frame: &mut Frame, app: &App, area: Rect) {
    let mut spans = Vec::new();

    // Show pending operations with elapsed time
    for op in &app.pending_operations {
        let elapsed = op.started.elapsed().as_secs();
        spans.push(Span::styled(
            format!("{}({})...{}s ", op.action, op.service, elapsed),
            Style::default().fg(Color::Yellow),
        ));
    }

    // Show status message if any
    if let Some((msg, time, level)) = &app.status_message {
        if time.elapsed() < Duration::from_secs(5) {
            let color = match level {
                StatusLevel::Info => Color::White,
                StatusLevel::Success => Color::Green,
                StatusLevel::Warning => Color::Yellow,
                StatusLevel::Error => Color::Red,
            };
            spans.push(Span::styled(msg, Style::default().fg(color)));
        }
    }

    // Default status if no pending ops or messages
    if spans.is_empty() {
        let service_info = app
            .selected_service()
            .map(|s| {
                let pid_str = if s.pid > 0 {
                    format!(" PID:{}", s.pid)
                } else {
                    String::new()
                };
                format!(
                    "{} {} {}{}",
                    state_symbol(&s.state),
                    s.name,
                    s.state,
                    pid_str
                )
            })
            .unwrap_or_else(|| "No service selected".to_string());

        let focus_indicator = match app.focus {
            Focus::Services => "[Services]",
            Focus::Deps => "[Deps]",
            Focus::Logs => "[Logs]",
        };

        spans.push(Span::raw(format!(
            "{} | {} | Tab: cycle focus",
            service_info, focus_indicator
        )));
    }

    let status = Paragraph::new(Line::from(spans))
        .style(Style::default().fg(Color::White))
        .block(
            Block::default()
                .borders(Borders::ALL)
                .border_style(Style::default().fg(Color::DarkGray)),
        );
    frame.render_widget(status, area);
}

fn draw_help_popup(frame: &mut Frame) {
    let area = frame.area();
    let popup_area = Rect {
        x: area.width / 6,
        y: area.height / 6,
        width: area.width * 2 / 3,
        height: area.height * 2 / 3,
    };

    frame.render_widget(Clear, popup_area);

    let help_text = vec![
        Line::from(Span::styled(
            "Keyboard Shortcuts",
            Style::default()
                .fg(Color::Cyan)
                .add_modifier(Modifier::BOLD),
        )),
        Line::from(""),
        Line::from(vec![Span::styled(
            "Navigation:",
            Style::default()
                .fg(Color::Yellow)
                .add_modifier(Modifier::BOLD),
        )]),
        Line::from(vec![
            Span::styled("  Up/k      ", Style::default().fg(Color::Green)),
            Span::raw("Move up in service list"),
        ]),
        Line::from(vec![
            Span::styled("  Down/j    ", Style::default().fg(Color::Green)),
            Span::raw("Move down in service list"),
        ]),
        Line::from(vec![
            Span::styled("  Tab       ", Style::default().fg(Color::Green)),
            Span::raw("Cycle focus: Services -> Deps -> Logs"),
        ]),
        Line::from(vec![
            Span::styled("  PgUp/PgDn ", Style::default().fg(Color::Green)),
            Span::raw("Scroll logs"),
        ]),
        Line::from(vec![
            Span::styled("  g/G       ", Style::default().fg(Color::Green)),
            Span::raw("Go to top/bottom of logs"),
        ]),
        Line::from(""),
        Line::from(vec![Span::styled(
            "Actions:",
            Style::default()
                .fg(Color::Yellow)
                .add_modifier(Modifier::BOLD),
        )]),
        Line::from(vec![
            Span::styled("  s         ", Style::default().fg(Color::Green)),
            Span::raw("Start selected service"),
        ]),
        Line::from(vec![
            Span::styled("  S         ", Style::default().fg(Color::Green)),
            Span::raw("Stop selected service"),
        ]),
        Line::from(vec![
            Span::styled("  r         ", Style::default().fg(Color::Green)),
            Span::raw("Restart selected service"),
        ]),
        Line::from(vec![
            Span::styled("  K         ", Style::default().fg(Color::Green)),
            Span::raw("Kill with SIGKILL"),
        ]),
        Line::from(vec![
            Span::styled("  d         ", Style::default().fg(Color::Green)),
            Span::raw("Remove service"),
        ]),
        Line::from(vec![
            Span::styled("  D         ", Style::default().fg(Color::Green)),
            Span::raw("Halt & Remove (kill tree + remove)"),
        ]),
        Line::from(vec![
            Span::styled("  R         ", Style::default().fg(Color::Green)),
            Span::raw("Refresh service list"),
        ]),
        Line::from(""),
        Line::from(vec![Span::styled(
            "Info:",
            Style::default()
                .fg(Color::Yellow)
                .add_modifier(Modifier::BOLD),
        )]),
        Line::from(vec![
            Span::styled("  w         ", Style::default().fg(Color::Green)),
            Span::raw("Show why service is blocked"),
        ]),
        Line::from(vec![
            Span::styled("  Enter     ", Style::default().fg(Color::Green)),
            Span::raw("Show service details"),
        ]),
        Line::from(vec![
            Span::styled("  ?         ", Style::default().fg(Color::Green)),
            Span::raw("Toggle this help"),
        ]),
        Line::from(vec![
            Span::styled("  Q/Esc     ", Style::default().fg(Color::Green)),
            Span::raw("Quit"),
        ]),
        Line::from(""),
        Line::from(Span::styled(
            "Press any key to close",
            Style::default().fg(Color::DarkGray),
        )),
    ];

    let help = Paragraph::new(help_text)
        .block(
            Block::default()
                .title(" Help ")
                .borders(Borders::ALL)
                .border_style(Style::default().fg(Color::Cyan)),
        )
        .style(Style::default().bg(Color::Black));

    frame.render_widget(help, popup_area);
}

fn draw_detail_popup(frame: &mut Frame, service: &ServiceDisplayInfo) {
    let area = frame.area();
    let popup_area = Rect {
        x: area.width / 6,
        y: area.height / 6,
        width: area.width * 2 / 3,
        height: area.height * 2 / 3,
    };

    frame.render_widget(Clear, popup_area);

    let state_col = state_color(&service.state);

    let mut detail_text = vec![
        Line::from(Span::styled(
            format!(" {} ", service.name),
            Style::default()
                .fg(Color::Cyan)
                .add_modifier(Modifier::BOLD),
        )),
        Line::from(""),
        Line::from(vec![Span::styled(
            "Status",
            Style::default()
                .fg(Color::Yellow)
                .add_modifier(Modifier::BOLD),
        )]),
        Line::from(vec![
            Span::raw("  State:  "),
            Span::styled(
                format!("{} {}", state_symbol(&service.state), &service.state),
                Style::default().fg(state_col).add_modifier(Modifier::BOLD),
            ),
        ]),
    ];

    if service.pid > 0 {
        detail_text.push(Line::from(vec![
            Span::raw("  PID:    "),
            Span::styled(service.pid.to_string(), Style::default().fg(Color::White)),
        ]));
    }

    if let Some(code) = service.exit_code {
        detail_text.push(Line::from(vec![
            Span::raw("  Exit:   "),
            Span::styled(format!("{}", code), Style::default().fg(Color::Cyan)),
        ]));
    }

    if let Some(ref err) = service.error {
        detail_text.push(Line::from(""));
        detail_text.push(Line::from(vec![Span::styled(
            "Error",
            Style::default().fg(Color::Red).add_modifier(Modifier::BOLD),
        )]));
        detail_text.push(Line::from(vec![
            Span::raw("  "),
            Span::styled(err, Style::default().fg(Color::Red)),
        ]));
    }

    detail_text.push(Line::from(""));
    detail_text.push(Line::from(Span::styled(
        "Press Esc to close",
        Style::default().fg(Color::DarkGray),
    )));

    let detail = Paragraph::new(detail_text)
        .block(
            Block::default()
                .title(" Service Details ")
                .borders(Borders::ALL)
                .border_style(Style::default().fg(Color::Cyan)),
        )
        .style(Style::default().bg(Color::Black));

    frame.render_widget(detail, popup_area);
}

fn draw_why_popup(frame: &mut Frame, why: &WhyBlocked) {
    let area = frame.area();
    let popup_area = Rect {
        x: area.width / 8,
        y: area.height / 8,
        width: area.width * 3 / 4,
        height: area.height * 3 / 4,
    };

    frame.render_widget(Clear, popup_area);

    let mut lines = vec![Line::from(Span::styled(
        "Why Blocked?",
        Style::default()
            .fg(Color::Cyan)
            .add_modifier(Modifier::BOLD),
    ))];

    if !why.blocked {
        lines.push(Line::from(""));
        lines.push(Line::from(Span::styled(
            "Service is not blocked",
            Style::default().fg(Color::Green),
        )));
    } else {
        lines.push(Line::from(""));

        // ASCII visualization
        for line in why.ascii.lines() {
            lines.push(Line::from(Span::styled(
                line.to_string(),
                Style::default().fg(Color::White),
            )));
        }

        if !why.waiting_on.is_empty() {
            lines.push(Line::from(""));
            lines.push(Line::from(vec![
                Span::styled("Waiting on: ", Style::default().fg(Color::Yellow)),
                Span::styled(why.waiting_on.join(", "), Style::default().fg(Color::White)),
            ]));
        }

        if !why.conflicts_with.is_empty() {
            lines.push(Line::from(vec![
                Span::styled("Conflicts with: ", Style::default().fg(Color::Red)),
                Span::styled(
                    why.conflicts_with.join(", "),
                    Style::default().fg(Color::White),
                ),
            ]));
        }
    }

    lines.push(Line::from(""));
    lines.push(Line::from(Span::styled(
        "Press any key to close",
        Style::default().fg(Color::DarkGray),
    )));

    let why_widget = Paragraph::new(lines)
        .block(
            Block::default()
                .title(" Why Blocked? ")
                .borders(Borders::ALL)
                .border_style(Style::default().fg(Color::Yellow)),
        )
        .style(Style::default().bg(Color::Black))
        .wrap(Wrap { trim: false });

    frame.render_widget(why_widget, popup_area);
}