Refactor buffer rendering

This commit is contained in:
Greg Shuflin committed 2024-03-05 11:32:29 -08:00
1 parent 662ed9ca6c
commit 0046e5d472
1 file changed
+47 -36
+47 -36
View File
@@ -60,53 +60,64 @@ impl TerminalState {
let line_number_style = line_number_style(&core);
let active_buffer = core.active_buffer();
let expand_tab = active_buffer.buffer_settings.expand_tab;
let terminal_buf_height = rect.height;
let cursor = &active_buffer.cursor_pos;
// TODO do this more efficiently
let start = 0 as usize;
let end = start + (terminal_buf_height as usize);
let raw_lines = active_buffer.visible_lines(start, end);
let text = render_single_buffer(
active_buffer,
terminal_buf_height,
cursor_style,
line_number_style,
);
frame.render_widget(Paragraph::new(text).white().on_black(), rect);
let mut text = vec![];
for (n, line) in raw_lines.iter().enumerate() {
let line_number = Span::styled(format!("{} ", n + 1), line_number_style);
if n == cursor.y() as usize {
let x = cursor.x() as usize;
fn render_single_buffer<'a>(
active_buffer: &TextBuffer,
terminal_buf_height: u16,
cursor_style: Style,
line_number_style: Style,
) -> Vec<Line<'a>> {
let cursor = active_buffer.cursor_pos;
let expand_tab = active_buffer.buffer_settings.expand_tab;
let screen_x = if x >= line.len() { line.len() } else { x };
// TODO do this more efficiently
let start = 0 as usize;
let end = start + (terminal_buf_height as usize);
let raw_lines = active_buffer.visible_lines(start, end);
let len = raw_lines.len();
let cursor = Span::styled(" ", cursor_style);
let mut text = vec![];
for (n, line) in raw_lines.into_iter().enumerate() {
let line_number = Span::styled(format!("{} ", n + 1), line_number_style);
if n == cursor.y() as usize {
let x = cursor.x() as usize;
//TODO utf-8 issues
let (precursor, postcursor) = line.split_at(screen_x);
let screen_x = if x >= line.len() { line.len() } else { x };
let rendered_precursor = process_line(precursor, expand_tab);
let rendered_postcursor = process_line(postcursor, expand_tab);
let cursor = Span::styled(" ", cursor_style);
text.push(Line::from(vec![
line_number,
rendered_precursor.into(),
cursor,
rendered_postcursor.to_owned().into(),
]));
} else {
text.push(Line::from(vec![line_number, line.into()]));
//TODO utf-8 issues
let (precursor, postcursor) = line.split_at(screen_x);
let rendered_precursor = process_line(precursor, expand_tab);
let rendered_postcursor = process_line(postcursor, expand_tab);
text.push(Line::from(vec![
line_number,
rendered_precursor.into(),
cursor,
rendered_postcursor.to_owned().into(),
]));
} else {
text.push(Line::from(vec![line_number, line.into()]));
}
}
}
if terminal_buf_height as usize > raw_lines.len() {
for _ in 0..(terminal_buf_height as usize) - raw_lines.len() {
text.push(Span::styled("-", line_number_style).into())
if terminal_buf_height as usize > len {
for _ in 0..(terminal_buf_height as usize) - len {
text.push(Span::styled("-", line_number_style).into())
}
}
}
render_single_buffer(frame, rect, text);
fn render_single_buffer(frame: &mut Frame, rect: Rect, text: Vec<Line>) {
frame.render_widget(Paragraph::new(text).white().on_black(), rect);
text
}
}