1- use crossterm :: event :: { self , Event , KeyCode } ;
1+ use crate :: app :: App ;
22use std:: {
33 io,
44 time:: { Duration , Instant } ,
55} ;
6- use tui:: {
7- backend:: Backend ,
8- layout:: { Constraint , Direction , Layout } ,
9- style:: { Color , Modifier , Style } ,
10- widgets:: { BarChart , Block , Borders } ,
11- Frame , Terminal ,
12- } ;
13- use crate :: app:: App ;
14- use crate :: state:: Page ;
6+ use tui:: { backend:: Backend , Terminal } ;
157pub struct AppBar < ' a > {
16- data : Vec < ( & ' a str , u64 ) > ,
8+ pub ( crate ) data : Vec < ( & ' a str , u64 ) > ,
179 last_tick : Instant ,
1810}
19-
11+ use super :: render:: ui;
12+ use super :: state:: information_state;
2013impl < ' a > AppBar < ' a > {
2114 pub fn new ( ) -> AppBar < ' a > {
2215 AppBar {
@@ -46,7 +39,7 @@ impl<'a> AppBar<'a> {
4639 ( "B23" , 3 ) ,
4740 ( "B24" , 5 ) ,
4841 ] ,
49- last_tick : Instant :: now ( ) ,
42+ last_tick : Instant :: now ( ) ,
5043 }
5144 }
5245
@@ -56,75 +49,21 @@ impl<'a> AppBar<'a> {
5649 }
5750}
5851impl < ' a > App for AppBar < ' a > {
59- fn run_app_local < B : Backend > ( & mut self , terminal : & mut Terminal < B > ) -> io:: Result < crate :: state:: IFEXIT > {
60- terminal. draw ( |f| ui ( f, self ) ) ?;
52+ fn run_app_local < B : Backend > (
53+ & mut self ,
54+ terminal : & mut Terminal < B > ,
55+ ) -> io:: Result < crate :: state:: IFEXIT > {
56+ terminal. draw ( |f| ui ( f, self ) ) ?;
6157 let tick_rate = Duration :: from_millis ( 250 ) ;
6258 //let mut last_tick = Instant::now();
6359 let timeout = tick_rate
6460 . checked_sub ( self . last_tick . elapsed ( ) )
6561 . unwrap_or_else ( || Duration :: from_secs ( 0 ) ) ;
66- if crossterm:: event:: poll ( timeout) ? {
67- if let Event :: Key ( key) = event:: read ( ) ? {
68- match key. code {
69- KeyCode :: Char ( 'q' ) => return Ok ( crate :: state:: IFEXIT :: Exit ) ,
70- KeyCode :: Char ( 't' ) => return Ok ( crate :: state:: IFEXIT :: Change ( Page :: SubScribe ) ) ,
71- _ => { }
72- }
73- }
74- }
62+
7563 if self . last_tick . elapsed ( ) >= tick_rate {
7664 self . on_tick ( ) ;
7765 self . last_tick = Instant :: now ( ) ;
7866 }
79- Ok ( crate :: state:: IFEXIT :: Next )
80-
67+ information_state ( timeout)
8168 }
8269}
83-
84-
85- fn ui < B : Backend > ( f : & mut Frame < B > , app : & AppBar ) {
86- let chunks = Layout :: default ( )
87- . direction ( Direction :: Vertical )
88- . margin ( 2 )
89- . constraints ( [ Constraint :: Percentage ( 50 ) , Constraint :: Percentage ( 50 ) ] . as_ref ( ) )
90- . split ( f. size ( ) ) ;
91- let barchart = BarChart :: default ( )
92- . block ( Block :: default ( ) . title ( "Data1" ) . borders ( Borders :: ALL ) )
93- . data ( & app. data )
94- . bar_width ( 9 )
95- . bar_style ( Style :: default ( ) . fg ( Color :: Yellow ) )
96- . value_style ( Style :: default ( ) . fg ( Color :: Black ) . bg ( Color :: Yellow ) ) ;
97- f. render_widget ( barchart, chunks[ 0 ] ) ;
98-
99- let chunks = Layout :: default ( )
100- . direction ( Direction :: Horizontal )
101- . constraints ( [ Constraint :: Percentage ( 50 ) , Constraint :: Percentage ( 50 ) ] . as_ref ( ) )
102- . split ( chunks[ 1 ] ) ;
103-
104- let barchart = BarChart :: default ( )
105- . block ( Block :: default ( ) . title ( "Data2" ) . borders ( Borders :: ALL ) )
106- . data ( & app. data )
107- . bar_width ( 5 )
108- . bar_gap ( 3 )
109- . bar_style ( Style :: default ( ) . fg ( Color :: Green ) )
110- . value_style (
111- Style :: default ( )
112- . bg ( Color :: Green )
113- . add_modifier ( Modifier :: BOLD ) ,
114- ) ;
115- f. render_widget ( barchart, chunks[ 0 ] ) ;
116-
117- let barchart = BarChart :: default ( )
118- . block ( Block :: default ( ) . title ( "Data3" ) . borders ( Borders :: ALL ) )
119- . data ( & app. data )
120- . bar_style ( Style :: default ( ) . fg ( Color :: Red ) )
121- . bar_width ( 7 )
122- . bar_gap ( 0 )
123- . value_style ( Style :: default ( ) . bg ( Color :: Red ) )
124- . label_style (
125- Style :: default ( )
126- . fg ( Color :: Cyan )
127- . add_modifier ( Modifier :: ITALIC ) ,
128- ) ;
129- f. render_widget ( barchart, chunks[ 1 ] ) ;
130- }
0 commit comments