node-app-build 5.22.0

Mini app developer CLI: scaffold, validate, package node-app-* Debian packages
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
use ratatui::{
    layout::{Constraint, Direction, Layout, Rect},
    style::{Color, Modifier, Style},
    text::{Line, Span},
    widgets::{Block, Borders, List, ListItem, Paragraph},
    Frame,
};

use super::state::{AppState, LogSource, ShutdownPhase};

pub fn draw(frame: &mut Frame, app: &mut AppState) {
    let area = frame.area();
    let rows = Layout::default()
        .direction(Direction::Vertical)
        .constraints([Constraint::Min(0), Constraint::Length(1)])
        .split(area);

    render_body(frame, rows[0], app);
    render_footer(frame, rows[1], app);
}


fn render_body(frame: &mut Frame, area: Rect, app: &mut AppState) {
    let cols = Layout::default()
        .direction(Direction::Horizontal)
        .constraints([Constraint::Length(26), Constraint::Min(0)])
        .split(area);
    app.layout.service_list = cols[0];
    render_service_list(frame, cols[0], app);
    render_log_pane(frame, cols[1], app);
}

fn render_service_list(frame: &mut Frame, area: Rect, app: &AppState) {
    let t = app.active_timings();

    let items: Vec<ListItem> = app
        .active_services()
        .iter()
        .map(|(source, svc)| {
            let active = *source == app.active_pane;
            let lbl_style = if active {
                Style::default().fg(Color::White).add_modifier(Modifier::BOLD)
            } else {
                Style::default().fg(Color::Gray)
            };
            let dim = Style::default().fg(Color::DarkGray);

            let line1 = Line::from(vec![
                Span::styled(
                    if active { "โ–ถ" } else { " " },
                    Style::default().fg(Color::Cyan),
                ),
                Span::styled(
                    format!("{} ", svc.status.indicator()),
                    Style::default().fg(svc.status.color()),
                ),
                Span::styled(format!("{:<10}", svc.label), lbl_style),
                Span::styled(svc.status.summary(), Style::default().fg(svc.status.color())),
            ]);

            // Line 2: existing detail (port, url, etc.)
            let line2 = if svc.detail.is_empty() {
                Line::raw("")
            } else {
                Line::from(Span::styled(format!("   {}", &svc.detail), dim))
            };

            // Line 3: timing metadata โ€” computed fresh every frame.
            let timing_str = build_timing_line(*source, svc, t);
            let line3 = if timing_str.is_empty() {
                Line::raw("")
            } else {
                Line::from(vec![
                    Span::styled("   ", dim),
                    Span::styled(timing_str, Style::default().fg(Color::Cyan)),
                ])
            };

            ListItem::new(vec![line1, line2, line3])
        })
        .collect();

    let title = if let Some(name) = app.instance_filter_label() {
        format!(" {} v{}  [{}] ", app.app_name, app.app_version, name)
    } else {
        format!(" {} v{} ", app.app_name, app.app_version)
    };
    let list = List::new(items).block(Block::default().borders(Borders::RIGHT).title(title));
    frame.render_widget(list, area);
}

/// Build the timing metadata string for a service sidebar entry.
/// Returns an empty string when there is nothing useful to show.
fn build_timing_line(
    source: LogSource,
    svc: &super::state::ServiceState,
    t: &super::state::Timings,
) -> String {
    match source {
        LogSource::Build => {
            match &svc.status {
                super::state::ServiceStatus::Building => {
                    let elapsed = t
                        .build_started_at
                        .map(|s| s.elapsed().as_millis())
                        .unwrap_or(0);
                    let count_hint = if t.build_count > 0 {
                        format!("  ร—{} prev", t.build_count)
                    } else {
                        String::new()
                    };
                    format!("elapsed {}{}", fmt_ms(elapsed as u64), count_hint)
                }
                super::state::ServiceStatus::Ready => {
                    let builtins = t.builtin_apps_build_ms
                        .map(|ms| format!("builtins {}  ", fmt_ms(ms)))
                        .unwrap_or_default();
                    match t.last_build_ms {
                        Some(ms) => format!("{}last {}  ร—{} builds", builtins, fmt_ms(ms), t.build_count),
                        None if !builtins.is_empty() => builtins.trim_end().to_string(),
                        None => String::new(),
                    }
                }
                super::state::ServiceStatus::Failed(_) => {
                    match t.last_build_ms {
                        Some(ms) => format!("failed {}  ร—{} total", fmt_ms(ms), t.build_count),
                        None => String::new(),
                    }
                }
                _ => String::new(),
            }
        }
        LogSource::App => {
            if let Some(ms) = t.last_reload_ms {
                format!("reload {}", fmt_ms(ms))
            } else {
                String::new()
            }
        }
        LogSource::Daemon => {
            let build_part = t.platform_build_ms
                .map(|ms| format!("build {}  ", fmt_ms(ms)))
                .unwrap_or_default();
            if let Some(started) = t.daemon_started_at {
                format!("{}up {}", build_part, fmt_duration(started.elapsed().as_secs()))
            } else if !build_part.is_empty() {
                build_part.trim_end().to_string()
            } else {
                String::new()
            }
        }
        _ => String::new(),
    }
}

