Skip to content

Commit 5e2fdb1

Browse files
committed
Add input validation
1 parent c3b5a74 commit 5e2fdb1

File tree

2 files changed

+61
-32
lines changed

2 files changed

+61
-32
lines changed

oryx-tui/src/handler.rs

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,7 @@ pub fn handle_key_events(
7171
app.is_editing = false;
7272
}
7373
ActivePopup::NewMetricExplorer => {
74-
app.section
75-
.metrics
76-
.handle_popup_keys(key_event, event_sender.clone())?;
74+
app.section.metrics.handle_popup_keys(key_event)?;
7775
app.is_editing = false;
7876
}
7977
_ => {}
@@ -99,12 +97,7 @@ pub fn handle_key_events(
9997
}
10098
}
10199
ActivePopup::NewMetricExplorer => {
102-
if app
103-
.section
104-
.metrics
105-
.handle_popup_keys(key_event, event_sender.clone())
106-
.is_ok()
107-
{
100+
if app.section.metrics.handle_popup_keys(key_event).is_ok() {
108101
app.active_popup = None;
109102
app.is_editing = false;
110103
}
@@ -122,9 +115,7 @@ pub fn handle_key_events(
122115
.handle_keys(key_event, event_sender.clone())?;
123116
}
124117
ActivePopup::NewMetricExplorer => {
125-
app.section
126-
.metrics
127-
.handle_popup_keys(key_event, event_sender.clone())?;
118+
app.section.metrics.handle_popup_keys(key_event)?;
128119
}
129120
_ => {}
130121
},

oryx-tui/src/section/metrics.rs

Lines changed: 58 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,38 @@ use crate::{
2828

2929
#[derive(Debug)]
3030
pub struct Metrics {
31-
user_input: Input,
31+
user_input: UserInput,
3232
app_packets: Arc<Mutex<Vec<AppPacket>>>,
3333
port_count: Option<Arc<Mutex<PortCountMetric>>>,
3434
terminate: Arc<AtomicBool>,
3535
}
3636

37+
#[derive(Debug, Clone, Default)]
38+
struct UserInput {
39+
input: Input,
40+
error: Option<String>,
41+
}
42+
43+
impl UserInput {
44+
fn validate(&mut self) -> AppResult<()> {
45+
self.error = None;
46+
if self.input.value().parse::<u16>().is_err() {
47+
self.error = Some("Invalid Port".to_string());
48+
return Err("Validation Error".into());
49+
}
50+
Ok(())
51+
}
52+
53+
fn clear(&mut self) {
54+
self.input.reset();
55+
self.error = None;
56+
}
57+
58+
fn value(&self) -> u16 {
59+
self.input.value().parse().unwrap()
60+
}
61+
}
62+
3763
#[derive(Debug, Default, Clone)]
3864
pub struct PortCountMetric {
3965
port: u16,
@@ -44,7 +70,7 @@ pub struct PortCountMetric {
4470
impl Metrics {
4571
pub fn new(packets: Arc<Mutex<Vec<AppPacket>>>) -> Self {
4672
Self {
47-
user_input: Input::default(),
73+
user_input: UserInput::default(),
4874
app_packets: packets,
4975
port_count: None,
5076
terminate: Arc::new(AtomicBool::new(false)),
@@ -107,25 +133,23 @@ impl Metrics {
107133
self.terminate
108134
.store(true, std::sync::atomic::Ordering::Relaxed);
109135
self.port_count = None;
110-
self.user_input.reset();
136+
self.user_input.clear();
111137
self.terminate
112138
.store(false, std::sync::atomic::Ordering::Relaxed);
113139
}
114140
}
115141

116-
pub fn handle_popup_keys(
117-
&mut self,
118-
key_event: KeyEvent,
119-
_sender: kanal::Sender<crate::event::Event>,
120-
) -> AppResult<()> {
142+
pub fn handle_popup_keys(&mut self, key_event: KeyEvent) -> AppResult<()> {
121143
match key_event.code {
122144
KeyCode::Esc => {
123-
self.user_input.reset();
145+
self.user_input.clear();
124146
}
125147

126148
KeyCode::Enter => {
127-
//TODO: validate input
128-
let port: u16 = self.user_input.value().parse().unwrap();
149+
self.user_input.validate()?;
150+
151+
let port: u16 = self.user_input.value();
152+
129153
let port_count = Arc::new(Mutex::new(PortCountMetric {
130154
port,
131155
tcp_count: 0,
@@ -198,7 +222,7 @@ impl Metrics {
198222
}
199223

200224
_ => {
201-
self.user_input.handle_event(&Event::Key(key_event));
225+
self.user_input.input.handle_event(&Event::Key(key_event));
202226
}
203227
}
204228

@@ -227,14 +251,28 @@ impl Metrics {
227251
.split(layout[1])[1];
228252

229253
//TODO: Center
230-
let rows = [Row::new(vec![
231-
Cell::from("Packet Counter".to_string())
232-
.bg(Color::DarkGray)
233-
.fg(Color::White),
234-
Cell::from(self.user_input.value())
235-
.bg(Color::DarkGray)
236-
.fg(Color::White),
237-
])];
254+
let rows = [
255+
Row::new(vec![
256+
Cell::from("Packet Counter".to_string())
257+
.bg(Color::DarkGray)
258+
.fg(Color::White),
259+
Cell::from(self.user_input.input.value())
260+
.bg(Color::DarkGray)
261+
.fg(Color::White),
262+
]),
263+
Row::new(vec![Cell::new(""), Cell::new("")]),
264+
Row::new(vec![
265+
Cell::new(""),
266+
Cell::from({
267+
if let Some(error) = &self.user_input.error {
268+
error.to_string()
269+
} else {
270+
String::new()
271+
}
272+
})
273+
.red(),
274+
]),
275+
];
238276

239277
let widths = [Constraint::Percentage(49), Constraint::Percentage(49)];
240278

0 commit comments

Comments
 (0)