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); 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( pub(crate) fn apply_line_number_color(
s: &str, s: &str,
is_novel: bool, is_novel: bool,

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

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