Skip to content

Commit 46ad25f

Browse files
committed
Fix contrast in terminals with a light theme
1 parent 2a725fb commit 46ad25f

File tree

2 files changed

+33
-18
lines changed

2 files changed

+33
-18
lines changed

src/list/state.rs

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
use anyhow::{Context, Result};
22
use 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::{
1921
use super::scroll_state::ScrollState;
2022

2123
const COL_SPACING: usize = 2;
24+
const SELECTED_ROW_ATTRIBUTES: Attributes = Attributes::none()
25+
.with(Attribute::Reverse)
26+
.with(Attribute::Bold);
2227

2328
fn 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;

src/watch/state.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ use crate::{
2020

2121
use super::{terminal_event::terminal_event_handler, InputPauseGuard, WatchEvent};
2222

23+
const HEADING_ATTRIBUTES: Attributes = Attributes::none()
24+
.with(Attribute::Bold)
25+
.with(Attribute::Underlined);
26+
2327
#[derive(PartialEq, Eq)]
2428
enum DoneStatus {
2529
DoneWithSolution(String),
@@ -209,9 +213,7 @@ impl<'a> WatchState<'a> {
209213

210214
if self.show_hint {
211215
stdout
212-
.queue(SetAttributes(
213-
Attributes::from(Attribute::Bold).with(Attribute::Underlined),
214-
))?
216+
.queue(SetAttributes(HEADING_ATTRIBUTES))?
215217
.queue(SetForegroundColor(Color::Cyan))?;
216218
stdout.write_all(b"Hint")?;
217219
stdout.queue(ResetColor)?;

0 commit comments

Comments
 (0)