11use anyhow:: { Context , Result } ;
22use crossterm:: {
33 cursor:: { MoveTo , MoveToNextLine } ,
4- style:: { Attribute , Color , ResetColor , SetAttribute , SetBackgroundColor , SetForegroundColor } ,
4+ style:: {
5+ Attribute , Attributes , Color , ResetColor , SetAttribute , SetAttributes , SetForegroundColor ,
6+ } ,
57 terminal:: { self , BeginSynchronizedUpdate , Clear , ClearType , EndSynchronizedUpdate } ,
68 QueueableCommand ,
79} ;
@@ -19,6 +21,9 @@ use crate::{
1921use super :: scroll_state:: ScrollState ;
2022
2123const COL_SPACING : usize = 2 ;
24+ const SELECTED_ROW_ATTRIBUTES : Attributes = Attributes :: none ( )
25+ . with ( Attribute :: Reverse )
26+ . with ( Attribute :: Bold ) ;
2227
2328fn next_ln ( stdout : & mut StdoutLock ) -> io:: Result < ( ) > {
2429 stdout
@@ -41,6 +46,7 @@ pub struct ListState<'a> {
4146 app_state : & ' a mut AppState ,
4247 scroll_state : ScrollState ,
4348 name_col_padding : Vec < u8 > ,
49+ path_col_padding : Vec < u8 > ,
4450 filter : Filter ,
4551 term_width : u16 ,
4652 term_height : u16 ,
@@ -52,13 +58,18 @@ impl<'a> ListState<'a> {
5258 stdout. queue ( Clear ( ClearType :: All ) ) ?;
5359
5460 let name_col_title_len = 4 ;
55- let name_col_width = app_state
56- . exercises ( )
57- . iter ( )
58- . map ( |exercise| exercise. name . len ( ) )
59- . max ( )
60- . map_or ( name_col_title_len, |max| max. max ( name_col_title_len) ) ;
61+ let path_col_title_len = 4 ;
62+ let ( name_col_width, path_col_width) = app_state. exercises ( ) . iter ( ) . fold (
63+ ( name_col_title_len, path_col_title_len) ,
64+ |( name_col_width, path_col_width) , exercise| {
65+ (
66+ name_col_width. max ( exercise. name . len ( ) ) ,
67+ path_col_width. max ( exercise. path . len ( ) ) ,
68+ )
69+ } ,
70+ ) ;
6171 let name_col_padding = vec ! [ b' ' ; name_col_width + COL_SPACING ] ;
72+ let path_col_padding = vec ! [ b' ' ; path_col_width] ;
6273
6374 let filter = Filter :: None ;
6475 let n_rows_with_filter = app_state. exercises ( ) . len ( ) ;
@@ -73,6 +84,7 @@ impl<'a> ListState<'a> {
7384 app_state,
7485 scroll_state,
7586 name_col_padding,
87+ path_col_padding,
7688 filter,
7789 // Set by `set_term_size`
7890 term_width : 0 ,
@@ -119,7 +131,7 @@ impl<'a> ListState<'a> {
119131 writer. write_str ( pre_highlight) ?;
120132 writer. stdout . queue ( SetForegroundColor ( Color :: Magenta ) ) ?;
121133 writer. write_str ( highlight) ?;
122- writer. stdout . queue ( ResetColor ) ?;
134+ writer. stdout . queue ( SetForegroundColor ( Color :: Reset ) ) ?;
123135 return writer. write_str ( post_highlight) ;
124136 }
125137 }
@@ -143,14 +155,12 @@ impl<'a> ListState<'a> {
143155 let mut writer = MaxLenWriter :: new ( stdout, self . term_width as usize ) ;
144156
145157 if self . scroll_state . selected ( ) == Some ( row_offset + n_displayed_rows) {
146- writer. stdout . queue ( SetBackgroundColor ( Color :: Rgb {
147- r : 40 ,
148- g : 40 ,
149- b : 40 ,
150- } ) ) ?;
151158 // The crab emoji has the width of two ascii chars.
152159 writer. add_to_len ( 2 ) ;
153160 writer. stdout . write_all ( "🦀" . as_bytes ( ) ) ?;
161+ writer
162+ . stdout
163+ . queue ( SetAttributes ( SELECTED_ROW_ATTRIBUTES ) ) ?;
154164 } else {
155165 writer. write_ascii ( b" " ) ?;
156166 }
@@ -164,12 +174,13 @@ impl<'a> ListState<'a> {
164174
165175 if exercise. done {
166176 writer. stdout . queue ( SetForegroundColor ( Color :: Green ) ) ?;
167- writer. write_ascii ( b"DONE " ) ?;
177+ writer. write_ascii ( b"DONE " ) ?;
168178 } else {
169179 writer. stdout . queue ( SetForegroundColor ( Color :: Yellow ) ) ?;
170- writer. write_ascii ( b"PENDING " ) ?;
180+ writer. write_ascii ( b"PENDING" ) ?;
171181 }
172182 writer. stdout . queue ( SetForegroundColor ( Color :: Reset ) ) ?;
183+ writer. write_ascii ( b" " ) ?;
173184
174185 self . draw_exericse_name ( & mut writer, exercise) ?;
175186
@@ -183,6 +194,8 @@ impl<'a> ListState<'a> {
183194 exercise. terminal_file_link ( & mut writer) ?;
184195 }
185196
197+ writer. write_ascii ( & self . path_col_padding [ exercise. path . len ( ) ..] ) ?;
198+
186199 next_ln ( stdout) ?;
187200 stdout. queue ( ResetColor ) ?;
188201 n_displayed_rows += 1 ;
0 commit comments