/// Format milliseconds compactly: <1 000 ms โ†’ "123ms", โ‰ฅ1 000 ms โ†’ "1.2s".
fn fmt_ms(ms: u64) -> String {
    if ms < 1_000 {
        format!("{ms}ms")
    } else {
        format!("{:.1}s", ms as f64 / 1_000.0)
    }
}

/// Format seconds as "Xm Ys" or just "Xs".
fn fmt_duration(secs: u64) -> String {
    if secs < 60 {
        format!("{secs}s")
    } else {
        format!("{}m {}s", secs / 60, secs % 60)
    }
}

fn render_log_pane(frame: &mut Frame, area: Rect, app: &mut AppState) {
    let rows = Layout::default()
        .direction(Direction::Vertical)
        .constraints([Constraint::Length(1), Constraint::Min(0)])
        .split(area);
    app.layout.log_scroll = rows[1];

    render_timings_bar(frame, rows[0], app);
    render_log_scroll(frame, rows[1], app);
}

fn render_timings_bar(frame: &mut Frame, area: Rect, app: &AppState) {
    let t = app.active_timings();
    let dim = Style::default().fg(Color::DarkGray);
    let val = Style::default().fg(Color::Cyan);
    let sep = Style::default().fg(Color::DarkGray);

    let mut spans: Vec<Span> = Vec::new();

    macro_rules! kv {
        ($label:expr, $value:expr) => {
            if !spans.is_empty() {
                spans.push(Span::styled("  ", sep));
            }
            spans.push(Span::styled(concat!($label, ":"), dim));
            spans.push(Span::styled($value, val));
        };
    }

    if let Some(ms) = t.platform_build_ms {
        kv!("platform", fmt_ms(ms));
    } else if t.platform_build_started_at.is_some() {
        let elapsed = t.platform_build_started_at.map(|s| s.elapsed().as_millis()).unwrap_or(0);
        kv!("platform", format!("{}โ€ฆ", fmt_ms(elapsed as u64)));
    }
    if let Some(ms) = t.builtin_apps_build_ms {
        kv!("builtins", fmt_ms(ms));
    }
    match (t.last_build_ms, t.build_count) {
        (Some(ms), n) => {
            let v = if n > 1 { format!("{} ร—{}", fmt_ms(ms), n) } else { fmt_ms(ms) };
            kv!("app", v);
        }
        (None, _) if t.build_started_at.is_some() => {
            let elapsed = t.build_started_at.map(|s| s.elapsed().as_millis()).unwrap_or(0);
            kv!("app", format!("{}โ€ฆ", fmt_ms(elapsed as u64)));
        }
        _ => {}
    }
    if let Some(ms) = t.last_reload_ms {
        kv!("reload", fmt_ms(ms));
    }
    if let Some(started) = t.daemon_started_at {
        kv!("up", fmt_duration(started.elapsed().as_secs()));
    }

    // Right side: active pane label (+ match count when search is active).
    let pane_label = {
        let src = app.active_pane;
        if !app.search_query.is_empty() {
            let total = app.log_lines(src).len();
            let matched = app.log_lines(src).iter().filter(|l| app.matches(l)).count();
            format!(" {matched}/{total} ")
        } else {
            format!(" {} ", src.tab_label())
        }
    };

    let mut all_spans: Vec<Span> = vec![Span::raw(" ")];
    all_spans.extend(spans);

    // Pad the gap then right-align the pane label.
    let content_width: usize = all_spans.iter().map(|s| s.content.len()).sum();
    let label_width = pane_label.len();
    let area_width = area.width as usize;
    if content_width + label_width < area_width {
        let pad = area_width - content_width - label_width;
        all_spans.push(Span::raw(" ".repeat(pad)));
    }
    all_spans.push(Span::styled(
        pane_label,
        Style::default().fg(Color::Black).bg(Color::Cyan),
    ));

    frame.render_widget(Paragraph::new(Line::from(all_spans)), area);
}


