11use ratatui:: {
2- layout:: { Alignment , Constraint , Rect } ,
3- style:: { Style , Stylize } ,
4- widgets:: { Block , Borders , Clear , Padding , Row , Table , TableState } ,
2+ layout:: { Alignment , Constraint , Direction , Layout } ,
3+ style:: { Color , Style , Stylize } ,
4+ widgets:: { Block , BorderType , Borders , Cell , Clear , Padding , Row , Table } ,
55 Frame ,
66} ;
77
88pub struct Help {
99 block_height : usize ,
10- state : TableState ,
11- keys : & ' static [ ( & ' static str , & ' static str ) ] ,
10+ keys : Vec < ( Cell < ' static > , & ' static str ) > ,
1211}
1312
1413impl Default for Help {
1514 fn default ( ) -> Self {
16- let mut state = TableState :: new ( ) . with_offset ( 0 ) ;
17- state. select ( Some ( 0 ) ) ;
18-
1915 Self {
2016 block_height : 0 ,
21- state,
22- keys : & [
23- ( "Esc" , "Switch to Normal mode / Dismiss pop-up" ) ,
24- ( "Tab" , "Switch the focus" ) ,
17+ keys : vec ! [
18+ (
19+ Cell :: from( "Esc" ) . bold( ) . yellow( ) ,
20+ "Switch to Normal mode / Dismiss pop-up" ,
21+ ) ,
22+ ( Cell :: from( "Tab" ) . bold( ) . yellow( ) , "Switch the focus" ) ,
2523 (
26- "ctrl + n" ,
24+ Cell :: from ( "ctrl + n" ) . bold ( ) . yellow ( ) ,
2725 "Start new chat and save the previous one to the history" ,
2826 ) ,
29- ( "ctrl + s" , "Save the chat to file in the current directory" ) ,
30- ( "ctrl + h" , "Show history" ) ,
31- ( "ctrl + t" , "Stop the stream response" ) ,
32- ( "j or Down" , "Scroll down" ) ,
33- ( "k or Up" , "Scroll up" ) ,
34- ( "G" , "Go to the end" ) ,
35- ( "gg" , "Go to the top" ) ,
36- ( "?" , "Show help" ) ,
27+ (
28+ Cell :: from( "ctrl + s" ) . bold( ) . yellow( ) ,
29+ "Save the chat to file in the current directory" ,
30+ ) ,
31+ ( Cell :: from( "ctrl + h" ) . bold( ) . yellow( ) , "Show history" ) ,
32+ (
33+ Cell :: from( "ctrl + t" ) . bold( ) . yellow( ) ,
34+ "Stop the stream response" ,
35+ ) ,
36+ ( Cell :: from( "j or Down" ) . bold( ) . yellow( ) , "Scroll down" ) ,
37+ ( Cell :: from( "k or Up" ) . bold( ) . yellow( ) , "Scroll up" ) ,
38+ ( Cell :: from( "G" ) . bold( ) . yellow( ) , "Go to the end" ) ,
39+ ( Cell :: from( "gg" ) . bold( ) . yellow( ) , "Go to the top" ) ,
40+ ( Cell :: from( "?" ) . bold( ) . yellow( ) , "Show help" ) ,
3741 ] ,
3842 }
3943 }
@@ -44,56 +48,51 @@ impl Help {
4448 Self :: default ( )
4549 }
4650
47- pub fn scroll_down ( & mut self ) {
48- let i = match self . state . selected ( ) {
49- Some ( i) => {
50- if i >= self . keys . len ( ) . saturating_sub ( self . block_height - 6 ) {
51- i
52- } else {
53- i + 1
54- }
55- }
56- None => 1 ,
57- } ;
58- * self . state . offset_mut ( ) = i;
59- self . state . select ( Some ( i) ) ;
60- }
61- pub fn scroll_up ( & mut self ) {
62- let i = match self . state . selected ( ) {
63- Some ( i) => {
64- if i > 1 {
65- i - 1
66- } else {
67- 0
68- }
69- }
70- None => 1 ,
71- } ;
72- * self . state . offset_mut ( ) = i;
73- self . state . select ( Some ( i) ) ;
74- }
51+ pub fn render ( & mut self , frame : & mut Frame ) {
52+ let layout = Layout :: default ( )
53+ . direction ( Direction :: Vertical )
54+ . constraints ( [
55+ Constraint :: Fill ( 1 ) ,
56+ Constraint :: Length ( 15 ) ,
57+ Constraint :: Fill ( 1 ) ,
58+ ] )
59+ . flex ( ratatui:: layout:: Flex :: SpaceBetween )
60+ . split ( frame. area ( ) ) ;
61+
62+ let block = Layout :: default ( )
63+ . direction ( Direction :: Horizontal )
64+ . constraints ( [
65+ Constraint :: Fill ( 1 ) ,
66+ Constraint :: Min ( 75 ) ,
67+ Constraint :: Fill ( 1 ) ,
68+ ] )
69+ . flex ( ratatui:: layout:: Flex :: SpaceBetween )
70+ . split ( layout[ 1 ] ) [ 1 ] ;
7571
76- pub fn render ( & mut self , frame : & mut Frame , block : Rect ) {
7772 self . block_height = block. height as usize ;
78- let widths = [ Constraint :: Length ( 15 ) , Constraint :: Min ( 60 ) ] ;
73+ let widths = [ Constraint :: Length ( 12 ) , Constraint :: Fill ( 1 ) ] ;
7974 let rows: Vec < Row > = self
8075 . keys
8176 . iter ( )
82- . map ( |key| Row :: new ( vec ! [ key. 0 , key. 1 ] ) )
77+ . map ( |key| {
78+ Row :: new ( vec ! [ key. 0 . to_owned( ) , key. 1 . into( ) ] )
79+ . style ( Style :: default ( ) . fg ( Color :: White ) )
80+ } )
8381 . collect ( ) ;
8482
8583 let table = Table :: new ( rows, widths) . block (
8684 Block :: default ( )
87- . padding ( Padding :: uniform ( 2 ) )
85+ . padding ( Padding :: uniform ( 1 ) )
8886 . title ( " Help " )
89- . title_style ( Style :: default ( ) . bold ( ) )
87+ . title_style ( Style :: default ( ) . bold ( ) . fg ( Color :: Green ) )
9088 . title_alignment ( Alignment :: Center )
9189 . borders ( Borders :: ALL )
9290 . style ( Style :: default ( ) )
93- . border_style ( Style :: default ( ) ) ,
91+ . border_type ( BorderType :: Thick )
92+ . border_style ( Style :: default ( ) . fg ( Color :: Green ) ) ,
9493 ) ;
9594
9695 frame. render_widget ( Clear , block) ;
97- frame. render_stateful_widget ( table, block, & mut self . state ) ;
96+ frame. render_widget ( table, block) ;
9897 }
9998}
0 commit comments