From 18843eaba2a9f3618ea633a55446d8c6ea5bfd38 Mon Sep 17 00:00:00 2001 From: daa Date: Thu, 1 Feb 2018 23:13:09 +0300 Subject: [PATCH] Keep width after add new lines, fix indent with firstc --- src/cmd_line.rs | 31 +++++++++++++++++++++---------- src/ui_model/model_layout.rs | 20 +++++++++++++++++++- 2 files changed, 40 insertions(+), 11 deletions(-) diff --git a/src/cmd_line.rs b/src/cmd_line.rs index 8838010..22f67ed 100644 --- a/src/cmd_line.rs +++ b/src/cmd_line.rs @@ -127,12 +127,19 @@ impl Level { fn prompt_lines(firstc: &str, prompt: &str, indent: u64) -> Vec<(Option, Vec)> { if !firstc.is_empty() { - vec![ - ( - None, - firstc.chars().chain((0..indent).map(|_| ' ')).collect(), - ), - ] + if firstc.len() >= indent as usize { + vec![(None, firstc.chars().collect())] + } else { + vec![ + ( + None, + firstc + .chars() + .chain((firstc.len()..indent as usize).map(|_| ' ')) + .collect(), + ), + ] + } } else if !prompt.is_empty() { prompt .lines() @@ -191,9 +198,11 @@ impl cursor::CursorRedrawCb for State { fn queue_redraw_cursor(&mut self) { if let Some(ref level) = self.levels.last() { let level_preferred_height = level.preferred_height; - let block_preferred_height = self.block.as_ref().map(|b| b.preferred_height).unwrap_or(0); + let block_preferred_height = + self.block.as_ref().map(|b| b.preferred_height).unwrap_or(0); - let gap = self.drawing_area.get_allocated_height() - level_preferred_height - block_preferred_height; + let gap = self.drawing_area.get_allocated_height() - level_preferred_height + - block_preferred_height; let model = &level.model_layout.model; @@ -206,9 +215,11 @@ impl cursor::CursorRedrawCb for State { let (x, y, width, height) = cur_point.to_area_extend_ink(model, cell_metrics); if gap > 0 { - self.drawing_area.queue_draw_area(x, y + gap / 2, width, height); + self.drawing_area + .queue_draw_area(x, y + gap / 2, width, height); } else { - self.drawing_area.queue_draw_area(x, y + block_preferred_height, width, height); + self.drawing_area + .queue_draw_area(x, y + block_preferred_height, width, height); } } } diff --git a/src/ui_model/model_layout.rs b/src/ui_model/model_layout.rs index d24813b..7113a86 100644 --- a/src/ui_model/model_layout.rs +++ b/src/ui_model/model_layout.rs @@ -1,8 +1,11 @@ +use std::cmp::max; + use ui_model::{Attrs, UiModel}; pub struct ModelLayout { pub model: UiModel, rows_filled: usize, + cols_filled: usize, } impl ModelLayout { @@ -12,6 +15,7 @@ impl ModelLayout { ModelLayout { model: UiModel::new(ModelLayout::ROWS_STEP as u64, columns), rows_filled: 0, + cols_filled: 0, } } @@ -78,7 +82,8 @@ impl ModelLayout { row_idx += 1; } - (max_col_idx + 1, self.rows_filled) + self.cols_filled = max(self.cols_filled, max_col_idx + 1); + (self.cols_filled, self.rows_filled) } fn count_lines(lines: &Vec, Vec)>>, max_columns: usize) -> usize { @@ -119,4 +124,17 @@ mod tests { assert_eq!(ModelLayout::ROWS_STEP * 2, rows); assert_eq!(ModelLayout::ROWS_STEP * 2, model.model.rows); } + + #[test] + fn test_cols_filled() { + let lines = vec![vec![(None, vec!['a'; 3])]; 1]; + let mut model = ModelLayout::new(5); + + let (cols, _) = model.layout(&lines); + assert_eq!(3, cols); + + let lines = vec![vec![(None, vec!['a'; 2])]; 1]; + let (cols, _) = model.layout_append(&lines); + assert_eq!(3, cols); + } }