amoxide-tui 0.2.0

Interactive TUI for amoxide — manage aliases and profiles visually
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
use crate::model::*;
use ratatui::prelude::*;
use ratatui::widgets::Paragraph;
use ratatui::Frame;

// Noctavox-inspired warm color palette
const TEXT_PRIMARY: Color = Color::Rgb(210, 210, 213); // #d2d2d5
const TEXT_MUTED: Color = Color::Rgb(100, 100, 103); // #646467
const GOLD: Color = Color::Rgb(220, 220, 100); // #dcdc64
const HEADER_DEFAULT: Color = Color::Rgb(190, 185, 170); // warm beige for inactive headers
const TREE_CONNECTOR: Color = Color::Rgb(70, 70, 73); // dim connector lines
const TREE_CONNECTOR_ACTIVE: Color = Color::Rgb(150, 150, 80); // brighter connectors for cursor row
const SELECTED_ACCENT: Color = Color::Rgb(208, 136, 74); // #d0884a — warm orange for selected marker/connectors
const SELECTED_ACCENT_MUTED: Color = Color::Rgb(154, 101, 53); // #9a6535 — muted orange for selected commands
const SELECTED_TEXT: Color = Color::Rgb(232, 232, 234); // #e8e8ea — bright white for selected alias names

pub fn draw(frame: &mut Frame, model: &TuiModel) {
    let area = frame.area();

    let help = Paragraph::new(help_bar(&model.mode));

    let chunks = Layout::default()
        .direction(Direction::Vertical)
        .constraints([
            Constraint::Length(1),
            Constraint::Length(1),
            Constraint::Min(0),
        ])
        .split(area);

    frame.render_widget(help, chunks[0]);

    // Add 1-column padding on left and right
    let padded = Layout::default()
        .direction(Direction::Horizontal)
        .constraints([
            Constraint::Length(1),
            Constraint::Min(0),
            Constraint::Length(1),
        ])
        .split(chunks[2]);
    let content_area = padded[1];

    match &model.mode {
        Mode::Moving => {
            let columns = Layout::default()
                .direction(Direction::Horizontal)
                .constraints([Constraint::Percentage(55), Constraint::Percentage(45)])
                .split(content_area);

            render_left_column(frame, model, columns[0]);
            render_right_column(frame, model, columns[1]);
        }
        Mode::TextInput(state) => {
            render_left_column(frame, model, content_area);
            render_text_input(frame, state, content_area);
        }
        Mode::Confirm(action) => {
            render_left_column(frame, model, content_area);
            render_confirm(frame, action, content_area);
        }
        Mode::Normal => {
            render_left_column(frame, model, content_area);
        }
    }
}

fn render_left_column(frame: &mut Frame, model: &TuiModel, area: Rect) {
    let tree_lines = render_tree_lines(model);
    let visible_height = area.height as usize;
    let start = model.scroll_offset;
    let end = (start + visible_height).min(tree_lines.len());
    let visible: Vec<Line> = if start < tree_lines.len() {
        tree_lines[start..end].to_vec()
    } else {
        Vec::new()
    };

    let tree_widget = Paragraph::new(Text::from(visible));
    frame.render_widget(tree_widget, area);
}

/// Returns (icon, label) for a tree header node.
fn header_content(node: &TreeNode, activation_order: Option<usize>) -> (String, String) {
    match &node.kind {
        NodeKind::GlobalHeader => (ICON_GLOBAL.to_string(), "global".to_string()),
        NodeKind::ProjectHeader => (ICON_PROJECT.to_string(), "project (.aliases)".to_string()),
        NodeKind::ProfileHeader => {
            let icon = if node.is_active {
                ICON_ACTIVE
            } else {
                ICON_INACTIVE
            };
            let tag = match activation_order {
                Some(n) => format!(" (active: {n})"),
                None => String::new(),
            };
            (format!("{icon} "), format!("{}{tag}", node.label))
        }
        NodeKind::AliasItem => unreachable!(),
    }
}

