Skip to content

Commit

Permalink
Merge pull request #26 from jclulow/illumos
Browse files Browse the repository at this point in the history
add test support for illumos systems
  • Loading branch information
eminence authored Apr 19, 2021
2 parents 6875333 + fa3aefc commit 6f9e4f7
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 21 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ terminal-size

Rust library to getting the size of your terminal.

Works on Linux, MacOS, and Windows.
Works on Linux, MacOS, Windows, and illumos.

```rust
use terminal_size::{Width, Height, terminal_size};
Expand Down
74 changes: 54 additions & 20 deletions src/unix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,30 +48,64 @@ fn compare_with_stty() {
use std::process::Command;
use std::process::Stdio;

let output = if cfg!(target_os = "linux") {
Command::new("stty")
.arg("size")
.arg("-F")
.arg("/dev/stderr")
.stderr(Stdio::inherit())
let (rows, cols) = if cfg!(target_os = "illumos") {
// illumos stty(1) does not accept a device argument, instead using
// stdin unconditionally:
let output = Command::new("stty")
.stdin(Stdio::inherit())
.output()
.unwrap();
assert!(output.status.success());

// stdout includes the row and columns thus: "rows = 80; columns = 24;"
let vals = String::from_utf8(output.stdout)
.unwrap()
.lines()
.map(|line| {
// Split each line on semicolons to get "k = v" strings:
line.split(';')
.map(str::trim)
.map(str::to_string)
.collect::<Vec<_>>()
})
.flatten()
.filter_map(|term| {
// split each "k = v" string and look for rows/columns:
match term.splitn(2, " = ").collect::<Vec<_>>().as_slice() {
["rows", n] | ["columns", n] => Some(n.parse().unwrap()),
_ => None,
}
})
.collect::<Vec<_>>();
(vals[0], vals[1])
} else {
Command::new("stty")
.arg("-f")
.arg("/dev/stderr")
.arg("size")
.stderr(Stdio::inherit())
.output()
.unwrap()
let output = if cfg!(target_os = "linux") {
Command::new("stty")
.arg("size")
.arg("-F")
.arg("/dev/stderr")
.stderr(Stdio::inherit())
.output()
.unwrap()
} else {
Command::new("stty")
.arg("-f")
.arg("/dev/stderr")
.arg("size")
.stderr(Stdio::inherit())
.output()
.unwrap()
};

assert!(output.status.success());
let stdout = String::from_utf8(output.stdout).unwrap();
// stdout is "rows cols"
let mut data = stdout.split_whitespace();
println!("{}", stdout);
let rows = u16::from_str_radix(data.next().unwrap(), 10).unwrap();
let cols = u16::from_str_radix(data.next().unwrap(), 10).unwrap();
(rows, cols)
};
let stdout = String::from_utf8(output.stdout).unwrap();
assert!(output.status.success());
// stdout is "rows cols"
let mut data = stdout.split_whitespace();
let rows = u16::from_str_radix(data.next().unwrap(), 10).unwrap();
let cols = u16::from_str_radix(data.next().unwrap(), 10).unwrap();
println!("{}", stdout);
println!("{} {}", rows, cols);

if let Some((Width(w), Height(h))) = terminal_size() {
Expand Down

0 comments on commit 6f9e4f7

Please sign in to comment.