argument: Use terminal_size crate for getting terminal size.

BUG=None
TEST=crosvm help  # reflows at width even in my emacs shell.

Change-Id: I18afb7e359f369d1a0f5291bb7386f84769efe81
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/3556929
Reviewed-by: Alexandre Courbot <acourbot@chromium.org>
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
Tested-by: kokoro <noreply+kokoro@google.com>
Commit-Queue: Junichi Uekawa <uekawa@chromium.org>
This commit is contained in:
Junichi Uekawa 2022-03-29 14:14:25 +09:00 committed by Chromeos LUCI
parent 5b414d97ec
commit 3c9351b446
2 changed files with 5 additions and 14 deletions

View file

@ -184,6 +184,7 @@ serde_json = "*"
serde_keyvalue = { path = "serde_keyvalue" }
sync = { path = "common/sync" }
tempfile = "3"
terminal_size = "0.1.17"
thiserror = { version = "1.0.20" }
uuid = { version = "0.8.2" }
vhost = { path = "vhost" }

View file

@ -44,11 +44,11 @@
//! ```
use std::convert::TryFrom;
use std::env;
use std::result;
use std::str::FromStr;
use remain::sorted;
use terminal_size::{terminal_size, Width};
use thiserror::Error;
/// An error with argument parsing.
@ -361,12 +361,10 @@ where
const DEFAULT_COLUMNS: usize = 80;
/// Get the number of columns on a display.
/// Get the number of columns on a display, with a reasonable default.
fn get_columns() -> usize {
if let Ok(columns_string) = env::var("COLUMNS") {
if let Ok(columns) = usize::from_str(&columns_string) {
return columns;
}
if let Some((Width(columns), _)) = terminal_size() {
return columns.into();
}
DEFAULT_COLUMNS
}
@ -851,12 +849,4 @@ I am going to give you another paragraph. However I don't know if it is useful",
is useful"
);
}
#[test]
fn column() {
env::set_var("COLUMNS", "100");
assert_eq!(get_columns(), 100);
env::remove_var("COLUMNS");
assert_eq!(get_columns(), 80);
}
}