summaryrefslogtreecommitdiffstats
path: root/src/ui/tui_backend.rs
blob: d59c61959d0a0b234c643741832d0a3ed22f8d20 (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
use termion::cursor::Goto;
use termion::event::Key;
use termion::input::TermRead;
use termion::raw::{IntoRawMode, RawTerminal};
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, SelectableList, Text, Widget};
use unicode_width::UnicodeWidthStr;

use crate::context::JoshutoContext;
// use crate::fs::JoshutoDirList;

pub struct TuiBackend {
    pub terminal: tui::Terminal<TermionBackend<AlternateScreen<RawTerminal<std::io::Stdout>>>>,
}

impl TuiBackend {
    pub fn new() -> std::io::Result<Self> {
        let stdout = std::io::stdout().into_raw_mode()?;
        let stdout = AlternateScreen::from(stdout);
        let backend = TermionBackend::new(stdout);
        let mut terminal = tui::Terminal::new(backend)?;
        Ok(Self { terminal })
    }

    pub fn render(&mut self, context: &JoshutoContext) {
        let curr_tab = context.curr_tab_ref();

        let curr_list = curr_tab.curr_list_ref();
        let parent_list = curr_tab.parent_list_ref();
        let child_list = curr_tab.child_list_ref();

        self.terminal.draw(|mut f| {
            let f_size = f.size();

            let constraints = match child_list {
                Some(_) => [
                    Constraint::Ratio(1, 6),
                    Constraint::Ratio(2, 6),
                    Constraint::Ratio(3, 6),
                ],
                None => [
                    Constraint::Ratio(1, 6),
                    Constraint::Ratio(5, 6),
                    Constraint::Ratio(0, 6),
                ],
            };
            let layout_rect = Layout::default()
                .direction(Direction::Horizontal)
                .constraints(constraints.as_ref())
                .split(f_size);

            if let Some(curr_list) = parent_list.as_ref() {
                let list_name = "parent_list";
                let list_style = Style::default().fg(tui::style::Color::LightBlue);
                let selected_style = Style::default()
                    .fg(tui::style::Color::LightBlue)
                    .modifier(tui::style::Modifier::REVERSED);

                SelectableList::default()
                    .block(Block::default().borders(Borders::ALL).title(list_name))
                    .items(&curr_list.contents)
                    .style(list_style)
                    .highlight_style(selected_style)
                    .select(curr_list.index)
                    .render(&mut f, layout_rect[0]);
            }

            if let Some(curr_list) = curr_list.as_ref() {
                let list_name = "curr_list";
                let list_style = Style::default().fg(tui::style::Color::LightBlue);
                let selected_style = Style::default()
                    .fg(tui::style::Color::LightBlue)
                    .modifier(tui::style::Modifier::REVERSED);

                SelectableList::default()
                    .block(Block::default().borders(Borders::ALL).title(list_name))
                    .items(&curr_list.contents)
                    .style(list_style)
                    .highlight_style(selected_style)
                    .select(curr_list.index)
                    .render(&mut f, layout_rect[1]);
            }

            if let Some(curr_list) = child_list.as_ref() {
                let list_name = "child_list";
                let list_style = Style::default().fg(tui::style::Color::LightBlue);
                let selected_style = Style::default()
                    .fg(tui::style::Color::LightBlue)
                    .modifier(tui::style::Modifier::REVERSED);

                SelectableList::default()
                    .block(Block::default().borders(Borders::ALL).title(list_name))
                    .items(&curr_list.contents)
                    .style(list_style)
                    .highlight_style(selected_style)
                    .select(curr_list.index)
                    .render(&mut f, layout_rect[2]);
            }
        });
    }
}

pub fn rerender(context: &mut JoshutoContext) {}