fn render_log_scroll(frame: &mut Frame, area: Rect, app: &mut AppState) {
    let height = area.height as usize;
    app.last_render_height = height;

    // Capture values we need without holding a borrow on app.
    let source = app.active_pane;
    let search_query = app.search_query.clone();
    let selection = app.selection;

    let text: Vec<Line> = if search_query.is_empty() {
        // No text filter โ€” show raw lines with colorization and selection support.
        // `log_lines()` already returns the right buffer (global or instance-specific).
        let total = app.log_lines(source).len();
        let start = app.visible_start_for(total, height);
        app.log_scroll_start = start;
        app.log_lines(source)
            .iter()
            .enumerate()
            .skip(start)
            .take(height)
            .map(|(abs_idx, l)| {
                if selection.is_some_and(|s| s.contains(abs_idx)) {
                    colorize_selected(l.as_str())
                } else {
                    colorize(l.as_str(), source)
                }
            })
            .collect()
    } else {
        // Search filter โ€” fuzzy-highlight matched lines.
        app.log_scroll_start = 0;
        let matched: Vec<(String, Vec<usize>)> = app
            .log_lines(source)
            .iter()
            .filter_map(|l| {
                app.match_positions(l.as_str())
                    .map(|pos| (l.clone(), pos))
            })
            .collect();
        let start = app.visible_start_for(matched.len(), height);
        matched
            .into_iter()
            .skip(start)
            .take(height)
            .map(|(l, pos)| render_fuzzy_highlighted(&l, &pos, source))
            .collect()
    };

    frame.render_widget(Paragraph::new(text).block(Block::default()), area);
}

/// Render a log line with a selection highlight (blue background).
fn colorize_selected(line: &str) -> Line<'static> {
    Line::from(Span::styled(
        line.to_owned(),
        Style::default()
            .bg(Color::Rgb(50, 80, 130))
            .fg(Color::White),
    ))
}

/// Render a log line with fuzzy-matched character positions highlighted.
fn render_fuzzy_highlighted(line: &str, positions: &[usize], source: LogSource) -> Line<'static> {
    let base = base_style(line, source);
    let hl = Style::default()
        .fg(Color::Yellow)
        .add_modifier(Modifier::BOLD);

    if positions.is_empty() {
        return Line::from(Span::styled(line.to_owned(), base));
    }

    let chars: Vec<char> = line.chars().collect();
    let pos_set: std::collections::HashSet<usize> =
        positions.iter().copied().collect();

    let mut spans: Vec<Span<'static>> = Vec::new();
    let mut segment = String::new();
    let mut in_hl = false;

    for (i, c) in chars.iter().enumerate() {
        let want_hl = pos_set.contains(&i);
        if want_hl != in_hl {
            if !segment.is_empty() {
                spans.push(Span::styled(
                    segment.clone(),
                    if in_hl { hl } else { base },
                ));
                segment.clear();
            }
            in_hl = want_hl;
        }
        segment.push(*c);
    }
    if !segment.is_empty() {
        spans.push(Span::styled(segment, if in_hl { hl } else { base }));
    }
    Line::from(spans)
}

fn colorize(line: &str, source: LogSource) -> Line<'static> {
    Line::from(Span::styled(line.to_owned(), base_style(line, source)))
}

fn base_style(line: &str, source: LogSource) -> Style {
    if line.contains(" ERROR") || line.contains("error[") || line.contains("โœ—") {
        Style::default().fg(Color::Red)
    } else if line.contains(" WARN") || line.contains("warning[") || line.contains("โš ") {
        Style::default().fg(Color::Yellow)
    } else if line.contains("โœ“") || line.contains(" INFO") {
        Style::default().fg(Color::Green)
    } else if source == LogSource::Build
        && (line.contains("Compiling") || line.contains("Finished") || line.contains("โ†’"))
    {
        Style::default().fg(Color::Cyan)
    } else {
        Style::default().fg(Color::Gray)
    }
}

