alman 0.1.2

A command-line tool and TUI for managing shell aliases with intelligent suggestions
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
use crate::tui::app::{App, AppMode};
use ratatui::{
    Frame,
    layout::{Constraint, Direction, Layout, Alignment},
    style::{Color, Style},
    text::{Line, Span},
    widgets::{Block, Borders, List, ListItem, Paragraph},
};

pub fn render(f: &mut Frame, app: &App, area: ratatui::layout::Rect) {
    match app.mode {
        AppMode::AddAliasStep1 => render_add_alias_step1(f, app, area),
        AppMode::AddAliasStep2 => render_add_alias_step2(f, app, area),
        AppMode::AddAliasConfirmation => render_add_alias_confirmation(f, app, area),
        AppMode::RemoveAliasStep1 => render_remove_alias_step1(f, app, area),
        AppMode::RemoveAliasConfirmation => render_remove_alias_confirmation(f, app, area),
        AppMode::ChangeAliasStep1 => render_change_alias_step1(f, app, area),
        AppMode::ChangeAliasStep2 => render_change_alias_step2(f, app, area),
        AppMode::ListAliases => render_list_aliases(f, app, area),

        _ => render_default_input(f, app, area),
    }
}

fn render_add_alias_step1(f: &mut Frame, app: &App, area: ratatui::layout::Rect) {
    let chunks = Layout::default()
        .direction(Direction::Vertical)
        .constraints([
            Constraint::Length(3), // Command input
            Constraint::Length(1), // Spacer
            Constraint::Min(0),    // Command list
        ])
        .split(area);

    // Command input field
    let input = Paragraph::new(app.input.as_str())
        .style(Style::default().fg(Color::Yellow))
        .block(
            Block::default()
                .borders(Borders::ALL)
                .title("Command to create alias for (type to filter, ↑↓ to navigate)"),
        );
    f.render_widget(input, chunks[0]);

    // Set cursor position for command input
    f.set_cursor_position((
        chunks[0].x + app.cursor_position as u16 + 1,
        chunks[0].y + 1,
    ));

    // Command list
    if !app.filtered_commands.is_empty() {
        let commands: Vec<ListItem> = app
            .filtered_commands
            .iter()
            .map(|cmd| ListItem::new(
                ratatui::text::Span::styled(cmd.command_text.as_str(), ratatui::style::Style::default().fg(ratatui::style::Color::Blue))
            ))
            .collect();

        let commands_list = List::new(commands)
            .block(Block::default().borders(Borders::ALL).title("Top Commands"))
            .highlight_style(Style::default().bg(Color::DarkGray))
            .highlight_symbol(">> ");

        f.render_stateful_widget(commands_list, chunks[2], &mut app.list_state.clone());
    }
}

fn render_add_alias_step2(f: &mut Frame, app: &App, area: ratatui::layout::Rect) {
    let chunks = Layout::default()
        .direction(Direction::Vertical)
        .constraints([
            Constraint::Length(3), // Selected command display
            Constraint::Length(3), // Alias input
            Constraint::Length(1), // Spacer
            Constraint::Min(0),    // Alias suggestions
        ])
        .split(area);

    // Display selected command
    let selected_cmd = app.selected_command.as_deref().unwrap_or("No command selected");
    let command_display = Paragraph::new(format!("Selected command: {}", selected_cmd))
        .style(Style::default().fg(Color::Green))
        .block(Block::default().borders(Borders::ALL).title("Selected Command"));
    f.render_widget(command_display, chunks[0]);

    // Alias input field
    let alias_input = Paragraph::new(app.alias_input.as_str())
        .style(Style::default().fg(Color::Yellow))
        .block(
            Block::default()
                .borders(Borders::ALL)
                .title("Enter alias name (use ↑↓ to navigate suggestions, Tab for first suggestion)"),
        );
    f.render_widget(alias_input, chunks[1]);

    // Set cursor position for alias input
    f.set_cursor_position((
        chunks[1].x + app.alias_cursor_position as u16 + 1,
        chunks[1].y + 1,
    ));

    // Alias suggestions list
    if !app.alias_suggestions.is_empty() {
        let suggestions: Vec<ListItem> = app
            .alias_suggestions
            .iter()
            .map(|suggestion| {
                use ratatui::text::{Span, Line};
                ListItem::new(Line::from(vec![
                    Span::styled(&suggestion.alias, ratatui::style::Style::default().fg(ratatui::style::Color::Magenta)),
                    Span::raw(" = "),
                    Span::styled(&suggestion.command, ratatui::style::Style::default().fg(ratatui::style::Color::Blue)),
                    Span::raw(" ("),
                    Span::styled(&suggestion.reason, ratatui::style::Style::default().fg(ratatui::style::Color::Green)),
                    Span::raw(")"),
                ]))
            })
            .collect();

        let suggestions_list = List::new(suggestions)
            .block(Block::default().borders(Borders::ALL).title("Alias Suggestions"))
            .highlight_style(Style::default().bg(Color::DarkGray))
            .highlight_symbol(">> ");

        f.render_stateful_widget(suggestions_list, chunks[3], &mut app.alias_suggestions_state.clone());
    }
}

