From 695dfd8a38954595a750460c7b5dabd087cf1609 Mon Sep 17 00:00:00 2001 From: Jan Pikl Date: Fri, 5 Apr 2024 22:49:41 +0200 Subject: [PATCH] custom command format --- .vscode/settings.json | 3 +++ Cargo.lock | 1 - Cargo.toml | 1 - docs/reference/rew.md | 3 ++- snapshots/error.svg | 2 +- src/commands/x.rs | 39 +++++++++++++++++++++++++-------------- 6 files changed, 31 insertions(+), 18 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index 21ba9ad..782795b 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -2,4 +2,7 @@ "editor.formatOnSave": true, "rust-analyzer.check.command": "clippy", "rust-analyzer.rustfmt.extraArgs": ["+nightly"], + "[svg]": { + "editor.formatOnSave": false, + }, } diff --git a/Cargo.lock b/Cargo.lock index f071b87..7d9485b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -696,7 +696,6 @@ dependencies = [ "deunicode", "itoap", "memchr", - "regex", "rstest", "term-transcript", "terminal_size", diff --git a/Cargo.toml b/Cargo.toml index d46e287..d84c2e4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -37,7 +37,6 @@ derive_more = { version = "1.0.0-beta.6", features = [ deunicode = "1.4.2" itoap = "1.0.1" memchr = "2.7.1" -regex = "1.10.4" terminal_size = "0.3.0" unicode-width = "0.1.11" which = "6.0.0" diff --git a/docs/reference/rew.md b/docs/reference/rew.md index 790ab0f..1f63677 100644 --- a/docs/reference/rew.md +++ b/docs/reference/rew.md @@ -1,6 +1,7 @@ # rew -The Swiss Army Knife of line-oriented text processing. Transform text by composing parallel shell pipelines! +The Swiss Army Knife of line-oriented text processing. +Transform text by composing parallel shell pipelines! ## Usage diff --git a/snapshots/error.svg b/snapshots/error.svg index f80c48a..1955971 100644 --- a/snapshots/error.svg +++ b/snapshots/error.svg @@ -69,7 +69,7 @@
$ rew x "{cat --unknown}"
rew cat (spawned by 'rew x'): invalid usage: unexpected argument found
 rew x: error: failed execution of expression {cat --unknown}
-rew x: └─> failed execution of command REW_BUF_MODE="full" REW_BUF_SIZE="32768" 
REW_NULL="false" _REW_SPAWNED_BY="rew x" "<masked_path>/rew" "cat" "--unknown"
+rew x: └─> failed execution of command REW_BUF_MODE="full" REW_BUF_SIZE="32768"
REW_NULL="false" _REW_SPAWNED_BY="rew x" "rew" "cat" "--unknown"
rew x: └─> child process exited with code 2
diff --git a/src/commands/x.rs b/src/commands/x.rs index 808ffc6..871b576 100644 --- a/src/commands/x.rs +++ b/src/commands/x.rs @@ -364,7 +364,7 @@ struct EvalContext { impl EvalContext { fn from_command(command: &Command) -> Self { Self { - raw_command: Some(format_command(command)), + raw_command: Some(format_command(command).unwrap_or_else(|_| format!("{command:?}"))), raw_expr: None, } } @@ -399,24 +399,35 @@ impl EvalContext { } } -#[cfg(not(debug_assertions))] -fn format_command(command: &Command) -> String { - format!("{command:?}") -} +fn format_command(command: &Command) -> Result { + use std::fmt::Write; + let mut output = String::new(); -#[cfg(debug_assertions)] -fn format_command(command: &Command) -> String { - let output = format!("{command:?}"); + for (key, val) in command.get_envs() { + let key = key.to_string_lossy(); + let val = val.unwrap_or_default(); + write!(&mut output, "{key}={val:?} ",)?; + } + // We want to obfuscate program path to make "transcript" tests reproducible. + #[cfg(debug_assertions)] if env::var_os("NEXTEST").is_some() { - // We want to obfuscate fs paths to make "transcript" tests reproducible. - let separator = std::path::MAIN_SEPARATOR_STR.replace('\\', "\\\\"); - let pattern = format!("[^\"]*{separator}",); - let regex = regex::Regex::new(&pattern).unwrap(); - regex.replace(&output, "/").to_string() + let program = std::path::Path::new(command.get_program()) + .file_stem() + .unwrap_or_default(); + write!(&mut output, "{program:?}")?; } else { - output + write!(&mut output, "{:?}", command.get_program())?; + } + + #[cfg(not(debug_assertions))] + write!(&mut output, "{:?}", command.get_program())?; + + for arg in command.get_args() { + write!(&mut output, " {arg:?}")?; } + + Ok(output) } trait WithEvalContext {