summaryrefslogtreecommitdiffstats
path: root/src/canvas/dialogs/help_dialog.rs
blob: 70492ae904f13ded780061d5f9253c1248b09b61 (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
use std::cmp::max;
use unicode_width::UnicodeWidthStr;

use tui::{
    backend::Backend,
    layout::{Alignment, Rect},
    terminal::Frame,
    widgets::{Block, Borders, Paragraph},
};

use crate::{app::App, canvas::Painter, constants};

const HELP_BASE: &str = " Help ── Esc to close ";

pub trait HelpDialog {
    fn draw_help_dialog<B: Backend>(
        &self, f: &mut Frame<'_, B>, app_state: &mut App, draw_loc: Rect,
    );
}

impl HelpDialog for Painter {
    fn draw_help_dialog<B: Backend>(
        &self, f: &mut Frame<'_, B>, app_state: &mut App, draw_loc: Rect,
    ) {
        let repeat_num = max(
            0,
            draw_loc.width as i32 - HELP_BASE.chars().count() as i32 - 2,
        );
        let help_title = format!(" Help ─{}─ Esc to close ", "─".repeat(repeat_num as usize));

        if app_state.is_force_redraw {
            // We must also recalculate how many lines are wrapping to properly get scrolling to work on
            // small terminal sizes... oh joy.

            // TODO: Make this more automated and easier to add.

            let mut overflow_buffer = 0;
            let paragraph_width = draw_loc.width - 2;
            constants::HELP_CONTENTS_TEXT.iter().for_each(|text_line| {
                overflow_buffer +=
                    UnicodeWidthStr::width(*text_line).saturating_sub(1) as u16 / paragraph_width;
            });

            // General
            app_state.help_dialog_state.general_index =
                constants::HELP_CONTENTS_TEXT.len() as u16 + 1 + overflow_buffer;
            constants::GENERAL_HELP_TEXT.iter().for_each(|text_line| {
                overflow_buffer +=
                    UnicodeWidthStr::width(*text_line).saturating_sub(1) as u16 / paragraph_width;
            });

            // CPU
            app_state.help_dialog_state.cpu_index =
                (constants::HELP_CONTENTS_TEXT.len() + constants::GENERAL_HELP_TEXT.len()) as u16
                    + 2
                    + overflow_buffer;
            constants::CPU_HELP_TEXT.iter().for_each(|text_line| {
                overflow_buffer +=
                    UnicodeWidthStr::width(*text_line).saturating_sub(1) as u16 / paragraph_width;
            });

            // Processes
            app_state.help_dialog_state.process_index = (constants::HELP_CONTENTS_TEXT.len()
                + constants::GENERAL_HELP_TEXT.len()
                + constants::CPU_HELP_TEXT.len())
                as u16
                + 3
                + overflow_buffer;
            constants::PROCESS_HELP_TEXT.iter().for_each(|text_line| {
                overflow_buffer +=
                    UnicodeWidthStr::width(*text_line).saturating_sub(1) as u16 / paragraph_width;
            });

            // Search
            app_state.help_dialog_state.search_index = (constants::HELP_CONTENTS_TEXT.len()
                + constants::GENERAL_HELP_TEXT.len()
                + constants::CPU_HELP_TEXT.len()
                + constants::PROCESS_HELP_TEXT.len())
                as u16
                + 4
                + overflow_buffer;
            constants::SEARCH_HELP_TEXT.iter().for_each(|text_line| {
                overflow_buffer +=
                    UnicodeWidthStr::width(*text_line).saturating_sub(1) as u16 / paragraph_width;
            });

            // Battery
            app_state.help_dialog_state.battery_index = (constants::HELP_CONTENTS_TEXT.len()
                + constants::GENERAL_HELP_TEXT.len()
                + constants::CPU_HELP_TEXT.len()
                + constants::PROCESS_HELP_TEXT.len()
                + constants::SEARCH_HELP_TEXT.len())
                as u16
                + 5
                + overflow_buffer;
            constants::BATTERY_HELP_TEXT.iter().for_each(|text_line| {
                overflow_buffer +=
                    UnicodeWidthStr::width(*text_line).saturating_sub(1) as u16 / paragraph_width;
            });

            app_state.help_dialog_state.scroll_state.max_scroll_index =
                (self.styled_help_text.len() as u16
                    + (constants::NUM_CATEGORIES - 3)
                    + overflow_buffer)
                    .saturating_sub(draw_loc.height);

            // Fix if over-scrolled
            if app_state
                .help_dialog_state
                .scroll_state
                .current_scroll_index
                >= app_state.help_dialog_state.scroll_state.max_scroll_index
            {
                app_state
                    .help_dialog_state
                    .scroll_state
                    .current_scroll_index = app_state
                    .help_dialog_state
                    .scroll_state
                    .max_scroll_index
                    .saturating_sub(1);
            }
        }

        f.render_widget(
            Paragraph::new(self.styled_help_text.iter())
                .block(
                    Block::default()
                        .title(&help_title)
                        .title_style(self.colours.border_style)
                        .style(self.colours.border_style)
                        .borders(Borders::ALL)
                        .border_style(self.colours.border_style),
                )
                .style(self.colours.text_style)
                .alignment(Alignment::Left)
                .wrap(true)
                .scroll(
                    app_state
                        .help_dialog_state
                        .scroll_state
                        .current_scroll_index,
                ),
            draw_loc,
        );
    }
}