fn render_add_alias_confirmation(f: &mut Frame, app: &App, area: ratatui::layout::Rect) {
    let chunks = Layout::default()
        .direction(Direction::Vertical)
        .constraints([
            Constraint::Length(3), // Message
            Constraint::Length(1), // Spacer
            Constraint::Length(3), // Buttons
        ])
        .split(area);

    // Confirmation message
    let alias = app.confirmation_alias.as_deref().unwrap_or("unknown");
    let command = app.confirmation_command.as_deref().unwrap_or("unknown");
    let message = format!("Alias added: {} = {}", alias, command);
    let confirmation_message = Paragraph::new(message)
        .style(Style::default().fg(Color::Green))
        .block(Block::default().borders(Borders::ALL).title("Alias Added"))
        .alignment(Alignment::Center);
    f.render_widget(confirmation_message, chunks[0]);

    // Buttons
    let ok_text = if app.confirmation_selection {
        Span::styled(" [OK] ", Style::default().fg(Color::White).bg(Color::Blue))
    } else {
        Span::styled(" [OK] ", Style::default().fg(Color::Blue))
    };
    
    let undo_text = if !app.confirmation_selection {
        Span::styled(" [Undo] ", Style::default().fg(Color::White).bg(Color::Red))
    } else {
        Span::styled(" [Undo] ", Style::default().fg(Color::Red))
    };

    let buttons = Paragraph::new(Line::from(vec![ok_text, undo_text]))
        .block(Block::default().borders(Borders::ALL).title("Confirm"))
        .alignment(Alignment::Center);
    f.render_widget(buttons, chunks[2]);
}

fn render_remove_alias_step1(f: &mut Frame, app: &App, area: ratatui::layout::Rect) {
    let chunks = Layout::default()
        .direction(Direction::Vertical)
        .constraints([
            Constraint::Length(3), // Alias input
            Constraint::Length(1), // Spacer
            Constraint::Min(0),    // Alias list
        ])
        .split(area);

    // Alias input field
    let input = Paragraph::new(app.input.as_str())
        .style(Style::default().fg(Color::Yellow))
        .block(
            Block::default()
                .borders(Borders::ALL)
                .title("Filter aliases to remove (type to filter, ↑↓ to navigate)"),
        );
    f.render_widget(input, chunks[0]);

    // Set cursor position for alias input
    f.set_cursor_position((
        chunks[0].x + app.cursor_position as u16 + 1,
        chunks[0].y + 1,
    ));

    // Alias list
    if !app.filtered_aliases.is_empty() {
        let aliases: Vec<ListItem> = app
            .filtered_aliases
            .iter()
            .map(|(alias, command)| {
                ListItem::new(Line::from(vec![
                    Span::styled(
                        format!("{} = ", alias),
                        Style::default().fg(Color::Magenta),
                    ),
                    Span::styled(
                        command,
                        Style::default().fg(Color::Blue),
                    ),
                ]))
            })
            .collect();

        let aliases_list = List::new(aliases)
            .block(Block::default().borders(Borders::ALL).title("Aliases"))
            .highlight_style(Style::default().bg(Color::DarkGray))
            .highlight_symbol(">> ");

        f.render_stateful_widget(aliases_list, chunks[2], &mut app.list_state.clone());
    }
}

