From 7d2bc1c7a4333de5460cb86a8dca5e5ecad2a643 Mon Sep 17 00:00:00 2001 From: mo8it Date: Wed, 28 Aug 2024 00:56:22 +0200 Subject: [PATCH] Use a Vec for the name col padding --- src/dev/check.rs | 4 +++- src/list/state.rs | 13 ++++++------- src/main.rs | 1 - 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/dev/check.rs b/src/dev/check.rs index 4c5e0728..a6db3c21 100644 --- a/src/dev/check.rs +++ b/src/dev/check.rs @@ -14,9 +14,11 @@ use crate::{ collections::{hash_set_with_capacity, HashSet}, exercise::{RunnableExercise, OUTPUT_CAPACITY}, info_file::{ExerciseInfo, InfoFile}, - CURRENT_FORMAT_VERSION, MAX_EXERCISE_NAME_LEN, + CURRENT_FORMAT_VERSION, }; +const MAX_EXERCISE_NAME_LEN: usize = 32; + // Find a char that isn't allowed in the exercise's `name` or `dir`. fn forbidden_char(input: &str) -> Option { input.chars().find(|c| !c.is_alphanumeric() && *c != '_') diff --git a/src/list/state.rs b/src/list/state.rs index e263b7ed..51e4cfa9 100644 --- a/src/list/state.rs +++ b/src/list/state.rs @@ -14,13 +14,11 @@ use crate::{ app_state::AppState, exercise::Exercise, term::{progress_bar, terminal_file_link, CountedWrite, MaxLenWriter}, - MAX_EXERCISE_NAME_LEN, }; use super::scroll_state::ScrollState; -// +1 for column padding. -const SPACE: &[u8] = &[b' '; MAX_EXERCISE_NAME_LEN + 1]; +const COL_SPACING: usize = 2; fn next_ln(stdout: &mut StdoutLock) -> io::Result<()> { stdout @@ -41,7 +39,7 @@ pub struct ListState<'a> { pub message: String, app_state: &'a mut AppState, scroll_state: ScrollState, - name_col_width: usize, + name_col_padding: Vec, filter: Filter, term_width: u16, term_height: u16, @@ -61,6 +59,7 @@ impl<'a> ListState<'a> { .map(|exercise| exercise.name.len()) .max() .map_or(name_col_title_len, |max| max.max(name_col_title_len)); + let name_col_padding = vec![b' '; name_col_width + COL_SPACING]; let filter = Filter::None; let n_rows_with_filter = app_state.exercises().len(); @@ -73,7 +72,7 @@ impl<'a> ListState<'a> { message: String::with_capacity(128), app_state, scroll_state, - name_col_width, + name_col_padding, filter, // Set by `set_term_size` term_width: 0, @@ -162,7 +161,7 @@ impl<'a> ListState<'a> { writer.stdout.queue(SetForegroundColor(Color::Reset))?; writer.write_str(exercise.name)?; - writer.write_ascii(&SPACE[..self.name_col_width + 2 - exercise.name.len()])?; + writer.write_ascii(&self.name_col_padding[exercise.name.len()..])?; terminal_file_link(&mut writer, exercise.path, Color::Blue)?; @@ -184,7 +183,7 @@ impl<'a> ListState<'a> { // Header let mut writer = MaxLenWriter::new(stdout, self.term_width as usize); writer.write_ascii(b" Current State Name")?; - writer.write_ascii(&SPACE[..self.name_col_width - 2])?; + writer.write_ascii(&self.name_col_padding[2..])?; writer.write_ascii(b"Path")?; next_ln(stdout)?; diff --git a/src/main.rs b/src/main.rs index e8f274b0..e53cd5a7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -25,7 +25,6 @@ mod term; mod watch; const CURRENT_FORMAT_VERSION: u8 = 1; -const MAX_EXERCISE_NAME_LEN: usize = 32; /// Rustlings is a collection of small exercises to get you used to writing and reading Rust code #[derive(Parser)]