summaryrefslogtreecommitdiffstats
path: root/src/textfield.rs
blob: 5ee06ec6d5554d0f67682b71f49e37e649780374 (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
use std::io::{self, Write};

use rustyline::completion::{Candidate, Completer, FilenameCompleter, Pair};
use rustyline::line_buffer;

use termion::cursor::Goto;
use termion::event::Key;
use termion::input::TermRead;
use termion::raw::IntoRawMode;
use termion::screen::AlternateScreen;
use tui::backend::{Backend, TermionBackend};
use tui::layout::{Constraint, Direction, Layout, Rect};
use tui::style::{Color, Style};
use tui::widgets::{Block, Borders, List, Paragraph, Text, Widget};
use tui::Terminal;
use unicode_width::UnicodeWidthStr;

use crate::util::event::{Event, Events};
use crate::ui::TuiBackend;
use crate::window;

use crate::KEYMAP_T;

struct CompletionTracker {
    pub index: usize,
    pub pos: usize,
    pub original: String,
    pub candidates: Vec<Pair>,
}

impl CompletionTracker {
    pub fn new(pos: usize, candidates: Vec<Pair>, original: String) -> Self {
        CompletionTracker {
            index: 0,
            pos,
            original,
            candidates,
        }
    }
}

pub struct TextField<'a> {
    backend: &'a mut TuiBackend,
    events: &'a Events,
}

impl<'a> TextField<'a> {
    pub fn new(backend: &'a mut TuiBackend, events: &'a Events) -> Self {
        Self { backend, events }
    }
    /*
        Paragraph::new(paragraph_contents.iter())
            .wrap(true)
            .render(&mut f, Rect { x: 0, y: 0, height: 2, width: f_size.width});
    */

    pub fn readline(&mut self) -> Option<String> {
        let mut input_string = String::with_capacity(64);
        let events = self.events;
        loop {
            // Draw UI
            self.backend.terminal.draw(|mut f| {
                let f_size = f.size();
                Paragraph::new([Text::raw(&input_string)].iter())
                    .style(Style::default().fg(Color::Yellow))
                    .render(
                        &mut f,
                        Rect {
                            x: 0,
                            y: 0,
                            height: 2,
                            width: f_size.width,
                        },
                    );
            });

            write!(
                self.backend.terminal.backend_mut(),
                "{}",
                Goto(4 + input_string.width() as u16, 5)
            );
            io::stdout().flush().ok();

            // Handle input
            if let Ok(event) = events.next() {
                match event {
                    Event::Input(input) => match input {
                        Key::Char('\n') => {
                            break;
                        }
                        Key::Esc => {
                            return None;
                        }
                        Key::Backspace => {
                            input_string.pop();
                        }
                        Key::Char(c) => {
                            input_string.push(c);
                        }
                        _ => {}
                    },
                    _ => {}
                }
            }
        }
        eprintln!("You typed: {}", input_string);
        Some(input_string)
    }
}