mod.rs 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. #[cfg(target_os = "windows")]
  2. pub use self::cell::{DisplayWidth, TextCell, TextCellContents};
  3. pub use self::escape::escape;
  4. pub mod color_scale;
  5. pub mod details;
  6. pub mod file_name;
  7. pub mod grid;
  8. pub mod grid_details;
  9. pub mod icons;
  10. pub mod lines;
  11. pub mod render;
  12. pub mod table;
  13. pub mod time;
  14. mod cell;
  15. mod escape;
  16. mod tree;
  17. /// The **view** contains all information about how to format output.
  18. #[derive(Debug)]
  19. pub struct View {
  20. pub mode: Mode,
  21. pub width: TerminalWidth,
  22. pub file_style: file_name::Options,
  23. pub deref_links: bool,
  24. pub total_size: bool,
  25. }
  26. /// The **mode** is the “type” of output.
  27. #[derive(PartialEq, Eq, Debug)]
  28. #[allow(clippy::large_enum_variant)]
  29. pub enum Mode {
  30. Grid(grid::Options),
  31. Details(details::Options),
  32. GridDetails(grid_details::Options),
  33. Lines,
  34. }
  35. /// The width of the terminal requested by the user.
  36. #[derive(PartialEq, Eq, Debug, Copy, Clone)]
  37. pub enum TerminalWidth {
  38. /// The user requested this specific number of columns.
  39. Set(usize),
  40. /// Look up the terminal size at runtime.
  41. Automatic,
  42. }
  43. impl TerminalWidth {
  44. pub fn actual_terminal_width(self) -> Option<usize> {
  45. // All of stdin, stdout, and stderr could not be connected to a
  46. // terminal, but we’re only interested in stdout because it’s
  47. // where the output goes.
  48. #[cfg(unix)]
  49. let stdout_term_width = {
  50. use std::os::fd::AsRawFd;
  51. terminal_size::terminal_size_using_fd(std::io::stdout().as_raw_fd())
  52. .map(|(w, _h)| w.0 as _)
  53. };
  54. #[cfg(windows)]
  55. let stdout_term_width = {
  56. use windows_sys::Win32::System::Console::{GetStdHandle, STD_OUTPUT_HANDLE};
  57. terminal_size::terminal_size_using_handle(unsafe { GetStdHandle(STD_OUTPUT_HANDLE) })
  58. .map(|(w, _h)| w.0 as _)
  59. };
  60. #[rustfmt::skip]
  61. return match self {
  62. Self::Set(width) => Some(width),
  63. Self::Automatic => stdout_term_width,
  64. };
  65. }
  66. }