Keep width after add new lines, fix indent with firstc

This commit is contained in:
daa 2018-02-01 23:13:09 +03:00
parent d02c1fb3d6
commit 18843eaba2
2 changed files with 40 additions and 11 deletions

View File

@ -127,12 +127,19 @@ impl Level {
fn prompt_lines(firstc: &str, prompt: &str, indent: u64) -> Vec<(Option<Attrs>, Vec<char>)> { fn prompt_lines(firstc: &str, prompt: &str, indent: u64) -> Vec<(Option<Attrs>, Vec<char>)> {
if !firstc.is_empty() { if !firstc.is_empty() {
vec![ if firstc.len() >= indent as usize {
( vec![(None, firstc.chars().collect())]
None, } else {
firstc.chars().chain((0..indent).map(|_| ' ')).collect(), vec![
), (
] None,
firstc
.chars()
.chain((firstc.len()..indent as usize).map(|_| ' '))
.collect(),
),
]
}
} else if !prompt.is_empty() { } else if !prompt.is_empty() {
prompt prompt
.lines() .lines()
@ -191,9 +198,11 @@ impl cursor::CursorRedrawCb for State {
fn queue_redraw_cursor(&mut self) { fn queue_redraw_cursor(&mut self) {
if let Some(ref level) = self.levels.last() { if let Some(ref level) = self.levels.last() {
let level_preferred_height = level.preferred_height; 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; 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); let (x, y, width, height) = cur_point.to_area_extend_ink(model, cell_metrics);
if gap > 0 { 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 { } 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);
} }
} }
} }

View File

@ -1,8 +1,11 @@
use std::cmp::max;
use ui_model::{Attrs, UiModel}; use ui_model::{Attrs, UiModel};
pub struct ModelLayout { pub struct ModelLayout {
pub model: UiModel, pub model: UiModel,
rows_filled: usize, rows_filled: usize,
cols_filled: usize,
} }
impl ModelLayout { impl ModelLayout {
@ -12,6 +15,7 @@ impl ModelLayout {
ModelLayout { ModelLayout {
model: UiModel::new(ModelLayout::ROWS_STEP as u64, columns), model: UiModel::new(ModelLayout::ROWS_STEP as u64, columns),
rows_filled: 0, rows_filled: 0,
cols_filled: 0,
} }
} }
@ -78,7 +82,8 @@ impl ModelLayout {
row_idx += 1; 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<(Option<Attrs>, Vec<char>)>>, max_columns: usize) -> usize { fn count_lines(lines: &Vec<Vec<(Option<Attrs>, Vec<char>)>>, max_columns: usize) -> usize {
@ -119,4 +124,17 @@ mod tests {
assert_eq!(ModelLayout::ROWS_STEP * 2, rows); assert_eq!(ModelLayout::ROWS_STEP * 2, rows);
assert_eq!(ModelLayout::ROWS_STEP * 2, model.model.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);
}
} }