fn render_remove_alias_confirmation(f: &mut Frame, app: &App, area: ratatui::layout::Rect) {
    let chunks = Layout::default()
        .direction(Direction::Vertical)
        .constraints([
            Constraint::Length(3), // Message
            Constraint::Length(1), // Spacer
            Constraint::Length(3), // Buttons
        ])
        .split(area);

    // Confirmation message
    let alias = app.remove_confirmation_alias.as_deref().unwrap_or("unknown");
    let command = app.remove_confirmation_command.as_deref().unwrap_or("");
    let message = if command.is_empty() {
        format!("Remove alias: {}", alias)
    } else {
        format!("Remove alias: {} = {}", alias, command)
    };
    let confirmation_message = Paragraph::new(message)
        .style(Style::default().fg(Color::Red))
        .block(Block::default().borders(Borders::ALL).title("Remove Alias"))
        .alignment(Alignment::Center);
    f.render_widget(confirmation_message, chunks[0]);

    // Buttons
    let ok_text = if app.remove_confirmation_selection {
        Span::styled(" [OK] ", Style::default().fg(Color::White).bg(Color::Blue))
    } else {
        Span::styled(" [OK] ", Style::default().fg(Color::Blue))
    };
    
    let undo_text = if !app.remove_confirmation_selection {
        Span::styled(" [Undo] ", Style::default().fg(Color::White).bg(Color::Red))
    } else {
        Span::styled(" [Undo] ", Style::default().fg(Color::Red))
    };

    let buttons = Paragraph::new(Line::from(vec![ok_text, undo_text]))
        .block(Block::default().borders(Borders::ALL).title("Confirm"))
        .alignment(Alignment::Center);
    f.render_widget(buttons, chunks[2]);
}

fn render_change_alias_step1(f: &mut Frame, app: &App, area: ratatui::layout::Rect) {
    let chunks = Layout::default()
        .direction(Direction::Vertical)
        .constraints([
            Constraint::Length(3), // Alias input
            Constraint::Length(1), // Spacer
            Constraint::Min(0),    // Alias list
        ])
        .split(area);

    // Alias input field
    let input = Paragraph::new(app.input.as_str())
        .style(Style::default().fg(Color::Yellow))
        .block(
            Block::default()
                .borders(Borders::ALL)
                .title("Filter aliases to change (type to filter, ↑↓ to navigate)"),
        );
    f.render_widget(input, chunks[0]);

    // Set cursor position for alias input
    f.set_cursor_position((
        chunks[0].x + app.cursor_position as u16 + 1,
        chunks[0].y + 1,
    ));

    // Alias list
    if !app.filtered_aliases.is_empty() {
        let aliases: Vec<ListItem> = app
            .filtered_aliases
            .iter()
            .map(|(alias, command)| {
                ListItem::new(Line::from(vec![
                    Span::styled(
                        format!("{} = ", alias),
                        Style::default().fg(Color::Magenta),
                    ),
                    Span::styled(
                        command,
                        Style::default().fg(Color::Blue),
                    ),
                ]))
            })
            .collect();

        let aliases_list = List::new(aliases)
            .block(Block::default().borders(Borders::ALL).title("Aliases"))
            .highlight_style(Style::default().bg(Color::DarkGray))
            .highlight_symbol(">> ");

        f.render_stateful_widget(aliases_list, chunks[2], &mut app.list_state.clone());
    }
}

fn render_change_alias_step2(f: &mut Frame, app: &App, area: ratatui::layout::Rect) {
    let chunks = Layout::default()
        .direction(Direction::Vertical)
        .constraints([
            Constraint::Length(3), // Selected alias display
            Constraint::Length(3), // New alias input
            Constraint::Length(1), // Spacer
            Constraint::Min(0),    // Alias suggestions
        ])
        .split(area);

    // Display selected alias
    let old_alias = app.change_old_alias.as_deref().unwrap_or("No alias selected");
    let old_command = app.change_old_command.as_deref().unwrap_or("");
    let alias_display = if old_command.is_empty() {
        format!("Selected alias: {}", old_alias)
    } else {
        format!("Selected alias: {} = {}", old_alias, old_command)
    };
    let alias_display_widget = Paragraph::new(alias_display)
        .style(Style::default().fg(Color::Green))
        .block(Block::default().borders(Borders::ALL).title("Selected Alias"));
    f.render_widget(alias_display_widget, chunks[0]);

    // New alias input field
    let new_alias_input = Paragraph::new(app.change_new_alias.as_str())
        .style(Style::default().fg(Color::Yellow))
        .block(
            Block::default()
                .borders(Borders::ALL)
                .title("Enter new alias name (use ↑↓ to navigate suggestions, Tab for first suggestion)"),
        );
    f.render_widget(new_alias_input, chunks[1]);

    // Set cursor position for new alias input
    f.set_cursor_position((
        chunks[1].x + app.change_new_alias_cursor_position as u16 + 1,
        chunks[1].y + 1,
    ));

    // Alias suggestions list (show all)
    if !app.change_alias_suggestions.is_empty() {
        let suggestions: Vec<ListItem> = app
            .change_alias_suggestions
            .iter()
            .map(|suggestion| {
                use ratatui::text::{Span, Line};
                ListItem::new(Line::from(vec![
                    Span::styled(&suggestion.alias, ratatui::style::Style::default().fg(ratatui::style::Color::Magenta)),
                    Span::raw(" = "),
                    Span::styled(&suggestion.command, ratatui::style::Style::default().fg(ratatui::style::Color::Blue)),
                    Span::raw(" ("),
                    Span::styled(&suggestion.reason, ratatui::style::Style::default().fg(ratatui::style::Color::Green)),
                    Span::raw(")"),
                ]))
            })
            .collect();

        let mut state = app.change_alias_suggestions_state.clone();
        f.render_stateful_widget(
            List::new(suggestions)
                .block(Block::default().borders(Borders::ALL).title("Alias Suggestions (all)"))
                .highlight_style(Style::default().bg(Color::DarkGray))
                .highlight_symbol(">> "),
            chunks[3],
            &mut state,
        );
    }
}

