grid.rs 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. use term_grid as grid;
  2. use file::File;
  3. use output::DisplayWidth;
  4. use output::colours::Colours;
  5. use super::filename;
  6. #[derive(PartialEq, Debug, Copy, Clone)]
  7. pub struct Grid {
  8. pub across: bool,
  9. pub console_width: usize,
  10. pub colours: Colours,
  11. }
  12. impl Grid {
  13. pub fn view(&self, files: &[File]) {
  14. let direction = if self.across { grid::Direction::LeftToRight }
  15. else { grid::Direction::TopToBottom };
  16. let mut grid = grid::Grid::new(grid::GridOptions {
  17. direction: direction,
  18. filling: grid::Filling::Spaces(2),
  19. });
  20. grid.reserve(files.len());
  21. for file in files.iter() {
  22. let mut width = DisplayWidth::from(&*file.name);
  23. if file.dir.is_none() {
  24. if let Some(ref parent) = file.path.parent() {
  25. width = width + 1 + DisplayWidth::from(parent.to_string_lossy().as_ref());
  26. }
  27. }
  28. grid.add(grid::Cell {
  29. contents: filename(file, &self.colours, false).strings().to_string(),
  30. width: *width,
  31. });
  32. }
  33. if let Some(display) = grid.fit_into_width(self.console_width) {
  34. print!("{}", display);
  35. }
  36. else {
  37. // File names too long for a grid - drop down to just listing them!
  38. for file in files.iter() {
  39. println!("{}", filename(file, &self.colours, false).strings());
  40. }
  41. }
  42. }
  43. }