1
- use std:: io:: { self , stdout } ;
1
+ use std:: io:: { self } ;
2
2
3
- use color_eyre:: { config :: HookBuilder , Result } ;
3
+ use color_eyre:: Result ;
4
4
use 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 } ,
13
10
widgets:: * ,
11
+ DefaultTerminal ,
14
12
} ;
15
13
use tui_scrollview:: { ScrollView , ScrollViewState } ;
16
14
17
15
fn 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
23
21
}
24
22
25
23
#[ derive( Debug , Default , Clone ) ]
@@ -48,11 +46,10 @@ impl App {
48
46
}
49
47
}
50
48
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 < ( ) > {
53
50
while self . is_running ( ) {
54
- self . handle_events ( ) ?;
55
51
self . draw ( & mut terminal) ?;
52
+ self . handle_events ( ) ?;
56
53
}
57
54
Ok ( ( ) )
58
55
}
@@ -61,7 +58,7 @@ impl App {
61
58
self . state == AppState :: Running
62
59
}
63
60
64
- fn draw ( & mut self , terminal : & mut Terminal < impl Backend > ) -> io:: Result < ( ) > {
61
+ fn draw ( & mut self , terminal : & mut DefaultTerminal ) -> io:: Result < ( ) > {
65
62
terminal. draw ( |frame| frame. render_widget ( self , frame. area ( ) ) ) ?;
66
63
Ok ( ( ) )
67
64
}
@@ -190,32 +187,3 @@ fn bars() -> BarGroup<'static> {
190
187
. map ( |( label, value, color) | Bar :: default ( ) . label ( label. into ( ) ) . value ( value) . style ( color) ) ;
191
188
BarGroup :: default ( ) . bars ( & data)
192
189
}
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