-
-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathalias_filter.rs
104 lines (85 loc) · 2.9 KB
/
alias_filter.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
use std::sync::Arc;
use std::time::Instant;
use ratatui::{
layout::{Alignment, Constraint, Direction, Layout},
style::{Color, Style, Stylize},
widgets::{Block, BorderType, Borders, Clear, Padding, Row, Table, TableState},
Frame,
};
use tui_input::{Input, InputRequest};
use crate::config::Config;
#[derive(Debug)]
pub struct AliasFilter {
pub filter: Option<String>,
input: Input,
state: TableState,
start: Instant,
}
impl AliasFilter {
pub fn new(_config: Arc<Config>) -> Self {
let mut state = TableState::new().with_offset(0);
state.select(Some(0));
let input = Input::new("".to_string());
let start = Instant::now();
Self {
state,
input,
filter: None,
start,
}
}
pub fn insert_char(&mut self, c: char) {
let req = InputRequest::InsertChar(c);
self.input.handle(req);
self.filter = Some(self.input.value().to_string());
}
pub fn delete_char(&mut self) {
let req = InputRequest::DeletePrevChar;
self.input.handle(req);
self.filter = Some(self.input.value().to_string());
}
pub fn render(&mut self, frame: &mut Frame) {
let layout = Layout::default()
.direction(Direction::Vertical)
.constraints([
Constraint::Fill(1),
Constraint::Length(5),
Constraint::Fill(1),
])
.flex(ratatui::layout::Flex::SpaceBetween)
.split(frame.area());
let block = Layout::default()
.direction(Direction::Horizontal)
.constraints([
Constraint::Fill(1),
Constraint::Min(80),
Constraint::Fill(1),
])
.flex(ratatui::layout::Flex::SpaceBetween)
.split(layout[1])[1];
let mut text = match &self.filter {
Some(f) => f.to_string(),
None => "".to_string(),
};
self.insert_cursor(&mut text);
let row = Row::new(vec![text]);
let table = Table::new([row], [Constraint::Length(20)]).block(
Block::default()
.padding(Padding::uniform(1))
.title(" Filter Device Names ")
.title_style(Style::default().bold().fg(Color::Green))
.title_alignment(Alignment::Center)
.borders(Borders::ALL)
.style(Style::default())
.border_type(BorderType::Thick)
.border_style(Style::default().fg(Color::Green)),
);
frame.render_widget(Clear, block);
frame.render_stateful_widget(table, block, &mut self.state);
}
fn insert_cursor(&self, s: &mut String) {
let time = Instant::now().duration_since(self.start).as_secs();
let c = if time % 2 == 0 { '_' } else { ' ' };
s.insert(self.input.cursor(), c);
}
}