Skip to content

Commit

Permalink
feat: add style for whitespaces
Browse files Browse the repository at this point in the history
  • Loading branch information
metiftikci committed Oct 26, 2023
1 parent ffbb37f commit 6d0de45
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 20 deletions.
16 changes: 0 additions & 16 deletions src/buffer/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,3 @@ impl Default for BufferOptions {
options
}
}

impl BufferOptions {
pub fn replace_chars(&self, text: &str) -> String {
let mut result = String::new();

for c in text.chars() {
if let Some(m) = self.chars.get(&c) {
result.push(m.clone());
} else {
result.push(c);
}
}

result
}
}
6 changes: 3 additions & 3 deletions src/screen/print_buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,11 +109,11 @@ impl Screen {
Style::new(T.info_column_fg, T.info_column_bg),
);
}
self.print_text(
self.print_buffer_text(
&buffer.options.chars,
buffer.area.y + y,
buffer.text_area.x,
&buffer.options.replace_chars(&text),
Style::new(T.text_fg, T.text_bg),
&text,
);
}
Err(_) => {
Expand Down
29 changes: 28 additions & 1 deletion src/screen/print_text.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use crate::{core::style::Style, screen::Screen};
use std::collections::HashMap;

use crate::{core::style::Style, screen::Screen, theme::THEME_ONE as T};

impl Screen {
pub fn print_text(&mut self, row: u16, column: u16, text: &str, style: Style) {
Expand All @@ -13,4 +15,29 @@ impl Screen {
column_index += 1;
}
}

pub fn print_buffer_text(
&mut self,
replace_chars: &HashMap<char, char>,
row: u16,
column: u16,
text: &str,
) {
let columns = self.rows.get_mut(row as usize).unwrap();
let mut column_index = column as usize;
for c in text.chars() {
let cell = columns.get_mut(column_index).unwrap();
cell.char = if let Some(replace_char) = replace_chars.get(&c) {
replace_char.clone()
} else {
c
};
cell.style = if c == ' ' {
Style::new(T.text_whitespace_fg, T.text_whitespace_bg)
} else {
Style::new(T.text_fg, T.text_bg)
};
column_index += 1;
}
}
}
4 changes: 4 additions & 0 deletions src/theme.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ pub struct Theme {
pub text_bg: Color,
pub text_selected_fg: Color,
pub text_selected_bg: Color,
pub text_whitespace_fg: Color,
pub text_whitespace_bg: Color,
pub text_finded_fg: Color,
pub text_finded_bg: Color,

Expand Down Expand Up @@ -61,6 +63,8 @@ pub const THEME_ONE: Theme = Theme {
text_bg: SILVER,
text_selected_fg: WHITE,
text_selected_bg: GRAY,
text_whitespace_fg: LIGHT_GRAY,
text_whitespace_bg: SILVER,
text_finded_fg: RED,
text_finded_bg: GRAY,

Expand Down

0 comments on commit 6d0de45

Please sign in to comment.