fn render_default_input(f: &mut Frame, app: &App, area: ratatui::layout::Rect) {
    let chunks = Layout::default()
        .direction(Direction::Vertical)
        .constraints([Constraint::Length(3), Constraint::Min(0)])
        .split(area);

    let input = Paragraph::new(app.input.as_str())
        .style(Style::default().fg(Color::Yellow))
        .block(
            Block::default()
                .borders(Borders::ALL)
                .title(format!("{:?}", app.mode)),
        );
    f.render_widget(input, chunks[0]);

    f.set_cursor_position((
        chunks[0].x + app.cursor_position as u16 + 1,
        chunks[0].y + 1,
    ));

    if !app.filtered_commands.is_empty() {
        let commands: Vec<ListItem> = app
            .filtered_commands
            .iter()
            .map(|cmd| ListItem::new(
                ratatui::text::Span::styled(cmd.command_text.as_str(), ratatui::style::Style::default().fg(ratatui::style::Color::Blue))
            ))
            .collect();

        let commands_list = List::new(commands)
            .block(Block::default().borders(Borders::ALL).title("Matching Commands"))
            .highlight_style(Style::default().bg(Color::DarkGray))
            .highlight_symbol(">> ");

        f.render_widget(commands_list, chunks[1]);
    }
}

fn render_list_aliases(f: &mut Frame, app: &App, area: ratatui::layout::Rect) {
    let chunks = Layout::default()
        .direction(Direction::Vertical)
        .constraints([
            Constraint::Length(3), // Header
            Constraint::Min(0),    // Alias list
            Constraint::Length(3), // Controls
        ])
        .split(area);

    // Header
    let header = Paragraph::new("Aliases")
        .style(Style::default().fg(Color::Cyan))
        .block(Block::default().borders(Borders::ALL).title("Alias List"));
    f.render_widget(header, chunks[0]);

    // Alias list
    if !app.aliases.is_empty() {
        let aliases: Vec<ListItem> = app
            .aliases
            .iter()
            .map(|(alias, command)| {
                ListItem::new(Line::from(vec![
                    Span::styled(
                        format!("{} = ", alias),
                        Style::default().fg(Color::Magenta),
                    ),
                    Span::styled(
                        command,
                        Style::default().fg(Color::Blue),
                    ),
                ]))
            })
            .collect();

        let aliases_list = List::new(aliases)
            .block(Block::default().borders(Borders::ALL).title("Aliases"))
            .highlight_style(Style::default().bg(Color::DarkGray))
            .highlight_symbol(">> ");

        f.render_stateful_widget(aliases_list, chunks[1], &mut app.list_aliases_state.clone());
    } else {
        let empty_message = Paragraph::new("No aliases found")
            .style(Style::default().fg(Color::Yellow))
            .block(Block::default().borders(Borders::ALL).title("Aliases"));
        f.render_widget(empty_message, chunks[1]);
    }

    // Controls
    let controls = Paragraph::new("↑/↓ - Navigate | Enter - Select | Esc - Back to main menu")
        .style(Style::default().fg(Color::Green))
        .block(Block::default().borders(Borders::ALL).title("Controls"));
    f.render_widget(controls, chunks[2]);
}