abalone 1.0.1

A full-screen interactive shell for CLI apps: header, input box with a command menu, status bar, help and settings panels, plus a self-updater and installer
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
//! The `/help` panel: tabs of text and a browsable list of the commands,
//! drawn in place of the input box until Esc.
//!
//! ```
//! use abalone::{Help, HelpTab};
//! let help = Help::new()
//!     .text("General", "My app does things.\n\n# Getting started\n  /init sets it up.")
//!     .commands("Commands")
//!     .link("https://example.com/my-app")
//!     .issues("https://example.com/my-app/issues");
//! assert_eq!(help.tabs.len(), 2);
//! ```

use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};

use crate::editor::CommandInfo;
use crate::style::{Row, Style, wrap};
use crate::theme::theme;
use crate::widgets::{accent_rule, title_with_tabs};

/// The keys the shell itself handles, added to the end of the first text tab.
pub const SHELL_KEYS: &str = "# Keys
  Enter            run the line, or the highlighted command
  Tab, →           complete the highlighted suggestion
  ↑ ↓              move in the menu; otherwise walk history
  Esc              hide the menu, or close a panel
  PgUp PgDn        scroll the output
  Ctrl+L           clear the screen
  Ctrl+C           clear the line; twice on an empty line quits";

/// One line of a text tab; long ones wrap.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct HelpLine {
    pub bold: bool,
    pub text: String,
}

/// Lines from text: `# Title` is a bold heading, everything else as written.
pub fn help_lines(text: &str) -> Vec<HelpLine> {
    text.lines()
        .map(|l| match l.strip_prefix("# ") {
            Some(h) => HelpLine {
                bold: true,
                text: h.to_owned(),
            },
            None => HelpLine {
                bold: false,
                text: l.to_owned(),
            },
        })
        .collect()
}

pub enum HelpTab {
    /// Text that scrolls.
    Text { name: String, lines: Vec<HelpLine> },
    /// The commands, two rows each; Enter puts one in the input line.
    Commands { name: String },
    /// Rows of your own for a `w`-column screen; they scroll like text.
    Custom {
        name: String,
        rows: Box<dyn Fn(usize) -> Vec<Row>>,
    },
}

impl HelpTab {
    pub fn name(&self) -> &str {
        match self {
            HelpTab::Text { name, .. } | HelpTab::Commands { name } | HelpTab::Custom { name, .. } => name,
        }
    }
}

/// What the help panel shows.
pub struct Help {
    pub title: String,
    pub tabs: Vec<HelpTab>,
    /// Shown as "For more help: …".
    pub more: Option<String>,
    /// Shown in the hint as "something wrong? …".
    pub issues: Option<String>,
    /// Add [`SHELL_KEYS`] to the first text tab.
    pub keys: bool,
}

impl Default for Help {
    /// The shell's keys, then the commands.
    fn default() -> Self {
        Help::new().text("General", "").commands("Commands")
    }
}

impl Help {
    /// No tabs yet; add them with [`Help::text`], [`Help::commands`] and [`Help::tab`].
    pub fn new() -> Self {
        Help {
            title: "Help".into(),
            tabs: vec![],
            more: None,
            issues: None,
            keys: true,
        }
    }

    pub fn title(mut self, title: impl Into<String>) -> Self {
        self.title = title.into();
        self
    }

    /// A text tab; see [`help_lines`] for the format.
    pub fn text(mut self, name: impl Into<String>, text: &str) -> Self {
        let mut lines = help_lines(text);
        while lines.first().is_some_and(|l| l.text.is_empty()) {
            lines.remove(0);
        }
        self.tabs.push(HelpTab::Text {
            name: name.into(),
            lines,
        });
        self
    }

    pub fn commands(mut self, name: impl Into<String>) -> Self {
        self.tabs.push(HelpTab::Commands { name: name.into() });
        self
    }

