render cursor position properly after tabs

This commit is contained in:
Crispy 2023-02-26 01:02:10 +01:00
parent 10a56c19d9
commit c656dc0931
2 changed files with 17 additions and 5 deletions

2
.gitignore vendored
View file

@ -1,2 +1,2 @@
/target
err
err.log

View file

@ -9,6 +9,8 @@ use termion::{
raw::IntoRawMode,
};
const TAB_SIZE: usize = 4;
pub struct Editor {
text: String,
lines: Vec<Line>,
@ -100,8 +102,8 @@ impl Editor {
}
}
fn current_line(&self) -> &Line {
self.lines.get(self.cursor.line).unwrap_or(&(0..0))
fn current_line(&self) -> Line {
self.lines.get(self.cursor.line).unwrap_or(&(0..0)).clone()
}
fn find_lines(&mut self) {
@ -124,12 +126,15 @@ impl Editor {
print!(
"{}{}",
cursor::Goto(1, row as u16 + 1),
text.replace('\t', " ")
text.replace('\t', &" ".repeat(TAB_SIZE))
);
}
print!(
"{}",
cursor::Goto(self.cursor.column as u16 + 1, self.cursor.line as u16 + 1)
cursor::Goto(
self.physical_column() as u16 + 1,
self.cursor.line as u16 + 1
)
);
stdout().flush().unwrap();
}
@ -154,4 +159,11 @@ impl Editor {
fn index(&self) -> usize {
self.current_line().start + self.cursor.column
}
fn physical_column(&self) -> usize {
let start = self.current_line().start;
let end = self.current_line().start + self.cursor.column;
let preceding_tabs = self.text[start..end].chars().filter(|&c| c == '\t').count();
self.cursor.column + preceding_tabs * (TAB_SIZE - 1)
}
}