fn render_footer(frame: &mut Frame, area: Rect, app: &AppState) {
    // Copy flash โ€” 2 s confirmation after clipboard copy.
    if app
        .copy_flash
        .is_some_and(|t| t.elapsed() < std::time::Duration::from_secs(2))
    {
        let lines = app.selection.map_or(0, |s| {
            let (a, b) = s.range();
            b - a + 1
        });
        let text = Line::from(vec![
            Span::styled(
                " โœ“ copied",
                Style::default().fg(Color::Green).add_modifier(Modifier::BOLD),
            ),
            Span::styled(
                format!(" {lines} line{}", if lines == 1 { "" } else { "s" }),
                Style::default().fg(Color::White),
            ),
            Span::styled(
                " to clipboard",
                Style::default().fg(Color::DarkGray),
            ),
        ]);
        frame.render_widget(Paragraph::new(text), area);
        return;
    }

    if app.shutdown_phase == ShutdownPhase::ShuttingDown {
        let text = Line::from(vec![
            Span::styled(
                " โ— shutting down โ€” ",
                Style::default().fg(Color::Yellow),
            ),
            Span::styled(
                "stopping daemon, ui-server and cleaning upโ€ฆ",
                Style::default().fg(Color::DarkGray),
            ),
        ]);
        frame.render_widget(Paragraph::new(text), area);
        return;
    }
    if app.shutdown_phase == ShutdownPhase::Done {
        let text = Line::from(Span::styled(
            " โœ“ all processes stopped",
            Style::default().fg(Color::Green),
        ));
        frame.render_widget(Paragraph::new(text), area);
        return;
    }
    let text = if app.search_input_active {
        // Search input mode โ€” show the query box.
        Line::from(vec![
            Span::styled(" /", Style::default().fg(Color::Yellow).add_modifier(Modifier::BOLD)),
            Span::raw(" "),
            Span::styled(
                app.search_query.clone(),
                Style::default().fg(Color::White).add_modifier(Modifier::BOLD),
            ),
            Span::styled("โ–ˆ", Style::default().fg(Color::Yellow)), // cursor
            Span::styled(
                "  [Enter/Esc] lock filter  [Backspace] delete",
                Style::default().fg(Color::DarkGray),
            ),
        ])
    } else if !app.search_query.is_empty() {
        // Search filter active.
        Line::from(vec![
            Span::styled(" filter: ", Style::default().fg(Color::Yellow)),
            Span::styled(
                app.search_query.clone(),
                Style::default().fg(Color::White).add_modifier(Modifier::BOLD),
            ),
            Span::styled("  [/]", Style::default().fg(Color::Cyan)),
            Span::raw(" edit  "),
            Span::styled("[Esc]", Style::default().fg(Color::Cyan)),
            Span::raw(" clear  "),
            Span::styled("[โ†‘โ†“]", Style::default().fg(Color::Cyan)),
            Span::raw(" scroll  "),
            Span::styled("[q]", Style::default().fg(Color::Cyan)),
            Span::raw(" quit "),
        ])
    } else {
        // Normal mode.
        Line::from(vec![
            Span::styled(" [tab]", Style::default().fg(Color::Cyan)),
            Span::raw(" pane  "),
            Span::styled("[/]", Style::default().fg(Color::Cyan)),
            Span::raw(" search  "),
            Span::styled("[i]", Style::default().fg(Color::Cyan)),
            Span::raw(" inst  "),
            Span::styled("[r]", Style::default().fg(Color::Cyan)),
            Span::raw(" restart  "),
            Span::styled("[b]", Style::default().fg(Color::Cyan)),
            Span::raw(" build  "),
            Span::styled("[c]", Style::default().fg(Color::Cyan)),
            Span::raw(" clear  "),
            Span::styled("[โ†‘โ†“]", Style::default().fg(Color::Cyan)),
            Span::raw(" scroll  "),
            Span::styled("[โ‡งโ†‘โ†“]", Style::default().fg(Color::Cyan)),
            Span::raw(" page  "),
            Span::styled("[g/G]", Style::default().fg(Color::Cyan)),
            Span::raw(" top/btm  "),
            Span::styled("[q]", Style::default().fg(Color::Cyan)),
            Span::raw(" quit  "),
            Span::styled("[drag]", Style::default().fg(Color::Cyan)),
            Span::raw(" copy "),
        ])
    };
    frame.render_widget(Paragraph::new(text), area);
}