Format errors more consistently

pull/907/head
Wilfred Hughes 2025-10-22 01:03:01 +07:00
parent a96ed2de96
commit 6f47e787ab
3 changed files with 37 additions and 21 deletions

@ -457,6 +457,19 @@ pub(crate) fn print_warning(s: &str, display_options: &DisplayOptions) {
eprint!("{}\n\n", s);
}
/// Style `s` as an error and write to stderr.
pub(crate) fn print_error(s: &str, use_color: bool) {
// TODO: this is inconsistent with print_warning regarding
// arguments and trailing whitespace.
let prefix = if use_color {
"error: ".red().bold().to_string()
} else {
"error: ".to_owned()
};
eprintln!("{}{}", prefix, s);
}
pub(crate) fn apply_line_number_color(
s: &str,
is_novel: bool,

@ -59,7 +59,6 @@ mod words;
#[macro_use]
extern crate log;
use crossterm::tty::IsTty as _;
use display::style::print_warning;
use log::info;
use options::FilePermissions;
@ -72,6 +71,7 @@ use crate::diff::dijkstra::ExceededGraphLimit;
use crate::diff::{dijkstra, unchanged};
use crate::display::context::opposite_positions;
use crate::display::hunks::{matched_pos_to_hunks, merge_adjacent};
use crate::display::style::print_error;
use crate::exit_codes::EXIT_BAD_ARGUMENTS;
use crate::exit_codes::{EXIT_FOUND_CHANGES, EXIT_SUCCESS};
use crate::files::{
@ -500,7 +500,10 @@ fn diff_conflicts_file(
let mut src = match guess_content(&bytes, path, binary_overrides) {
ProbableFileKind::Text(src) => src,
ProbableFileKind::Binary => {
eprintln!("error: Expected a text file with conflict markers, got a binary file.");
print_error(
"Expected a text file with conflict markers, got a binary file.",
display_options.use_color,
);
std::process::exit(EXIT_BAD_ARGUMENTS);
}
};
@ -512,20 +515,22 @@ fn diff_conflicts_file(
let conflict_files = match apply_conflict_markers(&src) {
Ok(cf) => cf,
Err(msg) => {
eprintln!("error: {}", msg);
print_error(&msg, display_options.use_color);
std::process::exit(EXIT_BAD_ARGUMENTS);
}
};
if conflict_files.num_conflicts == 0 {
eprintln!(
"{}: Difftastic requires two paths, or a single file with conflict markers {}.\n",
if std::io::stdout().is_tty() {
"error".red().bold().to_string()
} else {
"error".to_owned()
},
START_LHS_MARKER,
print_error(
&format!(
"Difftastic requires two paths, or a single file with conflict markers {}.\n",
if display_options.use_color {
START_LHS_MARKER.bold().to_string()
} else {
START_LHS_MARKER.to_owned()
}
),
display_options.use_color,
);
eprintln!("USAGE:\n\n {}\n", USAGE);

@ -12,7 +12,7 @@ use crossterm::tty::IsTty;
use owo_colors::OwoColorize as _;
use crate::{
display::style::BackgroundColor,
display::style::{print_error, BackgroundColor},
exit_codes::EXIT_BAD_ARGUMENTS,
parse::guess_language::{language_override_from_name, LanguageOverride},
version::VERSION,
@ -957,15 +957,13 @@ pub(crate) fn parse_args() -> Mode {
}
_ => {
if !args.is_empty() {
eprintln!(
"{}: Difftastic does not support being called with {} argument{}.\n",
if use_color {
"error".red().bold().to_string()
} else {
"error".to_owned()
},
args.len(),
if args.len() == 1 { "" } else { "s" }
print_error(
&format!(
"Difftastic does not support being called with {} argument{}.\n",
args.len(),
if args.len() == 1 { "" } else { "s" }
),
use_color,
);
}
eprintln!("USAGE:\n\n {}\n", USAGE);