1- use std:: io:: { self , stdout } ;
1+ use std:: io:: { self } ;
22
3- use color_eyre:: { config :: HookBuilder , Result } ;
3+ use color_eyre:: Result ;
44use ratatui:: {
5- crossterm:: {
6- event:: { self , Event , KeyCode , KeyEventKind } ,
7- terminal:: { disable_raw_mode, enable_raw_mode, EnterAlternateScreen , LeaveAlternateScreen } ,
8- ExecutableCommand ,
9- } ,
10- layout:: Size ,
11- prelude:: * ,
12- style:: palette:: tailwind,
5+ buffer:: Buffer ,
6+ crossterm:: event:: { self , Event , KeyCode , KeyEventKind } ,
7+ layout:: { Constraint , Direction , Layout , Rect , Size } ,
8+ style:: { palette:: tailwind, Color , Stylize } ,
9+ text:: { Line , Text } ,
1310 widgets:: * ,
11+ DefaultTerminal ,
1412} ;
1513use tui_scrollview:: { ScrollView , ScrollViewState } ;
1614
1715fn main ( ) -> Result < ( ) > {
18- init_error_hooks ( ) ?;
19- let terminal = init_terminal ( ) ? ;
20- App :: new ( ) . run ( terminal) ? ;
21- restore_terminal ( ) ? ;
22- Ok ( ( ) )
16+ color_eyre :: install ( ) ?;
17+ let terminal = ratatui :: init ( ) ;
18+ let result = App :: new ( ) . run ( terminal) ;
19+ ratatui :: restore ( ) ;
20+ result
2321}
2422
2523#[ derive( Debug , Default , Clone ) ]
@@ -48,11 +46,10 @@ impl App {
4846 }
4947 }
5048
51- fn run ( & mut self , mut terminal : Terminal < impl Backend > ) -> Result < ( ) > {
52- self . draw ( & mut terminal) ?;
49+ fn run ( & mut self , mut terminal : DefaultTerminal ) -> Result < ( ) > {
5350 while self . is_running ( ) {
54- self . handle_events ( ) ?;
5551 self . draw ( & mut terminal) ?;
52+ self . handle_events ( ) ?;
5653 }
5754 Ok ( ( ) )
5855 }
@@ -61,7 +58,7 @@ impl App {
6158 self . state == AppState :: Running
6259 }
6360
64- fn draw ( & mut self , terminal : & mut Terminal < impl Backend > ) -> io:: Result < ( ) > {
61+ fn draw ( & mut self , terminal : & mut DefaultTerminal ) -> io:: Result < ( ) > {
6562 terminal. draw ( |frame| frame. render_widget ( self , frame. area ( ) ) ) ?;
6663 Ok ( ( ) )
6764 }
@@ -190,32 +187,3 @@ fn bars() -> BarGroup<'static> {
190187 . map ( |( label, value, color) | Bar :: default ( ) . label ( label. into ( ) ) . value ( value) . style ( color) ) ;
191188 BarGroup :: default ( ) . bars ( & data)
192189}
193-
194- fn init_error_hooks ( ) -> Result < ( ) > {
195- let ( panic, error) = HookBuilder :: default ( ) . into_hooks ( ) ;
196- let panic = panic. into_panic_hook ( ) ;
197- let error = error. into_eyre_hook ( ) ;
198- color_eyre:: eyre:: set_hook ( Box :: new ( move |e| {
199- let _ = restore_terminal ( ) ;
200- error ( e)
201- } ) ) ?;
202- std:: panic:: set_hook ( Box :: new ( move |info| {
203- let _ = restore_terminal ( ) ;
204- panic ( info)
205- } ) ) ;
206- Ok ( ( ) )
207- }
208-
209- fn init_terminal ( ) -> Result < Terminal < impl Backend > > {
210- enable_raw_mode ( ) ?;
211- stdout ( ) . execute ( EnterAlternateScreen ) ?;
212- let backend = CrosstermBackend :: new ( stdout ( ) ) ;
213- let terminal = Terminal :: new ( backend) ?;
214- Ok ( terminal)
215- }
216-
217- fn restore_terminal ( ) -> Result < ( ) > {
218- disable_raw_mode ( ) ?;
219- stdout ( ) . execute ( LeaveAlternateScreen ) ?;
220- Ok ( ( ) )
221- }
0 commit comments