    pub fn tab(mut self, tab: HelpTab) -> Self {
        self.tabs.push(tab);
        self
    }

    pub fn link(mut self, url: impl Into<String>) -> Self {
        self.more = Some(url.into());
        self
    }

    pub fn issues(mut self, url: impl Into<String>) -> Self {
        self.issues = Some(url.into());
        self
    }

    /// Leave the shell's keys out of the first text tab.
    pub fn without_keys(mut self) -> Self {
        self.keys = false;
        self
    }

    /// A tab's lines with the keys added where they go.
    fn lines(&self, tab: usize) -> Vec<HelpLine> {
        let HelpTab::Text { lines, .. } = &self.tabs[tab] else {
            return vec![];
        };
        let first_text = self.tabs.iter().position(|t| matches!(t, HelpTab::Text { .. }));
        let mut lines = lines.clone();
        if self.keys && first_text == Some(tab) {
            if !lines.is_empty() {
                lines.push(HelpLine {
                    bold: false,
                    text: String::new(),
                });
            }
            lines.extend(help_lines(SHELL_KEYS));
        }
        lines
    }
}

pub enum HelpAction {
    None,
    Close,
    /// Close and put this in the input line.
    Use(String),
}

#[derive(Default)]
pub struct HelpPanel {
    tab: usize,
    /// Highlighted command on a commands tab.
    selected: usize,
    /// First row shown on a text tab.
    scroll: usize,
}

impl HelpPanel {
    fn on_commands(&self, help: &Help) -> bool {
        matches!(help.tabs.get(self.tab), Some(HelpTab::Commands { .. }))
    }

    pub fn key(&mut self, key: KeyEvent, help: &Help, commands: &[CommandInfo]) -> HelpAction {
        let n = commands.len();
        let tabs = help.tabs.len().max(1);
        let list = self.on_commands(help);
        match key.code {
            KeyCode::Esc | KeyCode::Char('q') => return HelpAction::Close,
            KeyCode::Char('c') if key.modifiers.contains(KeyModifiers::CONTROL) => return HelpAction::Close,
            KeyCode::Left | KeyCode::BackTab => {
                self.tab = (self.tab + tabs - 1) % tabs;
                self.scroll = 0;
            }
            KeyCode::Right | KeyCode::Tab => {
                self.tab = (self.tab + 1) % tabs;
                self.scroll = 0;
            }
            KeyCode::Up if list => self.selected = self.selected.saturating_sub(1),
            KeyCode::Down if list => self.selected = (self.selected + 1).min(n.saturating_sub(1)),
            KeyCode::PageUp if list => self.selected = self.selected.saturating_sub(5),
            KeyCode::PageDown if list => self.selected = (self.selected + 5).min(n.saturating_sub(1)),
            KeyCode::Home if list => self.selected = 0,
            KeyCode::End if list => self.selected = n.saturating_sub(1),
            KeyCode::Up => self.scroll = self.scroll.saturating_sub(1),
            KeyCode::Down => self.scroll += 1,
            KeyCode::PageUp => self.scroll = self.scroll.saturating_sub(10),
            KeyCode::PageDown => self.scroll += 10,
            KeyCode::Enter if list => {
                if let Some(c) = commands.get(self.selected) {
                    return HelpAction::Use(format!("/{} ", c.name));
                }
            }
            _ => {}
        }
        HelpAction::None
    }

