leetcode_tui_core/
popup.rs

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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
use ratatui::widgets::{ListState, ScrollbarState};
use std::fmt::Display;

use crate::emit;

#[derive(Default)]
pub struct Popup {
    pub visible: bool,
    lines: Vec<String>,
    pub v_scroll_state: ScrollbarState,
    pub v_scroll: u16,
    title: Option<String>,
}

impl Popup {
    pub fn get_title(&self) -> Option<&str> {
        self.title.as_deref()
    }
}

impl Popup {
    pub fn new(lines: Vec<String>) -> Self {
        let mut p = Popup {
            lines,
            ..Default::default()
        };
        p.v_scroll_state = p.v_scroll_state.content_length(p.lines.len() as u16);
        p
    }

    pub fn toggle(&mut self) -> bool {
        self.visible = !self.visible;
        true
    }

    pub fn get_text(&self) -> &Vec<String> {
        &self.lines
    }

    pub fn reset(&mut self, title: Option<String>, lines: Vec<String>) {
        let mut p = Self::new(lines);
        p.visible = self.visible;
        p.title = title;
        *self = p;
    }

    pub fn get_lines(&self) -> &Vec<String> {
        &self.lines
    }

    pub fn scroll_down(&mut self) -> bool {
        if self.v_scroll == self.lines.len().saturating_sub(1) as u16 {
            return false;
        }
        self.v_scroll = self.v_scroll.saturating_add(1);
        self.v_scroll_state = self.v_scroll_state.position(self.v_scroll);
        true
    }

    pub fn scroll_up(&mut self) -> bool {
        if self.v_scroll == 0 {
            return false;
        }
        self.v_scroll = self.v_scroll.saturating_sub(1);
        self.v_scroll_state = self.v_scroll_state.position(self.v_scroll);
        true
    }
}

#[derive(Default)]
pub struct SelectPopup<T: Display> {
    pub visible: bool,
    pub state: ListState,
    items: Vec<T>,
    sender: Option<tokio::sync::oneshot::Sender<Option<usize>>>,
    title: Option<String>,
}

impl<T: Display> SelectPopup<T> {
    pub fn get_title(&self) -> Option<&str> {
        self.title.as_deref()
    }
}

impl<T: Display> SelectPopup<T> {
    pub fn with_items(
        &mut self,
        maybe_title: Option<String>,
        items: Vec<T>,
        sender: tokio::sync::oneshot::Sender<Option<usize>>,
    ) {
        *self = SelectPopup {
            visible: self.visible,
            state: ListState::default(),
            items,
            sender: Some(sender),
            title: maybe_title,
        };
        if !self.items.is_empty() {
            self.state.select(Some(0))
        }
    }

    pub fn get_lines(&self) -> &Vec<T> {
        &self.items
    }

    pub fn toggle(&mut self) -> bool {
        self.visible = !self.visible;
        true
    }

    pub fn close_unselected(&mut self) -> bool {
        if let Some(sender) = self.sender.take() {
            if let Err(e) = sender.send(None) {
                emit!(Error(format!("Could not send {e:?} via one shot channel.")));
            } else {
                self.toggle();
            }
        }
        true
    }

    pub fn close(&mut self) -> bool {
        let mut error_message = None;
        if let Some(sender) = self.sender.take() {
            let k = sender.send(self.state.selected());
            if let Err(e) = k {
                error_message = Some(format!(
                    "index: {:?} could not be sent through the channel",
                    e
                ));
            };
        } else {
            error_message = Some(
                "Sender not present in Stateful list. Cannot send the selected item.".to_string(),
            );
        }
        if let Some(em) = error_message {
            emit!(Error(em));
        }
        self.toggle();
        true
    }

    pub fn next_item(&mut self) -> bool {
        let i = match self.state.selected() {
            Some(i) => {
                if i >= self.items.len() - 1 {
                    0
                } else {
                    i + 1
                }
            }
            None => 0,
        };
        self.state.select(Some(i));
        true
    }

    pub fn prev_item(&mut self) -> bool {
        let i = match self.state.selected() {
            Some(i) => {
                if i == 0 {
                    self.items.len() - 1
                } else {
                    i - 1
                }
            }
            None => 0,
        };
        self.state.select(Some(i));
        true
    }

    pub fn unselect(&mut self) {
        self.state.select(None);
    }
}