/// Returns (label_color, icon_color) for a tree header node.
fn header_colors(node: &TreeNode, is_cursor: bool) -> (Color, Color) {
    let highlight = is_cursor || (node.kind == NodeKind::ProfileHeader && node.is_active);
    let label_color = if highlight { GOLD } else { HEADER_DEFAULT };
    let icon_color = match &node.kind {
        NodeKind::ProfileHeader if !highlight => TEXT_MUTED,
        _ => label_color,
    };
    (label_color, icon_color)
}

fn render_right_column(frame: &mut Frame, model: &TuiModel, area: Rect) {
    let mut lines: Vec<Line<'static>> = Vec::new();
    lines.push(Line::from(Span::styled(
        "-> Move to",
        Style::default().fg(HEADER_DEFAULT).bold(),
    )));
    lines.push(Line::from(""));

    for (i, node) in model.dest_tree.iter().enumerate() {
        if node.kind == NodeKind::AliasItem {
            continue;
        }
        let is_cursor = i == model.dest_cursor && model.active_column == Column::Right;
        let marker = if is_cursor {
            MARKER_CURSOR
        } else {
            MARKER_NONE
        };
        let conn = if is_cursor {
            TREE_CONNECTOR_ACTIVE
        } else {
            TREE_CONNECTOR
        };

        let (icon, label) =
            header_content(node, model.app_model.config.activation_order(&node.label));
        let (label_color, icon_color) = header_colors(node, is_cursor);

        lines.push(Line::from(vec![
            Span::styled(
                format!("{}{marker}", node.prefix),
                Style::default().fg(conn),
            ),
            Span::styled(icon, Style::default().fg(icon_color)),
            Span::styled(label, Style::default().fg(label_color).bold()),
        ]));
    }

    frame.render_widget(Paragraph::new(Text::from(lines)), area);
}

fn render_text_input(frame: &mut Frame, state: &TextInputState, area: Rect) {
    let input_area = Rect {
        x: area.x,
        y: area.y + area.height.saturating_sub(1),
        width: area.width,
        height: 1,
    };
    let prompt = match state {
        TextInputState::NewProfile(text) => Line::from(vec![
            Span::styled("  New profile: ", Style::default().fg(GOLD)),
            Span::styled(text.as_str(), Style::default().fg(TEXT_PRIMARY)),
            Span::styled("_", Style::default().fg(TEXT_PRIMARY)),
        ]),
        TextInputState::NewAlias {
            name,
            command,
            active_field,
            target,
        } => {
            let target_label = match target {
                AliasTarget::Global => "global",
                AliasTarget::Project => "project",
                AliasTarget::Profile(p) => p.as_str(),
            };
            let name_style = if *active_field == AliasField::Name {
                Style::default().fg(TEXT_PRIMARY)
            } else {
                Style::default().fg(TEXT_MUTED)
            };
            let cmd_style = if *active_field == AliasField::Command {
                Style::default().fg(TEXT_PRIMARY)
            } else {
                Style::default().fg(TEXT_MUTED)
            };
            let cursor_after_name = *active_field == AliasField::Name;
            let cursor_after_cmd = *active_field == AliasField::Command;
            Line::from(vec![
                Span::styled(format!("  [{target_label}] "), Style::default().fg(GOLD)),
                Span::styled(name.as_str(), name_style),
                if cursor_after_name {
                    Span::styled("_", Style::default().fg(TEXT_PRIMARY))
                } else {
                    Span::raw("")
                },
                Span::styled(" = ", Style::default().fg(TEXT_MUTED)),
                Span::styled(command.as_str(), cmd_style),
                if cursor_after_cmd {
                    Span::styled("_", Style::default().fg(TEXT_PRIMARY))
                } else {
                    Span::raw("")
                },
            ])
        }
    };
    frame.render_widget(ratatui::widgets::Clear, input_area);
    frame.render_widget(Paragraph::new(prompt), input_area);
}

