Skip to content

Commit

Permalink
Display stdin CLI arguments as "(stdin)"
Browse files Browse the repository at this point in the history
This improves display for #389, and makes language detection use
pattern matching on FileArgument rather than comparing literal
strings.
  • Loading branch information
Wilfred committed Oct 14, 2022
1 parent b4ff28c commit 02f1cca
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 19 deletions.
19 changes: 13 additions & 6 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,8 @@ fn diff_file(
diff_file_content(
lhs_display_path,
rhs_display_path,
lhs_path,
rhs_path,
&lhs_bytes,
&rhs_bytes,
display_options.tab_width,
Expand All @@ -246,6 +248,8 @@ fn diff_file(
fn diff_file_content(
lhs_display_path: &str,
rhs_display_path: &str,
_lhs_path: &FileArgument,
rhs_path: &FileArgument,
lhs_bytes: &[u8],
rhs_bytes: &[u8],
tab_width: usize,
Expand Down Expand Up @@ -283,13 +287,12 @@ fn diff_file_content(
rhs_src.pop();
}

// Prefer the RHS path for language detection, unless it's /dev/null.
let (guess_src, guess_path) = if rhs_display_path == "/dev/null" || rhs_display_path == "-" {
// TODO: take a Path directly instead.
(&lhs_src, Path::new(&lhs_display_path))
} else {
(&rhs_src, Path::new(&rhs_display_path))
let (guess_src, guess_path) = match rhs_path {
FileArgument::NamedPath(_) => (&rhs_src, Path::new(&rhs_display_path)),
FileArgument::Stdin => (&rhs_src, Path::new(&lhs_display_path)),
FileArgument::DevNull => (&lhs_src, Path::new(&lhs_display_path)),
};

let language = language_override.or_else(|| guess(guess_path, guess_src));
let lang_config = language.map(tsp::from_language);

Expand Down Expand Up @@ -549,6 +552,8 @@ fn print_diff_result(display_options: &DisplayOptions, summary: &DiffResult) {

#[cfg(test)]
mod tests {
use std::ffi::OsStr;

use super::*;
use crate::options::{DEFAULT_BYTE_LIMIT, DEFAULT_GRAPH_LIMIT, DEFAULT_TAB_WIDTH};

Expand All @@ -558,6 +563,8 @@ mod tests {
let res = diff_file_content(
"foo.el",
"foo.el",
&FileArgument::from_path_argument(OsStr::new("foo.el")),
&FileArgument::from_path_argument(OsStr::new("foo.el")),
s.as_bytes(),
s.as_bytes(),
DEFAULT_TAB_WIDTH,
Expand Down
30 changes: 17 additions & 13 deletions src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -314,18 +314,22 @@ pub fn parse_args() -> Mode {

// TODO: document these different ways of calling difftastic.
let (lhs_display_path, rhs_display_path, lhs_path, rhs_path, in_vcs) = match &args[..] {
[lhs_path, rhs_path] => (
lhs_path.to_owned(),
rhs_path.to_owned(),
FileArgument::from_cli_argument(lhs_path),
FileArgument::from_cli_argument(rhs_path),
false,
),
[lhs_path, rhs_path] => {
let lhs_arg = FileArgument::from_cli_argument(lhs_path);
let rhs_arg = FileArgument::from_cli_argument(rhs_path);
(
lhs_arg.display(),
rhs_arg.display(),
lhs_arg,
rhs_arg,
false,
)
}
[display_path, lhs_tmp_file, _lhs_hash, _lhs_mode, rhs_tmp_file, _rhs_hash, _rhs_mode] => {
// https://git-scm.com/docs/git#Documentation/git.txt-codeGITEXTERNALDIFFcode
(
display_path.to_owned(),
display_path.to_owned(),
display_path.to_string_lossy().to_string(),
display_path.to_string_lossy().to_string(),
FileArgument::from_path_argument(lhs_tmp_file),
FileArgument::from_path_argument(rhs_tmp_file),
true,
Expand All @@ -336,8 +340,8 @@ pub fn parse_args() -> Mode {
// Rename file.
// TODO: where does git document these 9 arguments?
(
old_name.to_owned(),
new_name.to_owned(),
old_name.to_string_lossy().to_string(),
new_name.to_string_lossy().to_string(),
FileArgument::from_path_argument(lhs_tmp_file),
FileArgument::from_path_argument(rhs_tmp_file),
true,
Expand Down Expand Up @@ -439,8 +443,8 @@ pub fn parse_args() -> Mode {
language_override,
lhs_path,
rhs_path,
lhs_display_path: lhs_display_path.to_string_lossy().to_string(),
rhs_display_path: rhs_display_path.to_string_lossy().to_string(),
lhs_display_path,
rhs_display_path,
}
}

Expand Down

0 comments on commit 02f1cca

Please sign in to comment.