Skip to content

Commit

Permalink
refactor(number-lines): add dynamic number lines width
Browse files Browse the repository at this point in the history
  • Loading branch information
metiftikci committed Oct 1, 2023
1 parent 600a683 commit 1b11983
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 8 deletions.
8 changes: 4 additions & 4 deletions src/buffer/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,23 @@ impl Buffer {
self.lines.get_mut(row).unwrap()
}

pub fn get_line_visible_text(&self, row: usize) -> String {
pub fn get_line_visible_text(&self, row: usize) -> Option<String> {
if self.lines.len() <= row {
return String::new();
return None;
}

let line = self.get_line(row);

let start_index = self.scroll.x;
if line.len() <= start_index {
return String::new();
return Some(String::new());
}
let end_index = min(
line.len(),
self.scroll.x + (self.area.width as usize),
);

line[start_index..end_index].to_string()
Some(line[start_index..end_index].to_string())
}

pub fn get_line_max_cursor_x(&self, row: usize) -> usize {
Expand Down
19 changes: 15 additions & 4 deletions src/palette.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ impl Palette {
let window = editor.get_active_window();
let mut palette = Palette::from(editor.size.height, editor.size.width);

palette.cursor.x = window.get_active_buffer_visible_x(window.get_active_buffer().cursor.x);
palette.cursor.x =
window.get_active_buffer_visible_x(window.get_active_buffer().cursor.x) + 5;
palette.cursor.y = window.get_active_buffer_visible_y(window.get_active_buffer().cursor.y);

let buffer = window.get_active_buffer();
Expand All @@ -88,10 +89,20 @@ impl Palette {
None => palette.print(0, 0, &String::from("[No Name]")),
}

for y in 0..buffer.area.height {
let line = buffer.get_line_visible_text(buffer.scroll.y + y as usize);
let number_column_width = buffer.lines.len().to_string().len();

palette.print(y + 1, 0, &line);
for y in 0..buffer.area.height {
let row_index = buffer.scroll.y + y as usize;
match buffer.get_line_visible_text(row_index) {
Some(text) => {
palette.print(
y + 1,
0,
&format!(" {:>2$} {}", row_index + 1, text, number_column_width),
);
}
None => palette.print(y + 1, 0, &format!("~")),
}
}

palette.print(palette.size.height - 1, 0, &format!("{}", buffer.mode));
Expand Down

0 comments on commit 1b11983

Please sign in to comment.