fn render_confirm(frame: &mut Frame, action: &ConfirmAction, area: Rect) {
    let input_area = Rect {
        x: area.x,
        y: area.y + area.height.saturating_sub(1),
        width: area.width,
        height: 1,
    };
    let message = match action {
        ConfirmAction::DeleteProfile(name) => {
            format!("  Delete profile \"{name}\"? [y/n]")
        }
        ConfirmAction::OverwriteAliases {
            aliases,
            destination,
        } => {
            let count = aliases.len();
            let dest = match destination {
                MoveDestination::Global => "global".to_string(),
                MoveDestination::Project => "project".to_string(),
                MoveDestination::Profile(name) => format!("profile \"{name}\""),
            };
            format!("  Move {count} alias(es) to {dest}, overwriting duplicates? [y/n]")
        }
    };
    let widget = Paragraph::new(message).style(Style::default().fg(GOLD));
    frame.render_widget(ratatui::widgets::Clear, input_area);
    frame.render_widget(widget, input_area);
}

fn render_tree_lines(model: &TuiModel) -> Vec<Line<'static>> {
    let mut lines = Vec::new();

    for (i, node) in model.tree.iter().enumerate() {
        let is_cursor = i == model.cursor && model.active_column == Column::Left;
        let is_selected = node
            .alias_id
            .as_ref()
            .is_some_and(|id| model.selected.contains(id));

        match &node.kind {
            NodeKind::GlobalHeader | NodeKind::ProjectHeader | NodeKind::ProfileHeader => {
                let marker = if is_cursor {
                    MARKER_CURSOR
                } else {
                    MARKER_NONE
                };
                let conn = if is_cursor {
                    TREE_CONNECTOR_ACTIVE
                } else {
                    TREE_CONNECTOR
                };
                let (icon, label) =
                    header_content(node, model.app_model.config.activation_order(&node.label));
                let (label_color, icon_color) = header_colors(node, is_cursor);

                lines.push(Line::from(vec![
                    Span::styled(
                        format!("{}{marker}", node.prefix),
                        Style::default().fg(conn),
                    ),
                    Span::styled(icon, Style::default().fg(icon_color)),
                    Span::styled(label, Style::default().fg(label_color).bold()),
                ]));
            }
            NodeKind::AliasItem => {
                let is_last_alias = model
                    .tree
                    .get(i + 1)
                    .is_none_or(|next| next.kind != NodeKind::AliasItem);

                let arm = if is_last_alias {
                    TREE_LAST
                } else {
                    TREE_BRANCH
                };

                let marker = if is_cursor {
                    MARKER_CURSOR
                } else if is_selected {
                    MARKER_SELECTED
                } else {
                    MARKER_NONE
                };

                let conn = if is_cursor {
                    TREE_CONNECTOR_ACTIVE
                } else if is_selected {
                    SELECTED_ACCENT_MUTED
                } else {
                    TREE_CONNECTOR
                };
                let name_style = if is_cursor {
                    Style::default().fg(GOLD).bold()
                } else if is_selected {
                    Style::default().fg(SELECTED_TEXT).bold()
                } else {
                    Style::default().fg(TEXT_PRIMARY)
                };

                let marker_style = if is_selected {
                    Style::default().fg(SELECTED_ACCENT)
                } else {
                    Style::default().fg(conn)
                };

                // Single line: prefix arm marker name -> command
                let cmd_text = node.alias_command.as_deref().unwrap_or("");
                let cmd_style = if is_cursor {
                    Style::default().fg(HEADER_DEFAULT)
                } else if is_selected {
                    Style::default().fg(SELECTED_ACCENT_MUTED)
                } else {
                    Style::default().fg(TEXT_MUTED)
                };
                lines.push(Line::from(vec![
                    Span::styled(
                        format!("{}{arm}", node.content_prefix),
                        Style::default().fg(conn),
                    ),
                    Span::styled(marker.to_string(), marker_style),
                    Span::styled(node.label.clone(), name_style),
                    Span::styled(" -> ", Style::default().fg(TEXT_MUTED)),
                    Span::styled(cmd_text.to_string(), cmd_style),
                ]));

                // Breathing room between sections
                if is_last_alias {
                    let next_is_header = model.tree.get(i + 1).is_some_and(|n| {
                        matches!(
                            n.kind,
                            NodeKind::GlobalHeader
                                | NodeKind::ProjectHeader
                                | NodeKind::ProfileHeader
                        )
                    });
                    if next_is_header {
                        lines.push(Line::from(Span::styled(
                            node.content_prefix.clone(),
                            Style::default().fg(TREE_CONNECTOR),
                        )));
                    }
                }
            }
        }
    }

    lines
}

