Skip to content

Commit

Permalink
custom command format
Browse files Browse the repository at this point in the history
  • Loading branch information
jpikl committed Apr 5, 2024
1 parent 45d4436 commit 695dfd8
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 18 deletions.
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@
"editor.formatOnSave": true,
"rust-analyzer.check.command": "clippy",
"rust-analyzer.rustfmt.extraArgs": ["+nightly"],
"[svg]": {
"editor.formatOnSave": false,
},
}
1 change: 0 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
3 changes: 2 additions & 1 deletion docs/reference/rew.md
Original file line number Diff line number Diff line change
@@ -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

Expand Down
2 changes: 1 addition & 1 deletion snapshots/error.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
39 changes: 25 additions & 14 deletions src/commands/x.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
}
Expand Down Expand Up @@ -399,24 +399,35 @@ impl EvalContext {
}
}

#[cfg(not(debug_assertions))]
fn format_command(command: &Command) -> String {
format!("{command:?}")
}
fn format_command(command: &Command) -> Result<String> {
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, "<masked_path>/").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<T> {
Expand Down

0 comments on commit 695dfd8

Please sign in to comment.