aboutsummaryrefslogtreecommitdiffstats
path: root/crates/atuin-ai/src/tui/components/select.rs
blob: 5abbe655eb203f9ac4b9f4302557eb88e1dfd18c (plain) (blame)
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
use std::sync::mpsc;

use crossterm::event::KeyCode;
use eye_declare::{Elements, EventResult, Hooks, Span, Text, View, component, element, props};
use ratatui::style::Style;
use typed_builder::TypedBuilder;

use crate::tui::events::AiTuiEvent;

type OnSelectFn = Box<dyn Fn(&SelectOption) -> Option<AiTuiEvent> + Send + Sync + 'static>;

#[derive(TypedBuilder)]
pub(crate) struct SelectOption {
    #[builder(setter(into))]
    pub label: String,
    #[builder(setter(into))]
    pub value: String,
    #[builder(default = Style::default())]
    pub label_style: Style,
    #[builder(default = Style::default().reversed())]
    pub selected_style: Style,
}

#[derive(Default)]
pub(crate) struct PermissionSelectorState {
    selected_option: usize,
    tx: Option<mpsc::Sender<AiTuiEvent>>,
}

#[props]
pub(crate) struct Select {
    pub options: Vec<SelectOption>,
    pub on_select: OnSelectFn,
}

#[component(props = Select, state = PermissionSelectorState)]
pub(crate) fn permission_selector(
    props: &Select,
    state: &PermissionSelectorState,
    hooks: &mut Hooks<Select, PermissionSelectorState>,
) -> Elements {
    hooks.use_focusable(true);
    hooks.use_autofocus();

    hooks.use_context::<mpsc::Sender<AiTuiEvent>>(|tx, _, state| {
        state.tx = tx.cloned();
    });

    hooks.use_event(move |event, props, state| {
        if !event.is_key_press() {
            return EventResult::Ignored;
        }

        if let crossterm::event::Event::Key(key) = event {
            if key.kind != crossterm::event::KeyEventKind::Press {
                return EventResult::Ignored;
            }

            match key.code {
                KeyCode::Up => {
                    state.selected_option =
                        (state.selected_option + props.options.len() - 1) % props.options.len();
                    return EventResult::Consumed;
                }
                KeyCode::Down => {
                    state.selected_option = (state.selected_option + 1) % props.options.len();
                    return EventResult::Consumed;
                }
                KeyCode::Enter => {
                    let option = &props.options[state.selected_option];
                    if let Some(event) = (props.on_select)(option)
                        && let Some(ref tx) = state.tx
                    {
                        let _ = tx.send(event);
                    }
                    return EventResult::Consumed;
                }
                _ => {}
            }
        }

        EventResult::Ignored
    });

    element!(
        View {
            #(for (index, option) in props.options.iter().enumerate() {
                Text { Span(text: &option.label, style: if index == state.selected_option {
                    option.selected_style
                } else {
                    option.label_style
                }) }
            })
        }
    )
}