fn help_bar(mode: &Mode) -> Line<'static> {
    match mode {
        Mode::Normal => Line::from(vec![
            Span::raw("  "),
            Span::styled("q", Style::default().fg(GOLD)),
            Span::styled(" quit  ", Style::default().fg(TEXT_MUTED)),
            Span::styled("a", Style::default().fg(GOLD)),
            Span::styled(" add  ", Style::default().fg(TEXT_MUTED)),
            Span::styled("Space", Style::default().fg(GOLD)),
            Span::styled(" select  ", Style::default().fg(TEXT_MUTED)),
            Span::styled("m", Style::default().fg(GOLD)),
            Span::styled(" move  ", Style::default().fg(TEXT_MUTED)),
            Span::styled("n", Style::default().fg(GOLD)),
            Span::styled(" new profile  ", Style::default().fg(TEXT_MUTED)),
            Span::styled("x", Style::default().fg(GOLD)),
            Span::styled(" delete  ", Style::default().fg(TEXT_MUTED)),
            Span::styled("u", Style::default().fg(GOLD)),
            Span::styled(" use", Style::default().fg(TEXT_MUTED)),
        ]),
        Mode::Moving => Line::from(vec![
            Span::raw("  "),
            Span::styled("Esc", Style::default().fg(GOLD)),
            Span::styled(" cancel  ", Style::default().fg(TEXT_MUTED)),
            Span::styled("jk/↑↓", Style::default().fg(GOLD)),
            Span::styled(" navigate  ", Style::default().fg(TEXT_MUTED)),
            Span::styled("Enter", Style::default().fg(GOLD)),
            Span::styled(" move here  ", Style::default().fg(TEXT_MUTED)),
            Span::styled("Tab", Style::default().fg(GOLD)),
            Span::styled(" switch column", Style::default().fg(TEXT_MUTED)),
        ]),
        Mode::TextInput(TextInputState::NewProfile(_)) => Line::from(vec![
            Span::raw("  "),
            Span::styled("Esc", Style::default().fg(GOLD)),
            Span::styled(" cancel  ", Style::default().fg(TEXT_MUTED)),
            Span::styled("Enter", Style::default().fg(GOLD)),
            Span::styled(" confirm", Style::default().fg(TEXT_MUTED)),
        ]),
        Mode::TextInput(TextInputState::NewAlias { .. }) => Line::from(vec![
            Span::raw("  "),
            Span::styled("Tab", Style::default().fg(GOLD)),
            Span::styled(" switch field  ", Style::default().fg(TEXT_MUTED)),
            Span::styled("Esc", Style::default().fg(GOLD)),
            Span::styled(" cancel  ", Style::default().fg(TEXT_MUTED)),
            Span::styled("Enter", Style::default().fg(GOLD)),
            Span::styled(" confirm", Style::default().fg(TEXT_MUTED)),
        ]),
        Mode::Confirm(_) => Line::from(vec![
            Span::raw("  "),
            Span::styled("y", Style::default().fg(GOLD)),
            Span::styled(" confirm  ", Style::default().fg(TEXT_MUTED)),
            Span::styled("n", Style::default().fg(GOLD)),
            Span::styled(" cancel", Style::default().fg(TEXT_MUTED)),
        ]),
    }
}