-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixing issue with termsize::get when compiled in release mode
The function is erroneously returnin zero See softprops/termsize#19 for details
- Loading branch information
1 parent
0310e50
commit 8e0a469
Showing
5 changed files
with
61 additions
and
4 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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 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,52 @@ | ||
use libc::ioctl; | ||
use libc::{c_ushort, STDOUT_FILENO, TIOCGWINSZ}; | ||
use termsize::Size; | ||
|
||
// NOTE: the `termsize` library has merged a fix but is not released | ||
// See: https://github.com/softprops/termsize/issues/19 | ||
|
||
/// A representation of the size of the current terminal | ||
#[repr(C)] | ||
#[derive(Debug)] | ||
struct UnixSize { | ||
/// number of rows | ||
pub rows: c_ushort, | ||
/// number of columns | ||
pub cols: c_ushort, | ||
x: c_ushort, | ||
y: c_ushort, | ||
} | ||
|
||
/// Gets the current terminal size | ||
#[cfg(unix)] | ||
fn fixed_nix_get() -> Option<Size> { | ||
// http://rosettacode.org/wiki/Terminal_control/Dimensions#Library:_BSD_libc | ||
if atty::isnt(atty::Stream::Stdout) { | ||
return None; | ||
} | ||
let mut us = UnixSize { | ||
rows: 0, | ||
cols: 0, | ||
x: 0, | ||
y: 0, | ||
}; | ||
let r = unsafe { ioctl(STDOUT_FILENO, TIOCGWINSZ.into(), &mut us) }; | ||
if r == 0 { | ||
Some(Size { | ||
rows: us.rows, | ||
cols: us.cols, | ||
}) | ||
} else { | ||
None | ||
} | ||
} | ||
|
||
#[cfg(unix)] | ||
pub fn get() -> Option<Size> { | ||
fixed_nix_get() | ||
} | ||
|
||
#[cfg(not(unix))] | ||
pub fn get() -> Option<Size> { | ||
termsize::get() | ||
} |
This file contains 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 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