mod.rs 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. Json(Option<details::Options>),
  35. }
  36. /// The width of the terminal requested by the user.
  37. #[derive(PartialEq, Eq, Debug, Copy, Clone)]
  38. pub enum TerminalWidth {
  39. /// The user requested this specific number of columns.
  40. Set(usize),
  41. /// Look up the terminal size at runtime.
  42. Automatic,
  43. }
  44. impl TerminalWidth {
  45. pub fn actual_terminal_width(self) -> Option<usize> {
  46. // All of stdin, stdout, and stderr could not be connected to a
  47. // terminal, but we’re only interested in stdout because it’s
  48. // where the output goes.
  49. #[cfg(unix)]
  50. let stdout_term_width = {
  51. use std::os::fd::AsRawFd;
  52. terminal_size::terminal_size_using_fd(std::io::stdout().as_raw_fd())
  53. .map(|(w, _h)| w.0 as _)
  54. };
  55. #[cfg(windows)]
  56. let stdout_term_width = {
  57. use std::os::windows::io::RawHandle;
  58. use windows_sys::Win32::System::Console::{GetStdHandle, STD_OUTPUT_HANDLE};
  59. terminal_size::terminal_size_using_handle(unsafe {
  60. GetStdHandle(STD_OUTPUT_HANDLE) as RawHandle
  61. })
  62. .map(|(w, _h)| w.0 as _)
  63. };
  64. #[rustfmt::skip]
  65. return match self {
  66. Self::Set(width) => Some(width),
  67. Self::Automatic => stdout_term_width,
  68. };
  69. }
  70. }