summaryrefslogtreecommitdiffstats
path: root/src/ui/widgets/tui_folder_view.rs
blob: 6c145ddc46907f38a02f0b4bb7ca939824df79cd (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
use tui::buffer::Buffer;
use tui::layout::{Direction, Layout, Rect};
use tui::style::{Color, Style};
use tui::text::Span;
use tui::widgets::{Paragraph, Widget, Wrap};

use super::{TuiDirList, TuiDirListDetailed, TuiFooter, TuiTabBar, TuiTopBar};
use crate::context::JoshutoContext;

const TAB_VIEW_WIDTH: u16 = 15;

pub struct TuiFolderView<'a> {
    pub context: &'a JoshutoContext,
    pub show_bottom_status: bool,
}

use super::super::{DEFAULT_LAYOUT, NO_PREVIEW_LAYOUT};

impl<'a> TuiFolderView<'a> {
    pub fn new(context: &'a JoshutoContext) -> Self {
        Self {
            context,
            show_bottom_status: true,
        }
    }
}

impl<'a> Widget for TuiFolderView<'a> {
    fn render(self, area: Rect, buf: &mut Buffer) {
        let f_size = area;

        let curr_tab = self.context.tab_context_ref().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();

        let constraints = match child_list {
            Some(_) => DEFAULT_LAYOUT,
            None => NO_PREVIEW_LAYOUT,
        };
        let layout_rect = Layout::default()
            .direction(Direction::Horizontal)
            .margin(1)
            .constraints(constraints.as_ref())
            .split(f_size);

        if self.context.tab_context_ref().len() > 1 {
            let topbar_width = if f_size.width > TAB_VIEW_WIDTH {
                f_size.width - TAB_VIEW_WIDTH
            } else {
                0
            };

            let rect = Rect {
                x: 0,
                y: 0,
                width: topbar_width,
                height: 1,
            };
            TuiTopBar::new(curr_tab.pwd()).render(rect, buf);

            let rect = Rect {
                x: topbar_width,
                y: 0,
                width: TAB_VIEW_WIDTH,
                height: 1,
            };
            let name = if let Some(ostr) = curr_tab.pwd().file_name() {
                ostr.to_str().unwrap_or("")
            } else {
                ""
            };
            TuiTabBar::new(
                name,
                self.context.tab_context_ref().get_index(),
                self.context.tab_context_ref().len(),
            )
            .render(rect, buf);
        } else {
            let topbar_width = f_size.width;

            let rect = Rect {
                x: 0,
                y: 0,
                width: topbar_width,
                height: 1,
            };
            TuiTopBar::new(curr_tab.pwd()).render(rect, buf);
        }

        if let Some(list) = parent_list.as_ref() {
            TuiDirList::new(&list).render(layout_rect[0], buf);
        };

        if let Some(list) = curr_list.as_ref() {
            TuiDirListDetailed::new(&list).render(layout_rect[1], buf);
            let rect = Rect {
                x: 0,
                y: f_size.height - 1,
                width: f_size.width,
                height: 1,
            };

            let message_style = Style::default().fg(Color::Yellow);

            if self.show_bottom_status {
                /* draw the bottom status bar */
                if let Some(msg) = self.context.worker_msg() {
                    let text = Span::styled(msg.as_str(), message_style);
                    Paragraph::new(text)
                        .wrap(Wrap { trim: true })
                        .render(rect, buf);
                } else if !self.context.message_queue_ref().is_empty() {
                    let text = Span::styled(&self.context.message_queue_ref()[0], message_style);
                    Paragraph::new(text)
                        .wrap(Wrap { trim: true })
                        .render(rect, buf);
                } else if let Some(entry) = list.get_curr_ref() {
                    TuiFooter::new(entry).render(rect, buf);
                }
            }
        };

        if let Some(list) = child_list.as_ref() {
            TuiDirList::new(&list).render(layout_rect[2], buf);
        };
    }
}