leetcode_tui_core/
input.rs1use crate::SendError;
2
3#[derive(Default)]
4pub struct Input {
5 pub visible: bool,
6 current_text: Option<String>,
7 sender: Option<super::UBStrSender>,
8}
9
10impl Input {
11 pub fn text(&self) -> Option<&String> {
12 self.current_text.as_ref()
13 }
14}
15
16impl Input {
17 pub fn close(&mut self) -> bool {
18 self.current_text = None;
19 if let Some(sender) = self.sender.take() {
20 tokio::spawn(async move {
21 let _ = sender.send(None).emit_if_error();
22 });
23 }
24 self.toggle()
25 }
26
27 pub fn char(&mut self, c: char) -> bool {
28 if let Some(_text) = self.current_text.as_mut() {
29 _text.push(c);
30 } else {
31 self.current_text = Some(c.into());
32 }
33 self.try_send();
34 true
35 }
36
37 pub fn remove_char(&mut self) -> bool {
38 if let Some(_text) = self.current_text.as_mut() {
39 if !_text.is_empty() {
40 _text.pop();
41 self.try_send();
42 }
43 }
44 true
45 }
46
47 pub fn try_send(&mut self) {
48 let text = self.current_text.clone();
49 if let Some(sender) = self.sender.clone() {
50 tokio::spawn(async move {
51 let _ = sender.send(text).emit_if_error();
52 });
53 }
54 }
55
56 pub fn toggle(&mut self) -> bool {
57 self.visible = !self.visible;
58 true
59 }
60
61 pub fn reset_with(&mut self, sender: super::UBStrSender, default_input: Option<String>) {
62 self.sender = Some(sender);
63 self.current_text = default_input;
64 }
65}