| 1 | // env var which should disable workarounds |
| 2 | const NO_WORKAROUNDSrust-analyzer cargo git-delta 0.19.2 utils/workarounds/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: &consolerust-analyzer cargo console 0.15.7 crate/::Termrust-analyzer cargo console 0.15.7 term/Term#) -> usize { |
| 9 | fn guess_real_width(current_width: u16, term_stdout: &consolerust-analyzer cargo console 0.15.7 crate/::Termrust-analyzer cargo console 0.15.7 term/Term#) -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<u16> { |
| 10 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::processrust-analyzer cargo std https://github.com/rust-lang/rust/library/std process/::{Commandrust-analyzer cargo std https://github.com/rust-lang/rust/library/std process/Command#, Stdiorust-analyzer cargo std https://github.com/rust-lang/rust/library/std process/Stdio#}; |
| 11 | |
| 12 | let term_var = stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::envrust-analyzer cargo std https://github.com/rust-lang/rust/library/std env/::varrust-analyzer cargo std https://github.com/rust-lang/rust/library/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 stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::envrust-analyzer cargo std https://github.com/rust-lang/rust/library/std env/::varrust-analyzer cargo std https://github.com/rust-lang/rust/library/std env/var().(NO_WORKAROUNDSrust-analyzer cargo git-delta 0.19.2 utils/workarounds/NO_WORKAROUNDS.).is_ok() { |
| 19 | return Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#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 = stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc str/::from_utf8rust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/converts/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 | Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#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..=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeInclusive#25, 79..=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeInclusive#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_fixrust-analyzer cargo git-delta 0.19.2 utils/workarounds/windows_msys2_width_fix().(height_width: (u16, u16), _: &consolerust-analyzer cargo console 0.15.7 crate/::Termrust-analyzer cargo console 0.15.7 term/Term#) -> usize { |
| 58 | let _ = NO_WORKAROUNDSrust-analyzer cargo git-delta 0.19.2 utils/workarounds/NO_WORKAROUNDS.; |
| 59 | height_width.1.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().() |
| 60 | } |