Skip to content

Commit

Permalink
Reduced the number of lines
Browse files Browse the repository at this point in the history
I tried to reduce the number of lines. I realised that the control characters are actually printed by Kibi already in a way (I may be wrong). Now the only non-printable characters that will be rendered are LF, whitespace and tab.
  • Loading branch information
RaresCon committed Jan 28, 2023
1 parent a2610a5 commit 6a74bfe
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 64 deletions.
2 changes: 1 addition & 1 deletion config_example.ini
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
35 changes: 13 additions & 22 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
}
}

Expand All @@ -52,26 +46,23 @@ impl Config {
/// Will return `Err` if one of the configuration file cannot be parsed properly.
pub fn load() -> Result<Self, Error> {
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)
}
}
Expand Down Expand Up @@ -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();
Expand Down
12 changes: 6 additions & 6 deletions src/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ impl Editor {
pub fn new(config: Config) -> Result<Self, Error> {
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()?);
Expand Down Expand Up @@ -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);
}
Expand All @@ -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;
}
Expand All @@ -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);
}
}

Expand Down Expand Up @@ -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)]);
}
Expand Down Expand Up @@ -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<PromptMode>) {
// 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 {
Expand Down
46 changes: 11 additions & 35 deletions src/row.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -142,7 +137,6 @@ impl Row {
}
}
}

let c = line[i];

// At this point, hl_state is Normal or String
Expand All @@ -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
Expand All @@ -181,7 +174,6 @@ impl Row {
}
}
}

self.hl.push(HlType::Normal);
}

Expand All @@ -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 {
Expand All @@ -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(())
Expand Down

0 comments on commit 6a74bfe

Please sign in to comment.