Debian Codegraph @

src/utils/workarounds.rs

raw symbols SCIP
1 // env var which should disable workarounds
2 const NO_WORKAROUNDS: &str = "DELTA_NO_WORKAROUNDS";
3
4 // Work around a bug in the 'console' crate (inherited from 'terminal-size', #25): On Windows
5 // it can not determine the width of an MSYS2 / MINGW64 terminal (e.g. from Git-Bash) correctly.
6 // Instead use the usually included stty util from the MSYS2 distribution.
7 #[cfg(target_os = "windows")]
8 pub fn windows_msys2_width_fix(height_width: (u16, u16), term_stdout: &console::Term) -> usize {
9 fn guess_real_width(current_width: u16, term_stdout: &console::Term) -> Option<u16> {
10 use std::process::{Command, Stdio};
11
12 let term_var = std::env::var("TERM").ok()?;
13 // More checks before actually calling stty.
14 if term_var.starts_with("xterm")
15 && term_stdout.is_term()
16 && term_stdout.features().is_msys_tty()
17 {
18 if std::env::var(NO_WORKAROUNDS).is_ok() {
19 return Some(current_width);
20 }
21
22 // stderr/2 is passed to the Command below.
23 let pseudo_term = "/dev/fd/2";
24
25 // Read width via stty helper program (e.g. "C:\Program Files\Git\usr\bin\stty.exe")
26 // which gets both the MSYS2 and cmd.exe width right.
27 let result = Command::new("stty")
28 .stderr(Stdio::inherit())
29 .arg("-F")
30 .arg(pseudo_term)
31 .arg("size")
32 .output()
33 .ok()?;
34
35 if result.status.success() {
36 let size = std::str::from_utf8(&result.stdout).ok()?;
37 let mut it = size.split_whitespace();
38 let _height = it.next()?;
39 return it.next().map(|width| width.parse().ok())?;
40 }
41 }
42 None
43 }
44
45 // Calling an external binary is slow, so make sure this is actually necessary.
46 // The fallback values of 25 lines by 80 columns (sometimes zero indexed) are a good
47 // indicator.
48 let (height, width) = height_width;
49 match (height, width) {
50 (24..=25, 79..=80) => guess_real_width(width, term_stdout).unwrap_or(width),
51 _ => width,
52 }
53 .into()
54 }
55
56 #[cfg(not(target_os = "windows"))]
57 pub fn windows_msys2_width_fix(height_width: (u16, u16), _: &console::Term) -> usize {
58 let _ = NO_WORKAROUNDS;
59 height_width.1.into()
60 }