    /// The panel for a `w`-column screen: as tall as its tallest tab, at most `h` rows.
    pub fn rows(&mut self, help: &Help, commands: &[CommandInfo], w: usize, h: usize) -> Vec<Row> {
        let t = theme();
        let names: Vec<&str> = help.tabs.iter().map(HelpTab::name).collect();
        self.tab = self.tab.min(names.len().saturating_sub(1));
        let top: Vec<Row> = vec![
            accent_rule(w),
            vec![],
            title_with_tabs(&help.title, &names, self.tab),
            vec![],
        ];

        let mut bottom: Vec<Row> = vec![vec![]];
        if let Some(more) = &help.more {
            bottom.push(vec![
                (Style::fg(t.muted), "  For more help: ".into()),
                (Style::default(), more.clone()),
            ]);
            bottom.push(vec![]);
        }
        let keys = if self.on_commands(help) {
            "↑↓ move · Enter uses the command"
        } else {
            "↑↓ scroll"
        };
        let tabs = if names.len() > 1 { " · ←→ tabs" } else { "" };
        let issues = help
            .issues
            .as_ref()
            .map_or(String::new(), |u| format!(" · something wrong? {u}"));
        bottom.push(vec![(
            Style::fg(t.muted),
            format!("  {keys}{tabs} · Esc closes{issues}"),
        )]);

        // As tall as the tallest tab needs, so switching tabs keeps the height.
        let pages: Vec<Option<Vec<Row>>> = (0..help.tabs.len())
            .map(|i| match &help.tabs[i] {
                HelpTab::Text { .. } => Some(text_rows(&help.lines(i), w)),
                HelpTab::Custom { rows, .. } => Some(rows(w)),
                HelpTab::Commands { .. } => None,
            })
            .collect();
        let needed = pages
            .iter()
            .map(|p| p.as_ref().map_or(commands_height(commands.len()), Vec::len))
            .max()
            .unwrap_or(0);
        let body_h = needed.min(h.saturating_sub(top.len() + bottom.len())).max(1);
        let mut body = match pages.into_iter().nth(self.tab) {
            Some(Some(lines)) => self.scrolled(lines, body_h),
            Some(None) => self.commands(commands, body_h),
            None => vec![],
        };
        body.resize(body_h, vec![]);
        let mut rows = top;
        rows.extend(body);
        rows.extend(bottom);
        rows.truncate(h);
        rows
    }

    fn scrolled(&mut self, lines: Vec<Row>, h: usize) -> Vec<Row> {
        let max = lines.len().saturating_sub(h);
        self.scroll = self.scroll.min(max);
        let mut rows: Vec<Row> = lines.into_iter().skip(self.scroll).take(h).collect();
        if self.scroll < max
            && let Some(last) = rows.last_mut()
        {
            *last = vec![(Style::fg(theme().muted), format!("  ↓ {} more", max - self.scroll))];
        }
        rows
    }

    /// Two rows per command (name, then what it does), the highlighted one kept in view.
    fn commands(&mut self, commands: &[CommandInfo], h: usize) -> Vec<Row> {
        let t = theme();
        let n = commands.len();
        self.selected = self.selected.min(n.saturating_sub(1));
        // Too short for the heading and the "more" rows: the highlighted command alone.
        if commands_height(n) > h && h < 5 {
            let Some(c) = commands.get(self.selected) else {
                return vec![];
            };
            let mut rows = vec![
                vec![(Style::fg(t.accent), format!("  ❯ /{}", c.name))],
                vec![(Style::fg(t.muted), format!("    {}", c.about))],
            ];
            rows.truncate(h);
            return rows;
        }
        let mut rows: Vec<Row> = vec![vec![(Style::default(), "  Browse commands".into())]];
        // Everything, or as many as fit with a row for each "more" line.
        let fit = if commands_height(n) <= h {
            n
        } else {
            (h.saturating_sub(3) / 2).max(1)
        };
        let first = self
            .selected
            .saturating_sub(fit.saturating_sub(1))
            .min(n.saturating_sub(fit));
        if first > 0 {
            rows.push(vec![(Style::fg(t.muted), format!("    ↑ {first} more"))]);
        } else {
            rows.push(vec![]);
        }
        for (i, c) in commands.iter().enumerate().skip(first).take(fit) {
            let (lead, style) = if i == self.selected {
                ("  ❯ ", Style::fg(t.accent))
            } else {
                ("    ", Style::default())
            };
            rows.push(vec![(style, lead.into()), (style, format!("/{}", c.name))]);
            rows.push(vec![(Style::fg(t.muted), format!("    {}", c.about))]);
        }
        let below = n.saturating_sub(first + fit);
        if below > 0 {
            rows.push(vec![(Style::fg(t.muted), format!("    ↓ {below} more"))]);
        }
        rows
    }
}

