Fixed grid rendering.

Signed-off-by: Pavel Kirilin <win10@list.ru>
This commit is contained in:
2020-04-01 11:02:01 +04:00
parent 02344c86f9
commit 60b10d9dd0

View File

@ -8,7 +8,7 @@ use term_grid::{Grid, GridOptions, Filling, Direction, Cell};
use std::process::exit; use std::process::exit;
use std::str::FromStr; 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 { let mut grid = Grid::new(GridOptions {
direction: Direction::LeftToRight, direction: Direction::LeftToRight,
filling: Filling::Spaces(2), filling: Filling::Spaces(2),
@ -17,7 +17,11 @@ pub fn get_matched_files_grid(pattern: String) -> AppResult<String> {
for filename in filenames { for filename in filenames {
grid.add(Cell::from(filename)) 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> { pub fn choose_pattern(current_pattern: String) -> AppResult<String> {
@ -29,16 +33,21 @@ pub fn choose_pattern(current_pattern: String) -> AppResult<String> {
|stdout, pattern| { |stdout, pattern| {
if !pattern.is_empty() { if !pattern.is_empty() {
write!(stdout, "{}------Matched files------", termion::cursor::Goto(1, 3))?; 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() { if grid.is_empty() {
write!(stdout, "{}No matches found", write!(stdout, "{}No matches found",
termion::cursor::Goto(1, 4) termion::cursor::Goto(1, 4)
)?; )?;
} else { } else {
write!(stdout, "{}{}", for line in grid.lines() {
termion::cursor::Goto(1, 4), write!(stdout, "{}{}{}{}",
grid termion::cursor::Down(1),
)?; termion::clear::CurrentLine,
termion::cursor::Left(col),
line
)?;
}
} }
} }
Ok(()) Ok(())