diff --git a/src/tty_stuff.rs b/src/tty_stuff.rs index e3c4c392c462b460f279d1dd9391ef8c352a62de..c7b873716d26840041abc0017541bd89e2429b7d 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(())