Skip to main content

sandbox_quant/terminal/
completion.rs

1#[derive(Debug, Clone, PartialEq, Eq)]
2pub struct ShellCompletion {
3    pub value: String,
4    pub description: String,
5}
6
7pub fn format_completion_line(completions: &[ShellCompletion], selected: usize) -> String {
8    completions
9        .iter()
10        .enumerate()
11        .map(|(index, item)| {
12            if index == selected {
13                format!("[{}]", item.value)
14            } else {
15                item.value.clone()
16            }
17        })
18        .collect::<Vec<_>>()
19        .join("  ")
20}
21
22pub fn scroll_lines_needed(current_row: u16, terminal_height: u16, lines_needed: usize) -> usize {
23    if terminal_height == 0 {
24        return 0;
25    }
26
27    let last_row = terminal_height.saturating_sub(1) as usize;
28    let current_row = current_row as usize;
29    current_row
30        .saturating_add(lines_needed)
31        .saturating_sub(last_row)
32}
33
34pub fn next_completion_index(len: usize, current: usize) -> usize {
35    if len == 0 {
36        0
37    } else {
38        (current + 1) % len
39    }
40}
41
42pub fn previous_completion_index(len: usize, current: usize) -> usize {
43    if len == 0 {
44        0
45    } else if current == 0 {
46        len - 1
47    } else {
48        current - 1
49    }
50}