render cursor position properly after tabs
This commit is contained in:
parent
10a56c19d9
commit
c656dc0931
2 changed files with 17 additions and 5 deletions
2
.gitignore
vendored
2
.gitignore
vendored
|
@ -1,2 +1,2 @@
|
||||||
/target
|
/target
|
||||||
err
|
err.log
|
||||||
|
|
|
@ -9,6 +9,8 @@ use termion::{
|
||||||
raw::IntoRawMode,
|
raw::IntoRawMode,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const TAB_SIZE: usize = 4;
|
||||||
|
|
||||||
pub struct Editor {
|
pub struct Editor {
|
||||||
text: String,
|
text: String,
|
||||||
lines: Vec<Line>,
|
lines: Vec<Line>,
|
||||||
|
@ -100,8 +102,8 @@ impl Editor {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn current_line(&self) -> &Line {
|
fn current_line(&self) -> Line {
|
||||||
self.lines.get(self.cursor.line).unwrap_or(&(0..0))
|
self.lines.get(self.cursor.line).unwrap_or(&(0..0)).clone()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn find_lines(&mut self) {
|
fn find_lines(&mut self) {
|
||||||
|
@ -124,12 +126,15 @@ impl Editor {
|
||||||
print!(
|
print!(
|
||||||
"{}{}",
|
"{}{}",
|
||||||
cursor::Goto(1, row as u16 + 1),
|
cursor::Goto(1, row as u16 + 1),
|
||||||
text.replace('\t', " ")
|
text.replace('\t', &" ".repeat(TAB_SIZE))
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
print!(
|
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();
|
stdout().flush().unwrap();
|
||||||
}
|
}
|
||||||
|
@ -154,4 +159,11 @@ impl Editor {
|
||||||
fn index(&self) -> usize {
|
fn index(&self) -> usize {
|
||||||
self.current_line().start + self.cursor.column
|
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)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue