@@ -28,12 +28,38 @@ use crate::{
28
28
29
29
#[ derive( Debug ) ]
30
30
pub struct Metrics {
31
- user_input : Input ,
31
+ user_input : UserInput ,
32
32
app_packets : Arc < Mutex < Vec < AppPacket > > > ,
33
33
port_count : Option < Arc < Mutex < PortCountMetric > > > ,
34
34
terminate : Arc < AtomicBool > ,
35
35
}
36
36
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
+
37
63
#[ derive( Debug , Default , Clone ) ]
38
64
pub struct PortCountMetric {
39
65
port : u16 ,
@@ -44,7 +70,7 @@ pub struct PortCountMetric {
44
70
impl Metrics {
45
71
pub fn new ( packets : Arc < Mutex < Vec < AppPacket > > > ) -> Self {
46
72
Self {
47
- user_input : Input :: default ( ) ,
73
+ user_input : UserInput :: default ( ) ,
48
74
app_packets : packets,
49
75
port_count : None ,
50
76
terminate : Arc :: new ( AtomicBool :: new ( false ) ) ,
@@ -107,25 +133,23 @@ impl Metrics {
107
133
self . terminate
108
134
. store ( true , std:: sync:: atomic:: Ordering :: Relaxed ) ;
109
135
self . port_count = None ;
110
- self . user_input . reset ( ) ;
136
+ self . user_input . clear ( ) ;
111
137
self . terminate
112
138
. store ( false , std:: sync:: atomic:: Ordering :: Relaxed ) ;
113
139
}
114
140
}
115
141
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 < ( ) > {
121
143
match key_event. code {
122
144
KeyCode :: Esc => {
123
- self . user_input . reset ( ) ;
145
+ self . user_input . clear ( ) ;
124
146
}
125
147
126
148
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
+
129
153
let port_count = Arc :: new ( Mutex :: new ( PortCountMetric {
130
154
port,
131
155
tcp_count : 0 ,
@@ -198,7 +222,7 @@ impl Metrics {
198
222
}
199
223
200
224
_ => {
201
- self . user_input . handle_event ( & Event :: Key ( key_event) ) ;
225
+ self . user_input . input . handle_event ( & Event :: Key ( key_event) ) ;
202
226
}
203
227
}
204
228
@@ -227,14 +251,28 @@ impl Metrics {
227
251
. split ( layout[ 1 ] ) [ 1 ] ;
228
252
229
253
//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
+ ] ;
238
276
239
277
let widths = [ Constraint :: Percentage ( 49 ) , Constraint :: Percentage ( 49 ) ] ;
240
278
0 commit comments