-
Notifications
You must be signed in to change notification settings - Fork 415
Add terminal width fallback via stty if on windows/MSYS2 #1030
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,3 +5,4 @@ pub mod process; | |
pub mod regex_replacement; | ||
pub mod round_char_boundary; | ||
pub mod syntect; | ||
pub mod workarounds; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
// env var which should disable workarounds | ||
const NO_WORKAROUNDS: &str = "DELTA_NO_WORKAROUNDS"; | ||
|
||
// Work around a bug in the 'console' crate (inherited from 'terminal-size', #25): On Windows | ||
// it can not determine the width of an MSYS2 / MINGW64 terminal (e.g. from Git-Bash) correctly. | ||
// Instead use the usually included stty util from the MSYS2 distribution. | ||
#[cfg(target_os = "windows")] | ||
pub fn windows_msys2_width_fix(height_width: (u16, u16), term_stdout: &console::Term) -> usize { | ||
fn guess_real_width(current_width: u16, term_stdout: &console::Term) -> Option<u16> { | ||
use std::process::{Command, Stdio}; | ||
|
||
let term_var = std::env::var("TERM").ok()?; | ||
// More checks before actually calling stty. | ||
if term_var.starts_with("xterm") | ||
&& term_stdout.is_term() | ||
&& term_stdout.features().is_msys_tty() | ||
{ | ||
if std::env::var(NO_WORKAROUNDS).is_ok() { | ||
return Some(current_width); | ||
} | ||
|
||
// stderr/2 is passed to the Command below. | ||
let pseudo_term = "/dev/fd/2"; | ||
|
||
// Read width via stty helper program (e.g. "C:\Program Files\Git\usr\bin\stty.exe") | ||
// which gets both the MSYS2 and cmd.exe width right. | ||
let result = Command::new("stty") | ||
.stderr(Stdio::inherit()) | ||
.arg("-F") | ||
.arg(pseudo_term) | ||
.arg("size") | ||
.output() | ||
.ok()?; | ||
|
||
if result.status.success() { | ||
let size = std::str::from_utf8(&result.stdout).ok()?; | ||
let mut it = size.split_whitespace(); | ||
let _height = it.next()?; | ||
return it.next().map(|width| width.parse().ok())?; | ||
} | ||
} | ||
None | ||
} | ||
|
||
// Calling an external binary is slow, so make sure this is actually necessary. | ||
// The fallback values of 25 lines by 80 columns (sometimes zero indexed) are a good | ||
// indicator. | ||
let (height, width) = height_width; | ||
match (height, width) { | ||
8000 | (24..=25, 79..=80) => guess_real_width(width, term_stdout).unwrap_or(width), | |
_ => width, | ||
} | ||
.into() | ||
} | ||
|
||
#[cfg(not(target_os = "windows"))] | ||
pub fn windows_msys2_width_fix(height_width: (u16, u16), _: &console::Term) -> usize { | ||
let _ = NO_WORKAROUNDS; | ||
height_width.1.into() | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What's this for?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just so this const &str does not trigger an unused variable warning in the non-windows case.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah of course, thanks.