grid.rs 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. // SPDX-FileCopyrightText: 2024 Christina Sørensen
  2. // SPDX-License-Identifier: EUPL-1.2
  3. //
  4. // SPDX-FileCopyrightText: 2023-2024 Christina Sørensen, eza contributors
  5. // SPDX-FileCopyrightText: 2014 Benjamin Sago
  6. // SPDX-License-Identifier: MIT
  7. use std::io::{self, Write};
  8. use term_grid::{Direction, Filling, Grid, GridOptions};
  9. use crate::fs::File;
  10. use crate::fs::filter::FileFilter;
  11. use crate::output::file_name::Options as FileStyle;
  12. use crate::theme::Theme;
  13. #[derive(PartialEq, Eq, Debug, Copy, Clone)]
  14. pub struct Options {
  15. pub across: bool,
  16. }
  17. impl Options {
  18. #[must_use]
  19. pub fn direction(self) -> Direction {
  20. if self.across {
  21. Direction::LeftToRight
  22. } else {
  23. Direction::TopToBottom
  24. }
  25. }
  26. }
  27. pub struct Render<'a> {
  28. pub files: Vec<File<'a>>,
  29. pub theme: &'a Theme,
  30. pub file_style: &'a FileStyle,
  31. pub opts: &'a Options,
  32. pub console_width: usize,
  33. pub filter: &'a FileFilter,
  34. }
  35. impl Render<'_> {
  36. pub fn render<W: Write>(mut self, w: &mut W) -> io::Result<()> {
  37. self.filter.sort_files(&mut self.files);
  38. let cells = self
  39. .files
  40. .iter()
  41. .map(|file| {
  42. self.file_style
  43. .for_file(file, self.theme)
  44. .paint()
  45. .strings()
  46. .to_string()
  47. })
  48. .collect();
  49. let grid = Grid::new(
  50. cells,
  51. GridOptions {
  52. filling: Filling::Spaces(2),
  53. direction: self.opts.direction(),
  54. width: self.console_width,
  55. },
  56. );
  57. write!(w, "{grid}")
  58. }
  59. }