Skip to content

Commit ade458a

Browse files
committed
ui: Add VMTEST_NO_UI environment variable
This disable the terminal drawing UI in vmtest. Previous workaround was `vmtest ... | cat -`. But that's only obvious to people who are deeply familiar with linux/unix. So add a more mundane environment variable and document it.
1 parent e158ade commit ade458a

File tree

2 files changed

+19
-5
lines changed

2 files changed

+19
-5
lines changed

src/main.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,12 @@ use regex::Regex;
1313

1414
use vmtest::{Config, Target, Ui, VMConfig, Vmtest};
1515

16+
const HELP_ENV_VARS: &str = r#"Environment variables:
17+
VMTEST_NO_UI Set to disable UI [default: unset]
18+
"#;
19+
1620
#[derive(Parser, Debug)]
17-
#[clap(version)]
21+
#[clap(version, disable_colored_help=true, after_help=HELP_ENV_VARS)]
1822
struct Args {
1923
/// Path to config file
2024
#[clap(short, long)]

src/ui.rs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use std::cmp::min;
2+
use std::env;
23
use std::sync::mpsc::{channel, Receiver};
34
use std::thread;
45

@@ -26,9 +27,18 @@ struct Stage {
2627
expand: bool,
2728
}
2829

30+
/// Returns whether the terminal is attended or not
31+
fn is_attended(term: &Term) -> bool {
32+
if env::var_os("VMTEST_NO_UI").is_some() {
33+
return false;
34+
}
35+
36+
term.features().is_attended()
37+
}
38+
2939
/// Helper to clear lines depending on whether or not a tty is attached
3040
fn clear_last_lines(term: &Term, n: usize) {
31-
if term.features().is_attended() {
41+
if is_attended(term) {
3242
term.clear_last_lines(n).unwrap();
3343
}
3444
}
@@ -68,7 +78,7 @@ impl Stage {
6878
/// We kinda hack this to return 1 if we're not writing to terminal.
6979
/// Should really find a cleaner solution.
7080
fn window_size(&self) -> usize {
71-
if self.term.features().is_attended() {
81+
if is_attended(&self.term) {
7282
min(self.lines.len(), WINDOW_LENGTH)
7383
} else {
7484
min(self.lines.len(), 1)
@@ -91,7 +101,7 @@ impl Stage {
91101

92102
// Compute new visible lines
93103
let trimmed_line = line.trim_end();
94-
let styled_line = if self.term.features().is_attended() {
104+
let styled_line = if is_attended(&self.term) {
95105
// If output is attended, we do custom window with our own styling.
96106
// Therefore, we need to strip away any existing formatting.
97107
let stripped = strip_ansi_codes(trimmed_line);
@@ -133,7 +143,7 @@ impl Stage {
133143
impl Drop for Stage {
134144
fn drop(&mut self) {
135145
clear_last_lines(&self.term, self.window_size());
136-
if self.expand && self.term.features().is_attended() {
146+
if self.expand && is_attended(&self.term) {
137147
for line in &self.lines {
138148
self.term
139149
.write_line(line)

0 commit comments

Comments
 (0)