leetcode_tui_core/
input.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
use crate::SendError;

#[derive(Default)]
pub struct Input {
    pub visible: bool,
    current_text: Option<String>,
    sender: Option<super::UBStrSender>,
}

impl Input {
    pub fn text(&self) -> Option<&String> {
        self.current_text.as_ref()
    }
}

impl Input {
    pub fn close(&mut self) -> bool {
        self.current_text = None;
        if let Some(sender) = self.sender.take() {
            tokio::spawn(async move {
                let _ = sender.send(None).emit_if_error();
            });
        }
        self.toggle()
    }

    pub fn char(&mut self, c: char) -> bool {
        if let Some(_text) = self.current_text.as_mut() {
            _text.push(c);
        } else {
            self.current_text = Some(c.into());
        }
        self.try_send();
        true
    }

    pub fn remove_char(&mut self) -> bool {
        if let Some(_text) = self.current_text.as_mut() {
            if !_text.is_empty() {
                _text.pop();
                self.try_send();
            }
        }
        true
    }

    pub fn try_send(&mut self) {
        let text = self.current_text.clone();
        if let Some(sender) = self.sender.clone() {
            tokio::spawn(async move {
                let _ = sender.send(text).emit_if_error();
            });
        }
    }

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

    pub fn reset_with(&mut self, sender: super::UBStrSender, default_input: Option<String>) {
        self.sender = Some(sender);
        self.current_text = default_input;
    }
}