Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ui: Usability improvements #110

Merged
merged 3 commits into from
Jan 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,12 @@ use regex::Regex;

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

const HELP_ENV_VARS: &str = r#"Environment variables:
VMTEST_NO_UI Set to disable UI [default: unset]
"#;

#[derive(Parser, Debug)]
#[clap(version)]
#[clap(version, disable_colored_help=true, after_help=HELP_ENV_VARS)]
Comment on lines +16 to +21
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

clap supports both parameter and env variable by setting env in the argument derive. https://stackoverflow.com/a/73531388

This means that the value of args.dry_run could be set either from the CLI with --dry-run or from the environment with DRY_RUN=1, not exactly what you are trying to do, but mentioning htis in case.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was trying to avoid needing to thread the variable through a bunch of the code (which would require a larger refactor). Seemed ok enough to just bypass with env var :P

struct Args {
/// Path to config file
#[clap(short, long)]
Expand Down
58 changes: 41 additions & 17 deletions src/ui.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::cmp::min;
use std::env;
use std::sync::mpsc::{channel, Receiver};
use std::thread;

Expand Down Expand Up @@ -26,9 +27,19 @@ struct Stage {
expand: bool,
}

/// Helper to clear lines depending on whether or not a tty is attached
/// Returns whether the "windowed" UI is enabled or not.
/// If disabled, output is (mostly) just passed through.
fn ui_enabled(term: &Term) -> bool {
if env::var_os("VMTEST_NO_UI").is_some() {
return false;
}

term.features().is_attended()
}

/// Helper to clear lines depending on whether or not UI is enabled
fn clear_last_lines(term: &Term, n: usize) {
if term.features().is_attended() {
if ui_enabled(term) {
term.clear_last_lines(n).unwrap();
}
}
Expand Down Expand Up @@ -68,7 +79,7 @@ impl Stage {
/// We kinda hack this to return 1 if we're not writing to terminal.
/// Should really find a cleaner solution.
fn window_size(&self) -> usize {
if self.term.features().is_attended() {
if ui_enabled(&self.term) {
min(self.lines.len(), WINDOW_LENGTH)
} else {
min(self.lines.len(), 1)
Expand All @@ -91,25 +102,38 @@ impl Stage {

// Compute new visible lines
let trimmed_line = line.trim_end();
let stripped_line = strip_ansi_codes(trimmed_line);
let styled_line = match &custom {
Some(s) => s.apply_to(stripped_line),
None => style(stripped_line).dim(),
let styled_line = if ui_enabled(&self.term) {
// If UI is enabled, we do custom window with our own styling.
// Therefore, we need to strip away any existing formatting.
let stripped = strip_ansi_codes(trimmed_line);

// Clip output to fit in terminal.
//
// Note this _does_not_ handle characters that expand to multiple columns,
// like tabs or other fancy unicode. This is known to corrupt UI visuals. There
// is no real solution to this without doing a mini terminal emulator AFAIK.
// The workaround is to set VMTEST_NO_UI.
let width = self.term.size_checked().map(|(_, w)| w).unwrap_or(u16::MAX);
let clipped = truncate_str(&stripped, width as usize, "...");

// Apply styling
let styled = match &custom {
Some(s) => s.apply_to(clipped),
None => style(clipped).dim(),
};

styled.to_string()
} else {
// If output is not attended, we do pass through
trimmed_line.to_string()
};
self.lines.push(styled_line.to_string());
self.lines.push(styled_line);
// Unwrap should never fail b/c we're sizing with `min()`
let window = self.lines.windows(self.window_size()).last().unwrap();

// Get terminal width, if any
let width = match self.term.size_checked() {
Some((_, w)) => w,
_ => u16::MAX,
};

// Print visible lines
for line in window {
let clipped = truncate_str(line, width as usize - 3, "...");
self.term.write_line(&format!("{}", clipped)).unwrap();
self.term.write_line(line).unwrap();
}
}

Expand All @@ -125,7 +149,7 @@ impl Stage {
impl Drop for Stage {
fn drop(&mut self) {
clear_last_lines(&self.term, self.window_size());
if self.expand && self.term.features().is_attended() {
if self.expand && ui_enabled(&self.term) {
for line in &self.lines {
self.term
.write_line(line)
Expand Down
Loading