grid.rs 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. use std::io::{Write, Result as IOResult};
  2. use term_grid as grid;
  3. use fs::File;
  4. use output::colours::Colours;
  5. use output::file_name::{FileName, LinkStyle, Classify};
  6. #[derive(PartialEq, Debug, Copy, Clone)]
  7. pub struct Grid {
  8. pub across: bool,
  9. pub console_width: usize,
  10. pub colours: Colours,
  11. pub classify: Classify,
  12. }
  13. impl Grid {
  14. pub fn view<W: Write>(&self, files: &[File], w: &mut W) -> IOResult<()> {
  15. let direction = if self.across { grid::Direction::LeftToRight }
  16. else { grid::Direction::TopToBottom };
  17. let mut grid = grid::Grid::new(grid::GridOptions {
  18. direction: direction,
  19. filling: grid::Filling::Spaces(2),
  20. });
  21. grid.reserve(files.len());
  22. for file in files.iter() {
  23. let filename = FileName::new(file, LinkStyle::JustFilenames, self.classify, &self.colours).paint();
  24. let width = filename.width();
  25. grid.add(grid::Cell {
  26. contents: filename.strings().to_string(),
  27. width: *width,
  28. });
  29. }
  30. if let Some(display) = grid.fit_into_width(self.console_width) {
  31. write!(w, "{}", display)
  32. }
  33. else {
  34. // File names too long for a grid - drop down to just listing them!
  35. for file in files.iter() {
  36. let name_cell = FileName::new(file, LinkStyle::JustFilenames, self.classify, &self.colours).paint();
  37. writeln!(w, "{}", name_cell.strings())?;
  38. }
  39. Ok(())
  40. }
  41. }
  42. }