/// A text tab wrapped to the screen.
fn text_rows(lines: &[HelpLine], w: usize) -> Vec<Row> {
    let text_w = w.saturating_sub(6).max(10);
    let mut rows = Vec::new();
    for l in lines {
        let style = if l.bold {
            Style::default().bold()
        } else {
            Style::default()
        };
        for piece in wrap(&l.text, text_w) {
            rows.push(vec![(style, format!("  {piece}"))]);
        }
    }
    rows
}

/// Rows a commands tab needs to list `n` commands: a title, the row above
/// the list, and two rows per command.
fn commands_height(n: usize) -> usize {
    2 + 2 * n
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::style::row_text;
    use crossterm::event::KeyEventKind;

    fn key(code: KeyCode) -> KeyEvent {
        KeyEvent {
            kind: KeyEventKind::Press,
            ..KeyEvent::new(code, KeyModifiers::NONE)
        }
    }

    fn commands(n: usize) -> Vec<CommandInfo> {
        (0..n)
            .map(|i| CommandInfo::new(format!("cmd{i}"), format!("does {i}")))
            .collect()
    }

    fn text(rows: &[Row]) -> String {
        rows.iter().map(row_text).collect::<Vec<_>>().join("\n")
    }

    fn help() -> Help {
        Help::new()
            .text("General", "What it does.\n\n# Section\n  more")
            .commands("Commands")
            .link("https://example.com")
            .issues("https://example.com/issues")
    }

    #[test]
    fn fits_its_content_and_the_screen() {
        // Few commands: as tall as the General text needs, on either tab, with no room to spare.
        let (help, c) = (help(), commands(3));
        let mut p = HelpPanel::default();
        let natural = p.rows(&help, &c, 80, 200).len();
        assert!(natural < 200);
        p.key(key(KeyCode::Right), &help, &c);
        assert_eq!(p.rows(&help, &c, 80, 200).len(), natural);
        // Many commands: capped to the screen, never taller.
        let c = commands(60);
        for h in [8, 14, 40] {
            assert!(p.rows(&help, &c, 80, h).len() <= h);
        }
        assert_eq!(p.rows(&help, &c, 80, 40).len(), 40);
    }

    #[test]
    fn keeps_the_selected_command_in_view_and_uses_it() {
        let (help, c) = (help(), commands(30));
        let mut p = HelpPanel::default();
        p.key(key(KeyCode::Tab), &help, &c);
        for _ in 0..20 {
            p.key(key(KeyCode::Down), &help, &c);
        }
        let shown = text(&p.rows(&help, &c, 80, 20));
        assert!(shown.contains("❯ /cmd20"));
        assert!(shown.contains("↑") && shown.contains("↓"));
        assert!(matches!(p.key(key(KeyCode::Enter), &help, &c), HelpAction::Use(s) if s == "/cmd20 "));
        assert!(matches!(p.key(key(KeyCode::Esc), &help, &c), HelpAction::Close));
    }

    #[test]
    fn the_first_text_tab_ends_with_the_keys() {
        let (help, c) = (help(), commands(1));
        let shown = text(&HelpPanel::default().rows(&help, &c, 80, 200));
        assert!(shown.contains("  What it does.") && shown.contains("  Section"));
        assert!(shown.contains("Ctrl+L") && shown.contains("For more help: https://example.com"));
        let shown = text(&HelpPanel::default().rows(&help.without_keys(), &c, 80, 200));
        assert!(!shown.contains("Ctrl+L"));
    }
}