Skip to content

Commit 7d2bc1c

Browse files
committed
Use a Vec for the name col padding
1 parent c209c87 commit 7d2bc1c

File tree

3 files changed

+9
-9
lines changed

3 files changed

+9
-9
lines changed

src/dev/check.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,11 @@ use crate::{
1414
collections::{hash_set_with_capacity, HashSet},
1515
exercise::{RunnableExercise, OUTPUT_CAPACITY},
1616
info_file::{ExerciseInfo, InfoFile},
17-
CURRENT_FORMAT_VERSION, MAX_EXERCISE_NAME_LEN,
17+
CURRENT_FORMAT_VERSION,
1818
};
1919

20+
const MAX_EXERCISE_NAME_LEN: usize = 32;
21+
2022
// Find a char that isn't allowed in the exercise's `name` or `dir`.
2123
fn forbidden_char(input: &str) -> Option<char> {
2224
input.chars().find(|c| !c.is_alphanumeric() && *c != '_')

src/list/state.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,11 @@ use crate::{
1414
app_state::AppState,
1515
exercise::Exercise,
1616
term::{progress_bar, terminal_file_link, CountedWrite, MaxLenWriter},
17-
MAX_EXERCISE_NAME_LEN,
1817
};
1918

2019
use super::scroll_state::ScrollState;
2120

22-
// +1 for column padding.
23-
const SPACE: &[u8] = &[b' '; MAX_EXERCISE_NAME_LEN + 1];
21+
const COL_SPACING: usize = 2;
2422

2523
fn next_ln(stdout: &mut StdoutLock) -> io::Result<()> {
2624
stdout
@@ -41,7 +39,7 @@ pub struct ListState<'a> {
4139
pub message: String,
4240
app_state: &'a mut AppState,
4341
scroll_state: ScrollState,
44-
name_col_width: usize,
42+
name_col_padding: Vec<u8>,
4543
filter: Filter,
4644
term_width: u16,
4745
term_height: u16,
@@ -61,6 +59,7 @@ impl<'a> ListState<'a> {
6159
.map(|exercise| exercise.name.len())
6260
.max()
6361
.map_or(name_col_title_len, |max| max.max(name_col_title_len));
62+
let name_col_padding = vec![b' '; name_col_width + COL_SPACING];
6463

6564
let filter = Filter::None;
6665
let n_rows_with_filter = app_state.exercises().len();
@@ -73,7 +72,7 @@ impl<'a> ListState<'a> {
7372
message: String::with_capacity(128),
7473
app_state,
7574
scroll_state,
76-
name_col_width,
75+
name_col_padding,
7776
filter,
7877
// Set by `set_term_size`
7978
term_width: 0,
@@ -162,7 +161,7 @@ impl<'a> ListState<'a> {
162161
writer.stdout.queue(SetForegroundColor(Color::Reset))?;
163162

164163
writer.write_str(exercise.name)?;
165-
writer.write_ascii(&SPACE[..self.name_col_width + 2 - exercise.name.len()])?;
164+
writer.write_ascii(&self.name_col_padding[exercise.name.len()..])?;
166165

167166
terminal_file_link(&mut writer, exercise.path, Color::Blue)?;
168167

@@ -184,7 +183,7 @@ impl<'a> ListState<'a> {
184183
// Header
185184
let mut writer = MaxLenWriter::new(stdout, self.term_width as usize);
186185
writer.write_ascii(b" Current State Name")?;
187-
writer.write_ascii(&SPACE[..self.name_col_width - 2])?;
186+
writer.write_ascii(&self.name_col_padding[2..])?;
188187
writer.write_ascii(b"Path")?;
189188
next_ln(stdout)?;
190189

src/main.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ mod term;
2525
mod watch;
2626

2727
const CURRENT_FORMAT_VERSION: u8 = 1;
28-
const MAX_EXERCISE_NAME_LEN: usize = 32;
2928

3029
/// Rustlings is a collection of small exercises to get you used to writing and reading Rust code
3130
#[derive(Parser)]

0 commit comments

Comments
 (0)