emma
/
breed
Archived
forked from mars/breed
1
0
Fork 0

Selected line# and cursorline styling

This commit is contained in:
mars 2023-04-12 21:25:12 -04:00
parent b29891571b
commit 547dea6ac0
3 changed files with 29 additions and 4 deletions

View File

@ -74,15 +74,17 @@ impl Buffer {
cols: u16,
rows: u16,
scroll: Cursor,
cursor: Cursor,
out: &mut impl Write,
) -> crossterm::Result<u32> {
self.parse();
let mut styles = self.styles.lock();
let linenr_style = styles.get_scope("ui.linenr");
let linenr_style = styles.get_scope("ui.linenr").clone();
let linenr_width = self.text.len_lines().ilog10() + 1;
let gutter_width = linenr_width + 1;
let text_width = cols as usize - gutter_width as usize;
let line_padding: String = std::iter::repeat(' ').take(text_width).collect();
out.queue(cursor::MoveTo(0, 0))?;
@ -94,22 +96,40 @@ impl Buffer {
}
let row = row as usize + scroll.line;
let is_selected = row == cursor.line;
let linenr_style = if is_selected {
styles.get_scope("ui.linenr.selected").clone()
} else {
linenr_style
};
let line_style = if is_selected {
styles.get_scope("ui.cursorline.primary").clone()
} else {
Default::default()
};
let linenr = format!("{:width$} ", row, width = linenr_width as usize);
linenr_style.print_styled(out, &linenr)?;
let lhs = scroll.column;
let width = line.len_chars();
let mut remaining = text_width;
line_buf.clear();
if lhs < width {
let window = text_width.min(width - lhs);
let rhs = lhs + window;
let styled = &self.styled[row];
for (style, range) in styled.iter() {
for (chunk_style, range) in styled.iter() {
let range = range.start.max(lhs)..range.end.min(rhs);
if range.start < range.end {
// this range is in view so display
let content = line.slice(range.clone());
let mut style = line_style.clone();
style.apply(chunk_style);
style.print_styled(&mut line_buf, content)?;
remaining = text_width.saturating_sub(range.end);
}
if range.end >= rhs {
@ -119,6 +139,9 @@ impl Buffer {
}
}
let padding = &line_padding[0..remaining];
line_style.print_styled(&mut line_buf, padding)?;
out.write_all(&line_buf)?;
out.queue(cursor::MoveToNextLine(1))?;
}

View File

@ -170,7 +170,7 @@ impl State {
// draw buffer
let buffer_rows = if show_status_bar { rows - 1 } else { rows };
let lr_width = self.buffer.draw(cols, buffer_rows, self.scroll, out)?;
let lr_width = self.buffer.draw(cols, buffer_rows, self.scroll, self.cursor, out)?;
// draw cursor
let cursor_pos = set_cursor_pos.unwrap_or_else(|| {

View File

@ -1,4 +1,6 @@
"ui.linenr" = "gray"
"ui.cursorline" = {}
"ui.linenr" = "black"
"ui.linenr.selected" = "white"
"error" = "red"