Skip to content

Commit

Permalink
Fixing issue with termsize::get when compiled in release mode
Browse files Browse the repository at this point in the history
The function is erroneously returnin zero
See softprops/termsize#19 for details
  • Loading branch information
Yomguithereal committed May 31, 2024
1 parent 0310e50 commit 8e0a469
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 4 deletions.
5 changes: 3 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ flate2 = "1.0.27"
glob = "0.3.1"
indicatif = "0.17.8"
lazy_static = "1.4.0"
libc = "0.2.155"
md5 = "0.7.0"
numfmt = "1.1.1"
num_cpus = "1.4"
Expand Down
52 changes: 52 additions & 0 deletions src/fixed_termsize.rs
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()
}
2 changes: 2 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ extern crate rand_seeder;
extern crate ratatui;
#[macro_use]
extern crate lazy_static;
extern crate libc;
extern crate num_cpus;
extern crate numfmt;
#[cfg(not(windows))]
Expand Down Expand Up @@ -133,6 +134,7 @@ macro_rules! command_list {

mod cmd;
mod config;
mod fixed_termsize;
mod index;
mod json;
mod moonblade;
Expand Down
5 changes: 3 additions & 2 deletions src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ use unicode_segmentation::UnicodeSegmentation;
use unicode_width::UnicodeWidthStr;

use crate::config::{Config, Delimiter};
use crate::fixed_termsize;
use crate::select::SelectColumns;
use crate::CliResult;

Expand Down Expand Up @@ -300,7 +301,7 @@ pub fn acquire_stty_size() -> Option<termsize::Size> {

pub fn acquire_term_cols(cols_override: &Option<usize>) -> usize {
match cols_override {
None => match termsize::get() {
None => match fixed_termsize::get() {
None => match acquire_stty_size() {
None => 80,
Some(size) => size.cols as usize,
Expand All @@ -312,7 +313,7 @@ pub fn acquire_term_cols(cols_override: &Option<usize>) -> usize {
}

pub fn acquire_term_rows() -> Option<usize> {
termsize::get().map(|size| size.rows as usize)
fixed_termsize::get().map(|size| size.rows as usize)
}

pub fn pretty_print_float<T: Numeric>(f: &mut Formatter, x: T) -> String {
Expand Down

0 comments on commit 8e0a469

Please sign in to comment.