summaryrefslogtreecommitdiffstats
path: root/src/ui/widgets/tui_menu.rs
blob: 80a3d3ac53fad94774a54995b9acc2e549392636 (plain)
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
use std::iter::Iterator;

use termion::event::Key;
use tui::buffer::Buffer;
use tui::layout::Rect;
use tui::style::{Color, Style};
use tui::widgets::{Block, Borders, Clear, Widget};

use super::TuiView;
use crate::commands::{CommandKeybind, KeyCommand};
use crate::config::JoshutoCommandMapping;
use crate::context::JoshutoContext;
use crate::ui::TuiBackend;
use crate::util::event::Event;
use crate::util::worker;

const BORDER_HEIGHT: usize = 1;
const BOTTOM_MARGIN: usize = 1;

pub struct TuiCommandMenu;

trait ToString {
    fn to_string(&self) -> String;
}

impl ToString for Key {
    fn to_string(&self) -> String {
        match *self {
            Key::Char(c) => format!("{}", c),
            Key::Ctrl(c) => format!("ctrl+{}", c),
            Key::Left => format!("arrow_left"),
            Key::Right => format!("arrow_right"),
            Key::Up => format!("arrow_up"),
            Key::Down => format!("arrow_down"),
            Key::Backspace => format!("backspace"),
            Key::Home => format!("home"),
            Key::End => format!("end"),
            Key::PageUp => format!("page_up"),
            Key::PageDown => format!("page_down"),
            Key::BackTab => format!("backtab"),
            Key::Insert => format!("insert"),
            Key::Delete => format!("delete"),
            Key::Esc => format!("escape"),
            Key::F(i) => format!("f{}", i),
            k => format!("{:?}", k),
        }
    }
}

impl TuiCommandMenu {
    pub fn new() -> Self {
        Self {}
    }

    pub fn get_input<'a>(
        &mut self,
        backend: &mut TuiBackend,
        context: &mut JoshutoContext,
        m: &'a JoshutoCommandMapping,
    ) -> Option<&'a KeyCommand> {
        let mut map: &JoshutoCommandMapping = &m;
        let terminal = backend.terminal_mut();
        context.flush_event();

        loop {
            terminal.draw(|frame| {
                let f_size: Rect = frame.size();

                {
                    let view = TuiView::new(&context);
                    frame.render_widget(view, f_size);
                }

                {
                    // draw menu
                    let mut display_vec: Vec<String> = map
                        .as_ref()
                        .iter()
                        .map(|(k, v)| format!("  {}    {}", k.to_string(), v))
                        .collect();
                    display_vec.sort();
                    let display_str: Vec<&str> = display_vec.iter().map(|v| v.as_str()).collect();

                    let display_str_len = display_str.len();

                    let y = if (f_size.height as usize)
                        < display_str_len + BORDER_HEIGHT + BOTTOM_MARGIN
                    {
                        0
                    } else {
                        f_size.height
                            - (BORDER_HEIGHT + BOTTOM_MARGIN) as u16
                            - display_str_len as u16
                    };

                    let menu_rect = Rect {
                        x: 0,
                        y,
                        width: f_size.width,
                        height: (display_str_len + BORDER_HEIGHT) as u16,
                    };

                    frame.render_widget(Clear, menu_rect);
                    frame.render_widget(TuiMenu::new(&display_str), menu_rect);
                }
            });

            if let Ok(event) = context.poll_event() {
                match event {
                    Event::IOWorkerProgress(res) => {
                        worker::process_worker_progress(context, res);
                    }
                    Event::IOWorkerResult(res) => {
                        worker::process_finished_worker(context, res);
                    }
                    Event::Input(key) => {
                        match key {
                            Key::Esc => return None,
                            key => match map.as_ref().get(&key) {
                                Some(CommandKeybind::SimpleKeybind(s)) => {
                                    return Some(s);
                                }
                                Some(CommandKeybind::CompositeKeybind(m)) => {
                                    map = m;
                                }
                                None => return None,
                            },
                        }
                        context.flush_event();
                    }
                }
            }
        }
    }
}

pub struct TuiMenu<'a> {
    options: &'a [&'a str],
}

impl<'a> TuiMenu<'a> {
    pub fn new(options: &'a [&'a str]) -> Self {
        Self { options }
    }

    pub fn len(&self) -> usize {
        self.options.len()
    }
}

impl<'a> Widget for TuiMenu<'a> {
    fn render(self, area: Rect, buf: &mut Buffer) {
        let text_iter = self.options.iter().chain(&[" "]);
        let style = Style::default().fg(Color::Reset).bg(Color::Reset);
        let area_x = area.x + 1;
        let area_y = area.y + 1;

        Block::default()
            .style(style)
            .borders(Borders::TOP)
            .render(area, buf);

        for (i, text) in text_iter.enumerate() {
            buf.set_string(area_x, area_y + i as u16, text, style);
        }
    }
}