diff --git a/config_example.ini b/config_example.ini index 633a2435..669dbdd3 100644 --- a/config_example.ini +++ b/config_example.ini @@ -6,7 +6,7 @@ tab_stop = 4 quit_times = 2 # The duration for which messages are shown in the status bar, in seconds. -message_duration = 3 +msg_duration = 3 # Whether to display line numbers. show_line_numbers = true diff --git a/src/config.rs b/src/config.rs index 6d079bbf..ffc3fd75 100644 --- a/src/config.rs +++ b/src/config.rs @@ -12,28 +12,22 @@ use crate::{sys::conf_dirs as cdirs, Error, Error::Config as ConfErr}; #[derive(Debug, PartialEq, Eq)] pub struct Config { /// The size of a tab. Must be > 0. - pub tab_stop: usize, + pub t_stop: usize, /// The number of confirmations needed before quitting, when changes have been made since the /// file was last changed. - pub quit_times: usize, + pub q_times: usize, /// The duration for which messages are shown in the status bar. - pub message_dur: Duration, + pub msg_dur: Duration, /// Whether to display line numbers. - pub show_line_num: bool, + pub show_ln: bool, /// Whether to display nonprintable characters. - pub non_printable: bool, + pub non_p: bool, } impl Default for Config { /// Default configuration. fn default() -> Self { - Self { - tab_stop: 4, - quit_times: 2, - message_dur: Duration::new(3, 0), - show_line_num: true, - non_printable: false, - } + Self { t_stop: 4, q_times: 2, msg_dur: Duration::new(3, 0), show_ln: true, non_p: false } } } @@ -52,26 +46,23 @@ impl Config { /// Will return `Err` if one of the configuration file cannot be parsed properly. pub fn load() -> Result { let mut conf = Self::default(); - let paths: Vec<_> = cdirs().iter().map(|d| PathBuf::from(d).join("config.ini")).collect(); - for path in paths.iter().filter(|p| p.is_file()).rev() { process_ini_file(path, &mut |key, value| { match key { "tab_stop" => match parse_value(value)? { 0 => return Err("tab_stop must be > 0".into()), - tab_stop => conf.tab_stop = tab_stop, + t_stop => conf.t_stop = t_stop, }, - "quit_times" => conf.quit_times = parse_value(value)?, - "message_duration" => - conf.message_dur = Duration::from_secs_f32(parse_value(value)?), - "show_line_numbers" => conf.show_line_num = parse_value(value)?, + "quit_times" => conf.q_times = parse_value(value)?, + "msg_duration" => conf.msg_dur = Duration::from_secs_f32(parse_value(value)?), + "show_line_numbers" => conf.show_ln = parse_value(value)?, + "non_printable" => conf.non_p = parse_value(value)?, _ => return Err(format!("Invalid key: {key}")), }; Ok(()) })?; } - Ok(conf) } } @@ -227,12 +218,12 @@ mod tests { } fn test_config_dir(env_key: &OsStr, env_val: &OsStr, kibi_config_home: &Path) { - let custom_config = Config { tab_stop: 99, quit_times: 50, ..Config::default() }; + let custom_config = Config { t_stop: 99, q_times: 50, ..Config::default() }; let ini_content = format!( "# Configuration file tab_stop = {} quit_times={}", - custom_config.tab_stop, custom_config.quit_times + custom_config.t_stop, custom_config.q_times ); fs::create_dir_all(kibi_config_home).unwrap(); diff --git a/src/editor.rs b/src/editor.rs index a9acbac6..45565d44 100644 --- a/src/editor.rs +++ b/src/editor.rs @@ -171,7 +171,7 @@ impl Editor { pub fn new(config: Config) -> Result { sys::register_winsize_change_signal_handler()?; let mut editor = Self::default(); - (editor.quit_times, editor.config) = (config.quit_times, config); + (editor.quit_times, editor.config) = (config.q_times, config); // Enable raw mode and store the original (non-raw) terminal mode. editor.orig_term_mode = Some(sys::enable_raw_mode()?); @@ -297,7 +297,7 @@ impl Editor { // computed below using `successors`. let n_digits = successors(Some(self.rows.len()), |u| Some(u / 10).filter(|u| *u > 0)).count(); - let show_line_num = self.config.show_line_num && n_digits + 2 < self.window_width / 4; + let show_line_num = self.config.show_ln && n_digits + 2 < self.window_width / 4; self.ln_pad = if show_line_num { n_digits + 2 } else { 0 }; self.screen_cols = self.window_width.saturating_sub(self.ln_pad); } @@ -320,7 +320,7 @@ impl Editor { let mut hl_state = if y > 0 { self.rows[y - 1].hl_state } else { HlState::Normal }; for row in self.rows.iter_mut().skip(y) { let previous_hl_state = row.hl_state; - hl_state = row.update(&self.syntax, self.non_printable, hl_state, self.config.tab_stop); + hl_state = row.update(&self.syntax, self.non_printable, hl_state, self.config.t_stop); if ignore_following_rows || hl_state == previous_hl_state { return; } @@ -333,7 +333,7 @@ impl Editor { fn update_all_rows(&mut self) { let mut hl_state = HlState::Normal; for row in &mut self.rows { - hl_state = row.update(&self.syntax, self.non_printable, hl_state, self.config.tab_stop); + hl_state = row.update(&self.syntax, self.non_printable, hl_state, self.config.t_stop); } } @@ -568,7 +568,7 @@ impl Editor { /// Draw the message bar on the terminal, by adding characters to the buffer. fn draw_message_bar(&self, buffer: &mut String) { buffer.push_str(CLEAR_LINE_RIGHT_OF_CURSOR); - let msg_duration = self.config.message_dur; + let msg_duration = self.config.msg_dur; if let Some(sm) = self.status_msg.as_ref().filter(|sm| sm.time.elapsed() < msg_duration) { buffer.push_str(&sm.msg[..sm.msg.len().min(self.window_width)]); } @@ -598,7 +598,7 @@ impl Editor { /// should exit, and optionally the prompt mode to switch to. fn process_keypress(&mut self, key: &Key) -> (bool, Option) { // This won't be mutated, unless key is Key::Character(EXIT) - let mut quit_times = self.config.quit_times; + let mut quit_times = self.config.q_times; let mut prompt_mode = None; match key { diff --git a/src/row.rs b/src/row.rs index 970c4ae8..803ecc22 100644 --- a/src/row.rs +++ b/src/row.rs @@ -66,15 +66,10 @@ impl Row { let n_rend_chars = if c == '\t' { tab - (rx % tab) } else { c.width().unwrap_or(1) }; self.render.push_str( &(if c == '\t' && non_printable { - let mut str; - if n_rend_chars > 1 { - str = String::from('├'); - str.push_str(&"─".repeat(n_rend_chars - 2)); - str.push('┤'); - } else { - str = String::from('─'); + match n_rend_chars > 1 { + true => format!("├{}┤", &"─".repeat(n_rend_chars - 2)), + false => String::from('─'), } - str } else if c == '\t' { " ".repeat(n_rend_chars) } else { @@ -142,7 +137,6 @@ impl Row { } } } - let c = line[i]; // At this point, hl_state is Normal or String @@ -159,7 +153,6 @@ impl Row { self.hl.push(HlType::String); continue; } - let prev_sep = (i == 0) || is_sep(line[i - 1]); if syntax.highlight_numbers @@ -181,7 +174,6 @@ impl Row { } } } - self.hl.push(HlType::Normal); } @@ -199,30 +191,15 @@ impl Row { ) -> Result<(), Error> { let mut current_hl_type = HlType::Normal; let chars = self.render.chars().skip(offset).take(max_len); - let mut rx = self.render.chars().take(offset).map(|c| c.width().unwrap_or(1)).sum(); let mut rendered_char; + let mut rx = self.render.chars().take(offset).map(|c| c.width().unwrap_or(1)).sum(); for (c, mut hl_type) in chars.zip(self.hl.iter().skip(offset)) { if c.is_ascii_control() { - if !non_printable { - if (c as u8) <= 26 { - rendered_char = (b'@' + c as u8) as char - } else { - rendered_char = '?' - } + if (c as u8) <= 26 { + rendered_char = (b'@' + c as u8) as char } else { - match c { - '\x0D' => rendered_char = '␍', - // null - '\x00' => rendered_char = '␀', - // bell - '\x07' => rendered_char = '␇', - // backspace - '\x08' => rendered_char = '␈', - // escape - '\x1B' => rendered_char = '␛', - _ => rendered_char = '?', - } - }; + rendered_char = '?' + } write!(buffer, "{REVERSE_VIDEO}{rendered_char}{RESET_FMT}")?; // Restore previous color if current_hl_type != HlType::Normal { @@ -243,16 +220,15 @@ impl Row { current_hl_type = *hl_type; } if c.is_ascii_whitespace() && non_printable { - rendered_char = '·'; + buffer.push('·'); } else { - rendered_char = c; + buffer.push(c); } - buffer.push(rendered_char); } rx += c.width().unwrap_or(1); } if non_printable { - buffer.push('␊') + buffer.push('␊'); } buffer.push_str(RESET_FMT); Ok(())