1
1
use anyhow:: { Context , Result } ;
2
2
use crossterm:: {
3
3
cursor:: { MoveTo , MoveToNextLine } ,
4
- style:: { Attribute , Color , ResetColor , SetAttribute , SetBackgroundColor , SetForegroundColor } ,
4
+ style:: {
5
+ Attribute , Attributes , Color , ResetColor , SetAttribute , SetAttributes , SetForegroundColor ,
6
+ } ,
5
7
terminal:: { self , BeginSynchronizedUpdate , Clear , ClearType , EndSynchronizedUpdate } ,
6
8
QueueableCommand ,
7
9
} ;
@@ -19,6 +21,9 @@ use crate::{
19
21
use super :: scroll_state:: ScrollState ;
20
22
21
23
const COL_SPACING : usize = 2 ;
24
+ const SELECTED_ROW_ATTRIBUTES : Attributes = Attributes :: none ( )
25
+ . with ( Attribute :: Reverse )
26
+ . with ( Attribute :: Bold ) ;
22
27
23
28
fn next_ln ( stdout : & mut StdoutLock ) -> io:: Result < ( ) > {
24
29
stdout
@@ -41,6 +46,7 @@ pub struct ListState<'a> {
41
46
app_state : & ' a mut AppState ,
42
47
scroll_state : ScrollState ,
43
48
name_col_padding : Vec < u8 > ,
49
+ path_col_padding : Vec < u8 > ,
44
50
filter : Filter ,
45
51
term_width : u16 ,
46
52
term_height : u16 ,
@@ -52,13 +58,18 @@ impl<'a> ListState<'a> {
52
58
stdout. queue ( Clear ( ClearType :: All ) ) ?;
53
59
54
60
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
+ ) ;
61
71
let name_col_padding = vec ! [ b' ' ; name_col_width + COL_SPACING ] ;
72
+ let path_col_padding = vec ! [ b' ' ; path_col_width] ;
62
73
63
74
let filter = Filter :: None ;
64
75
let n_rows_with_filter = app_state. exercises ( ) . len ( ) ;
@@ -73,6 +84,7 @@ impl<'a> ListState<'a> {
73
84
app_state,
74
85
scroll_state,
75
86
name_col_padding,
87
+ path_col_padding,
76
88
filter,
77
89
// Set by `set_term_size`
78
90
term_width : 0 ,
@@ -119,7 +131,7 @@ impl<'a> ListState<'a> {
119
131
writer. write_str ( pre_highlight) ?;
120
132
writer. stdout . queue ( SetForegroundColor ( Color :: Magenta ) ) ?;
121
133
writer. write_str ( highlight) ?;
122
- writer. stdout . queue ( ResetColor ) ?;
134
+ writer. stdout . queue ( SetForegroundColor ( Color :: Reset ) ) ?;
123
135
return writer. write_str ( post_highlight) ;
124
136
}
125
137
}
@@ -143,14 +155,12 @@ impl<'a> ListState<'a> {
143
155
let mut writer = MaxLenWriter :: new ( stdout, self . term_width as usize ) ;
144
156
145
157
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
- } ) ) ?;
151
158
// The crab emoji has the width of two ascii chars.
152
159
writer. add_to_len ( 2 ) ;
153
160
writer. stdout . write_all ( "🦀" . as_bytes ( ) ) ?;
161
+ writer
162
+ . stdout
163
+ . queue ( SetAttributes ( SELECTED_ROW_ATTRIBUTES ) ) ?;
154
164
} else {
155
165
writer. write_ascii ( b" " ) ?;
156
166
}
@@ -164,12 +174,13 @@ impl<'a> ListState<'a> {
164
174
165
175
if exercise. done {
166
176
writer. stdout . queue ( SetForegroundColor ( Color :: Green ) ) ?;
167
- writer. write_ascii ( b"DONE " ) ?;
177
+ writer. write_ascii ( b"DONE " ) ?;
168
178
} else {
169
179
writer. stdout . queue ( SetForegroundColor ( Color :: Yellow ) ) ?;
170
- writer. write_ascii ( b"PENDING " ) ?;
180
+ writer. write_ascii ( b"PENDING" ) ?;
171
181
}
172
182
writer. stdout . queue ( SetForegroundColor ( Color :: Reset ) ) ?;
183
+ writer. write_ascii ( b" " ) ?;
173
184
174
185
self . draw_exericse_name ( & mut writer, exercise) ?;
175
186
@@ -183,6 +194,8 @@ impl<'a> ListState<'a> {
183
194
exercise. terminal_file_link ( & mut writer) ?;
184
195
}
185
196
197
+ writer. write_ascii ( & self . path_col_padding [ exercise. path . len ( ) ..] ) ?;
198
+
186
199
next_ln ( stdout) ?;
187
200
stdout. queue ( ResetColor ) ?;
188
201
n_displayed_rows += 1 ;
0 commit comments