From 60b10d9dd03832348c31e97bd138fcb6846dfa68 Mon Sep 17 00:00:00 2001 From: Pavel Kirilin <win10@list.ru> Date: Wed, 1 Apr 2020 11:02:01 +0400 Subject: [PATCH] Fixed grid rendering. Signed-off-by: Pavel Kirilin <win10@list.ru> --- src/tty_stuff.rs | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/src/tty_stuff.rs b/src/tty_stuff.rs index e3c4c39..c7b8737 100644 --- a/src/tty_stuff.rs +++ b/src/tty_stuff.rs @@ -8,7 +8,7 @@ use term_grid::{Grid, GridOptions, Filling, Direction, Cell}; use std::process::exit; use std::str::FromStr; -pub fn get_matched_files_grid(pattern: String) -> AppResult<String> { +pub fn get_matched_files_grid(pattern: String, screen_width: u16) -> AppResult<String> { let mut grid = Grid::new(GridOptions { direction: Direction::LeftToRight, filling: Filling::Spaces(2), @@ -17,7 +17,11 @@ pub fn get_matched_files_grid(pattern: String) -> AppResult<String> { for filename in filenames { grid.add(Cell::from(filename)) } - Ok(format!("{}", grid.fit_into_columns(6))) + if let Some(grid) = grid.fit_into_width(screen_width as usize) { + Ok(format!("{}", grid)) + } else { + Ok(String::new()) + } } pub fn choose_pattern(current_pattern: String) -> AppResult<String> { @@ -29,16 +33,21 @@ pub fn choose_pattern(current_pattern: String) -> AppResult<String> { |stdout, pattern| { if !pattern.is_empty() { write!(stdout, "{}------Matched files------", termion::cursor::Goto(1, 3))?; - let grid = get_matched_files_grid(pattern)?; + let (col, _) = termion::terminal_size()?; + let grid = get_matched_files_grid(pattern, col)?; if grid.is_empty() { write!(stdout, "{}No matches found", termion::cursor::Goto(1, 4) )?; } else { - write!(stdout, "{}{}", - termion::cursor::Goto(1, 4), - grid - )?; + for line in grid.lines() { + write!(stdout, "{}{}{}{}", + termion::cursor::Down(1), + termion::clear::CurrentLine, + termion::cursor::Left(col), + line + )?; + } } } Ok(()) -- GitLab