lines.rs 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. use std::io::{Write, Result as IOResult};
  2. use ansi_term::{ANSIStrings, ANSIGenericString};
  3. use fs::File;
  4. use output::file_name::{FileName, FileStyle};
  5. use style::Colours;
  6. use output::icons::painted_icon;
  7. use output::cell::TextCell;
  8. #[derive(PartialEq, Debug, Copy, Clone)]
  9. pub struct Options {
  10. pub icons: bool
  11. }
  12. /// The lines view literally just displays each file, line-by-line.
  13. pub struct Render<'a> {
  14. pub files: Vec<File<'a>>,
  15. pub colours: &'a Colours,
  16. pub style: &'a FileStyle,
  17. pub opts: &'a Options,
  18. }
  19. impl<'a> Render<'a> {
  20. pub fn render<W: Write>(&self, w: &mut W) -> IOResult<()> {
  21. for file in &self.files {
  22. let name_cell = self.render_file(file).paint();
  23. if self.opts.icons {
  24. // Create a TextCell for the icon then append the text to it
  25. let mut cell = TextCell::default();
  26. let icon = painted_icon(&file, self.style);
  27. cell.push(ANSIGenericString::from(icon), 2);
  28. cell.append(name_cell.promote());
  29. writeln!(w, "{}", ANSIStrings(&cell))?;
  30. } else {
  31. writeln!(w, "{}", ANSIStrings(&name_cell))?;
  32. }
  33. }
  34. Ok(())
  35. }
  36. fn render_file<'f>(&self, file: &'f File<'a>) -> FileName<'f, 'a, Colours> {
  37. self.style.for_file(file, self.colours).with_link_paths()
  38. }
  39. }