ui/CellBuffer: change row_iter() bounds to Range

Writing a range x..y is more ergonomic than (x, y+ 1)
async
Manos Pitsidianakis 2019-12-12 11:04:14 +02:00
parent 651fda1467
commit b401b64f35
Signed by: Manos Pitsidianakis
GPG Key ID: 73627C2F690DF710
4 changed files with 43 additions and 55 deletions

View File

@ -157,7 +157,7 @@ impl ListingTrait for CompactListing {
let (upper_left, bottom_right) = area; let (upper_left, bottom_right) = area;
change_colors(grid, area, fg_color, bg_color); change_colors(grid, area, fg_color, bg_color);
let mut x = get_x(upper_left) let x = get_x(upper_left)
+ self.data_columns.widths[0] + self.data_columns.widths[0]
+ self.data_columns.widths[1] + self.data_columns.widths[1]
+ self.data_columns.widths[2] + self.data_columns.widths[2]
@ -172,9 +172,8 @@ impl ListingTrait for CompactListing {
pos_dec(self.data_columns.columns[3].size(), (1, 1)), pos_dec(self.data_columns.columns[3].size(), (1, 1)),
), ),
); );
for _ in 0..self.data_columns.widths[3] { for c in grid.row_iter(x..(self.data_columns.widths[3] + x), get_y(upper_left)) {
grid[(x, get_y(upper_left))].set_bg(bg_color); grid[c].set_bg(bg_color);
x += 1;
} }
return; return;
} }
@ -280,9 +279,10 @@ impl ListingTrait for CompactListing {
let remainder = width let remainder = width
.saturating_sub(self.data_columns.widths[0]) .saturating_sub(self.data_columns.widths[0])
.saturating_sub(self.data_columns.widths[1]) .saturating_sub(self.data_columns.widths[1])
- 4; .saturating_sub(4);
self.data_columns.widths[2] = remainder / 6; self.data_columns.widths[2] = remainder / 6;
self.data_columns.widths[4] = (2 * remainder) / 3 - self.data_columns.widths[3]; self.data_columns.widths[4] =
((2 * remainder) / 3).saturating_sub(self.data_columns.widths[3]);
} else { } else {
let remainder = width let remainder = width
.saturating_sub(self.data_columns.widths[0]) .saturating_sub(self.data_columns.widths[0])
@ -290,7 +290,7 @@ impl ListingTrait for CompactListing {
.saturating_sub(8); .saturating_sub(8);
if min_col_width + self.data_columns.widths[4] > remainder { if min_col_width + self.data_columns.widths[4] > remainder {
self.data_columns.widths[4] = self.data_columns.widths[4] =
remainder - min_col_width - self.data_columns.widths[3]; remainder.saturating_sub(min_col_width + self.data_columns.widths[3]);
self.data_columns.widths[2] = min_col_width; self.data_columns.widths[2] = min_col_width;
} }
} }
@ -980,7 +980,7 @@ impl CompactListing {
((0, idx), (min_width.0, idx)), ((0, idx), (min_width.0, idx)),
None, None,
); );
for c in columns[0].row_iter((x, min_width.0.saturating_sub(1)), idx) { for c in columns[0].row_iter(x..min_width.0, idx) {
columns[0][c].set_bg(bg_color); columns[0][c].set_bg(bg_color);
} }
let (x, _) = write_string_to_grid( let (x, _) = write_string_to_grid(
@ -992,7 +992,7 @@ impl CompactListing {
((0, idx), (min_width.1.saturating_sub(1), idx)), ((0, idx), (min_width.1.saturating_sub(1), idx)),
None, None,
); );
for c in columns[1].row_iter((x, min_width.1.saturating_sub(1)), idx) { for c in columns[1].row_iter(x..min_width.1, idx) {
columns[1][c].set_bg(bg_color); columns[1][c].set_bg(bg_color);
} }
let (x, _) = write_string_to_grid( let (x, _) = write_string_to_grid(
@ -1004,7 +1004,7 @@ impl CompactListing {
((0, idx), (min_width.2, idx)), ((0, idx), (min_width.2, idx)),
None, None,
); );
for c in columns[2].row_iter((x, min_width.2.saturating_sub(1)), idx) { for c in columns[2].row_iter(x..min_width.2, idx) {
columns[2][c].set_bg(bg_color); columns[2][c].set_bg(bg_color);
} }
let (x, _) = write_string_to_grid( let (x, _) = write_string_to_grid(
@ -1016,7 +1016,7 @@ impl CompactListing {
((0, idx), (min_width.3, idx)), ((0, idx), (min_width.3, idx)),
None, None,
); );
for c in columns[3].row_iter((x, min_width.3.saturating_sub(1)), idx) { for c in columns[3].row_iter(x..min_width.3, idx) {
columns[3][c].set_bg(bg_color); columns[3][c].set_bg(bg_color);
} }
let (x, _) = write_string_to_grid( let (x, _) = write_string_to_grid(
@ -1040,25 +1040,25 @@ impl CompactListing {
((x + 1, idx), (min_width.4, idx)), ((x + 1, idx), (min_width.4, idx)),
None, None,
); );
for c in columns[4].row_iter((x, x), idx) { for c in columns[4].row_iter(x..(x + 1), idx) {
columns[4][c].set_bg(color); columns[4][c].set_bg(color);
} }
for c in columns[4].row_iter((_x, _x), idx) { for c in columns[4].row_iter(_x..(_x + 1), idx) {
columns[4][c].set_bg(color); columns[4][c].set_bg(color);
columns[4][c].set_keep_bg(true); columns[4][c].set_keep_bg(true);
} }
for c in columns[4].row_iter((x + 1, _x), idx) { for c in columns[4].row_iter((x + 1)..(_x + 1), idx) {
columns[4][c].set_keep_fg(true); columns[4][c].set_keep_fg(true);
columns[4][c].set_keep_bg(true); columns[4][c].set_keep_bg(true);
} }
for c in columns[4].row_iter((x, x), idx) { for c in columns[4].row_iter(x..(x + 1), idx) {
columns[4][c].set_keep_bg(true); columns[4][c].set_keep_bg(true);
} }
x = _x + 1; x = _x + 1;
} }
x x
}; };
for c in columns[4].row_iter((x, min_width.4.saturating_sub(1)), idx) { for c in columns[4].row_iter(x..min_width.4, idx) {
columns[4][c].set_ch(' '); columns[4][c].set_ch(' ');
columns[4][c].set_bg(bg_color); columns[4][c].set_bg(bg_color);
} }

