Use alternating gutter colors for lines of text.

This makes for a clearer visual distinction of where soft line
wrapping occurs.
This commit is contained in:
Nathan Vegdahl 2020-02-15 11:43:53 +09:00
parent 9512018983
commit 8c1778f674

View File

@ -52,16 +52,28 @@ const STYLE_CURSOR: Style = Style(
b: 0xD0, b: 0xD0,
}, },
); );
const STYLE_GUTTER: Style = Style( const STYLE_GUTTER_ODD: Style = Style(
Color::Rgb { Color::Rgb {
r: 0x70, r: 0x70,
g: 0x70, g: 0x70,
b: 0x70, b: 0x70,
}, },
Color::Rgb { Color::Rgb {
r: 0x22, r: 0x08,
g: 0x22, g: 0x08,
b: 0x22, b: 0x08,
},
);
const STYLE_GUTTER_EVEN: Style = Style(
Color::Rgb {
r: 0x78,
g: 0x78,
b: 0x78,
},
Color::Rgb {
r: 0x1B,
g: 0x1B,
b: 0x1B,
}, },
); );
const STYLE_INFO: Style = Style( const STYLE_INFO: Style = Style(
@ -498,6 +510,7 @@ impl TermUI {
) { ) {
// Calculate all the starting info // Calculate all the starting info
let gutter_width = editor.editor_dim.1 - editor.view_dim.1; let gutter_width = editor.editor_dim.1 - editor.view_dim.1;
let blank_gutter = &" "[..gutter_width - 1];
let (line_index, col_i) = editor.buffer.index_to_line_col(editor.view_pos.0); let (line_index, col_i) = editor.buffer.index_to_line_col(editor.view_pos.0);
let temp_line = editor.buffer.get_line(line_index); let temp_line = editor.buffer.get_line(line_index);
let (mut line_block_index, block_range) = block_index_and_range(&temp_line, col_i); let (mut line_block_index, block_range) = block_index_and_range(&temp_line, col_i);
@ -512,20 +525,33 @@ impl TermUI {
// Fill in the gutter with the appropriate background // Fill in the gutter with the appropriate background
for y in c1.0..(c2.0 + 1) { for y in c1.0..(c2.0 + 1) {
for x in c1.1..(c1.1 + gutter_width - 1) { self.screen.draw(c1.1, y, blank_gutter, STYLE_GUTTER_ODD);
self.screen.draw(x, y, " ", STYLE_GUTTER);
}
} }
let mut line_num = line_index + 1; let mut line_num = line_index + 1;
for line in editor.buffer.line_iter_at_index(line_index) { for line in editor.buffer.line_iter_at_index(line_index) {
let gutter_style = if (line_num % 2) == 0 {
STYLE_GUTTER_EVEN
} else {
STYLE_GUTTER_ODD
};
// Print line number // Print line number
if line_block_index == 0 { if line_block_index == 0 {
let lnx = c1.1 + (gutter_width - 2 - digit_count(line_num as u32, 10) as usize); let lnx = c1.1;
let lny = screen_line as usize; let lny = screen_line as usize;
if lny >= c1.0 && lny <= c2.0 { if lny >= c1.0 && lny <= c2.0 {
self.screen self.screen.draw(
.draw(lnx, lny, &format!("{}", line_num)[..], STYLE_GUTTER); lnx,
lny,
&format!(
"{}{} ",
&blank_gutter
[..(gutter_width - 2 - digit_count(line_num as u32, 10) as usize)],
line_num,
)[..],
gutter_style,
);
} }
} }
@ -541,6 +567,8 @@ impl TermUI {
let mut lines_traversed: usize = 0; let mut lines_traversed: usize = 0;
loop { loop {
for (g, (pos_y, pos_x), width) in g_iter { for (g, (pos_y, pos_x), width) in g_iter {
let do_gutter =
last_pos_y != pos_y || (lines_traversed == 0 && line_block_index != 0);
if last_pos_y != pos_y { if last_pos_y != pos_y {
if last_pos_y < pos_y { if last_pos_y < pos_y {
lines_traversed += pos_y - last_pos_y; lines_traversed += pos_y - last_pos_y;
@ -556,6 +584,11 @@ impl TermUI {
return; return;
} }
if do_gutter {
self.screen
.draw(c1.1, py as usize, blank_gutter, gutter_style);
}
// Draw the grapheme to the screen if it's in bounds // Draw the grapheme to the screen if it's in bounds
if (px >= c1.1 as isize) && (py >= c1.0 as isize) && (px <= c2.1 as isize) { if (px >= c1.1 as isize) && (py >= c1.0 as isize) && (px <= c2.1 as isize) {
// Check if the character is within a cursor // Check if the character is within a cursor