summaryrefslogtreecommitdiffstats
path: root/src/ui/widgets/tui_prompt.rs
blob: b9848d0f530db4dd083f11fd835850978ca1c91e (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
use termion::event::Key;
use tui::layout::Rect;
use tui::style::{Color, Style};
use tui::widgets::{Paragraph, Text, Widget};

use crate::context::JoshutoContext;
use crate::ui::TuiBackend;
use crate::util::event::Event;

use super::TuiView;

pub struct TuiPrompt<'a> {
    prompt: &'a str,
}

impl<'a> TuiPrompt<'a> {
    pub fn new(prompt: &'a str) -> Self {
        Self { prompt }
    }

    pub fn get_key(&mut self, backend: &mut TuiBackend, context: &JoshutoContext) -> Key {
        let terminal = backend.terminal_mut();

        context.events.flush();
        loop {
            terminal.draw(|mut frame| {
                let f_size = frame.size();
                if f_size.height == 0 {
                    return;
                }

                {
                    let mut view = TuiView::new(&context);
                    view.show_bottom_status = false;
                    view.render(&mut frame, f_size);
                }

                let prompt_style = Style::default().fg(Color::LightYellow);

                let text = [Text::styled(self.prompt, prompt_style)];

                let textfield_rect = Rect {
                    x: 0,
                    y: f_size.height - 1,
                    width: f_size.width,
                    height: 1,
                };

                Paragraph::new(text.iter())
                    .wrap(true)
                    .render(&mut frame, textfield_rect);
            });

            if let Ok(event) = context.events.next() {
                match event {
                    Event::Input(key) => {
                        return key;
                    }
                    _ => {}
                };
            }
        }
    }
}