Kaynağa Gözat

fix(output): terminal size query should only check `stdout`

Another API misuse. `terminal_size::terminal_size()` now looks into `stdout`, `stderr` and `stdin` to determine the terminal size with best effort.
This is desirable for TUI applications, but not for CLI applications.
`eza` only cares about the size of the terminal which is connected to standard output.
We change this to `terminal_size_with_fd(stdout.as_raw_fd)` on unix, and `terminal_size_with_handle(STD_OUTPUT_HANDLE)` on windows.
hehelego 2 yıl önce
ebeveyn
işleme
9d834fb3ca
1 değiştirilmiş dosya ile 17 ekleme ve 1 silme
  1. 17 1
      src/output/mod.rs

+ 17 - 1
src/output/mod.rs

@@ -50,10 +50,26 @@ impl TerminalWidth {
         // terminal, but we’re only interested in stdout because it’s
         // terminal, but we’re only interested in stdout because it’s
         // where the output goes.
         // where the output goes.
 
 
+        #[cfg(unix)]
+        let stdout_term_width = {
+            use std::os::fd::AsRawFd;
+            terminal_size::terminal_size_using_fd(std::io::stdout().as_raw_fd())
+                .map(|(w, _h)| w.0 as _)
+        };
+        #[cfg(windows)]
+        let stdout_term_width = {
+            use std::os::windows::io::RawHandle;
+            use windows_sys::Win32::System::Console::{GetStdHandle, STD_OUTPUT_HANDLE};
+            terminal_size::terminal_size_using_handle(unsafe {
+                GetStdHandle(STD_OUTPUT_HANDLE) as RawHandle
+            })
+            .map(|(w, h)| w.0 as _)
+        };
+
         #[rustfmt::skip]
         #[rustfmt::skip]
         return match self {
         return match self {
             Self::Set(width)  => Some(width),
             Self::Set(width)  => Some(width),
-            Self::Automatic   => terminal_size::terminal_size().map(|(w, _)| w.0.into()),
+            Self::Automatic   => stdout_term_width,
         };
         };
     }
     }
 }
 }