View File

@ -1000,7 +1000,7 @@ impl ConversationsListing {
((0, 3 * idx), (width - 1, 3 * idx)), ((0, 3 * idx), (width - 1, 3 * idx)),
None, None,
); );
for c in self.content.row_iter((x, x + 3), 3 * idx) { for c in self.content.row_iter(x..(x + 4), 3 * idx) {
self.content[c].set_bg(bg_color); self.content[c].set_bg(bg_color);
} }
/* draw subject */ /* draw subject */
@ -1025,25 +1025,25 @@ impl ConversationsListing {
((x + 1, 3 * idx), (width - 1, 3 * idx)), ((x + 1, 3 * idx), (width - 1, 3 * idx)),
None, None,
); );
for c in self.content.row_iter((x, x), 3 * idx) { for c in self.content.row_iter(x..(x + 1), 3 * idx) {
self.content[c].set_bg(color); self.content[c].set_bg(color);
} }
for c in self.content.row_iter((_x, _x), 3 * idx) { for c in self.content.row_iter(_x..(_x + 1), 3 * idx) {
self.content[c].set_bg(color); self.content[c].set_bg(color);
self.content[c].set_keep_bg(true); self.content[c].set_keep_bg(true);
} }
for c in self.content.row_iter((x + 1, _x), 3 * idx) { for c in self.content.row_iter(x + 1..(_x + 1), 3 * idx) {
self.content[c].set_keep_fg(true); self.content[c].set_keep_fg(true);
self.content[c].set_keep_bg(true); self.content[c].set_keep_bg(true);
} }
for c in self.content.row_iter((x, x), 3 * idx) { for c in self.content.row_iter(x..(x + 1), 3 * idx) {
self.content[c].set_keep_bg(true); self.content[c].set_keep_bg(true);
} }
x = _x + 1; x = _x + 1;
} }
x x
}; };
for c in self.content.row_iter((x, width.saturating_sub(1)), 3 * idx) { for c in self.content.row_iter(x..width, 3 * idx) {
self.content[c].set_ch(' '); self.content[c].set_ch(' ');
self.content[c].set_bg(bg_color); self.content[c].set_bg(bg_color);
} }
@ -1057,7 +1057,7 @@ impl ConversationsListing {
((0, 3 * idx + 1), (width - 1, 3 * idx + 1)), ((0, 3 * idx + 1), (width - 1, 3 * idx + 1)),
None, None,
); );
for c in self.content.row_iter((x, x + 4), 3 * idx + 1) { for c in self.content.row_iter(x..(x + 5), 3 * idx + 1) {
self.content[c].set_ch('▁'); self.content[c].set_ch('▁');
self.content[c].set_bg(bg_color); self.content[c].set_bg(bg_color);
} }
@ -1072,17 +1072,11 @@ impl ConversationsListing {
None, None,
); );
for c in self for c in self.content.row_iter(x..width, 3 * idx + 1) {
.content
.row_iter((x, width.saturating_sub(1)), 3 * idx + 1)
{
self.content[c].set_ch('▁'); self.content[c].set_ch('▁');
self.content[c].set_bg(bg_color); self.content[c].set_bg(bg_color);
} }
for c in self for c in self.content.row_iter(0..width, 3 * idx + 2) {
.content
.row_iter((0, width.saturating_sub(1)), 3 * idx + 2)
{
self.content[c].set_ch('▓'); self.content[c].set_ch('▓');
self.content[c].set_fg(padding_fg); self.content[c].set_fg(padding_fg);
self.content[c].set_bg(bg_color); self.content[c].set_bg(bg_color);
@ -1131,7 +1125,7 @@ impl Component for ConversationsListing {
area, area,
Some(get_x(upper_left)), Some(get_x(upper_left)),
); );
for c in grid.row_iter((x, get_x(bottom_right)), y) { for c in grid.row_iter(x..(get_x(bottom_right) + 1), y) {
grid[c] = Cell::default(); grid[c] = Cell::default();
} }
clear_area(grid, ((x, y), set_y(bottom_right, y))); clear_area(grid, ((x, y), set_y(bottom_right, y)));

View File

@ -161,10 +161,7 @@ impl ListingTrait for PlainListing {
pos_dec(self.data_columns.columns[3].size(), (1, 1)), pos_dec(self.data_columns.columns[3].size(), (1, 1)),
), ),
); );
for c in grid.row_iter( for c in grid.row_iter(x..(x + self.data_columns.widths[3]), get_y(upper_left)) {
(x, x + self.data_columns.widths[3].saturating_sub(1)),
get_y(upper_left),
) {
grid[c].set_bg(bg_color); grid[c].set_bg(bg_color);
} }
return; return;
@ -338,14 +335,11 @@ impl ListingTrait for PlainListing {
bg_color, bg_color,
); );
for c in grid.row_iter( for c in grid.row_iter(
( flag_x
flag_x, ..std::cmp::min(
std::cmp::min(
get_x(bottom_right), get_x(bottom_right),
flag_x + 2 + self.data_columns.widths[3], flag_x + 2 + self.data_columns.widths[3],
) ),
.saturating_sub(1),
),
get_y(upper_left) + r, get_y(upper_left) + r,
) { ) {
grid[c].set_bg(bg_color); grid[c].set_bg(bg_color);
@ -775,7 +769,7 @@ impl PlainListing {
((0, idx), (min_width.0, idx)), ((0, idx), (min_width.0, idx)),
None, None,
); );
for c in columns[0].row_iter((x, min_width.0.saturating_sub(1)), idx) { for c in columns[0].row_iter(x..min_width.0, idx) {
columns[0][c].set_bg(bg_color); columns[0][c].set_bg(bg_color);
} }
let (x, _) = write_string_to_grid( let (x, _) = write_string_to_grid(
@ -787,7 +781,7 @@ impl PlainListing {
((0, idx), (min_width.1, idx)), ((0, idx), (min_width.1, idx)),
None, None,
); );
for c in columns[1].row_iter((x, min_width.1.saturating_sub(1)), idx) { for c in columns[1].row_iter(x..min_width.1, idx) {
columns[1][c].set_bg(bg_color); columns[1][c].set_bg(bg_color);
} }
let (x, _) = write_string_to_grid( let (x, _) = write_string_to_grid(
@ -799,7 +793,7 @@ impl PlainListing {
((0, idx), (min_width.2, idx)), ((0, idx), (min_width.2, idx)),
None, None,
); );
for c in columns[2].row_iter((x, min_width.2.saturating_sub(1)), idx) { for c in columns[2].row_iter(x..min_width.2, idx) {
columns[2][c].set_bg(bg_color); columns[2][c].set_bg(bg_color);
} }
let (x, _) = write_string_to_grid( let (x, _) = write_string_to_grid(
@ -811,7 +805,7 @@ impl PlainListing {
((0, idx), (min_width.3, idx)), ((0, idx), (min_width.3, idx)),
None, None,
); );
for c in columns[3].row_iter((x, min_width.3.saturating_sub(1)), idx) { for c in columns[3].row_iter(x..min_width.3, idx) {
columns[3][c].set_bg(bg_color); columns[3][c].set_bg(bg_color);
} }
let (x, _) = write_string_to_grid( let (x, _) = write_string_to_grid(
@ -835,25 +829,25 @@ impl PlainListing {
((x + 1, idx), (min_width.4, idx)), ((x + 1, idx), (min_width.4, idx)),
None, None,
); );
for c in columns[4].row_iter((x, x), idx) { for c in columns[4].row_iter(x..(x + 1), idx) {
columns[4][c].set_bg(color); columns[4][c].set_bg(color);
} }
for c in columns[4].row_iter((_x, _x), idx) { for c in columns[4].row_iter(_x..(_x + 1), idx) {
columns[4][c].set_bg(color); columns[4][c].set_bg(color);
columns[4][c].set_keep_bg(true); columns[4][c].set_keep_bg(true);
} }
for c in columns[4].row_iter((x + 1, _x), idx) { for c in columns[4].row_iter((x + 1)..(_x + 1), idx) {
columns[4][c].set_keep_fg(true); columns[4][c].set_keep_fg(true);
columns[4][c].set_keep_bg(true); columns[4][c].set_keep_bg(true);
} }
for c in columns[4].row_iter((x, x), idx) { for c in columns[4].row_iter(x..(x + 1), idx) {
columns[4][c].set_keep_bg(true); columns[4][c].set_keep_bg(true);
} }
x = _x + 1; x = _x + 1;
} }
x x
}; };
for c in columns[4].row_iter((x, min_width.4.saturating_sub(1)), idx) { for c in columns[4].row_iter(x..min_width.4, idx) {
columns[4][c].set_bg(bg_color); columns[4][c].set_bg(bg_color);
} }
if context.accounts[self.cursor_pos.0] if context.accounts[self.cursor_pos.0]

View File

@ -355,12 +355,12 @@ impl CellBuffer {
} }
/// See `RowIterator` documentation. /// See `RowIterator` documentation.
pub fn row_iter(&self, bounds: (usize, usize), row: usize) -> RowIterator { pub fn row_iter(&self, bounds: std::ops::Range<usize>, row: usize) -> RowIterator {
if row < self.rows { if row < self.rows {
RowIterator { RowIterator {
row, row,
col: std::cmp::min(self.cols.saturating_sub(1), bounds.0) col: std::cmp::min(self.cols.saturating_sub(1), bounds.start)
..(std::cmp::min(self.cols, bounds.1 + 1)), ..(std::cmp::min(self.cols, bounds.end)),
} }
} else { } else {
RowIterator { row, col: 0..0 } RowIterator { row, col: 0..0 }
@ -1871,7 +1871,7 @@ pub mod ansi {
/// `BoundsIterator` which iterates each row. /// `BoundsIterator` which iterates each row.
/// ```norun /// ```norun
/// for c in grid.row_iter( /// for c in grid.row_iter(
/// (x, x + 10), /// x..(x + 11),
/// 0, /// 0,
/// ) { /// ) {
/// grid[c].set_ch('w'); /// grid[c].set_ch('w');