Skip to content

Commit

Permalink
Adapt to updated linter rules
Browse files Browse the repository at this point in the history
  • Loading branch information
walles committed Feb 7, 2023
1 parent 9a8e961 commit f6c6807
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 39 deletions.
17 changes: 7 additions & 10 deletions src/commit_line.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ pub fn format_commit_line(line: &str, highlight_background: bool) -> String {
let parts = line.split('(').collect::<Vec<_>>();
if parts.len() == 1 {
// Just "commit: 123abc", color it all yellow
return format!("{}{}{}", header, line, NORMAL);
return format!("{header}{line}{NORMAL}");
}

let commit_part = parts[0].trim();
let without_trailing_parenthesis = parts[1].strip_suffix(')');
if without_trailing_parenthesis.is_none() {
// No final parenthesis, this is weird, fall back to showing everything
// in yellow
return format!("{}{}{}", header, line, NORMAL);
return format!("{header}{line}{NORMAL}");
}

let parenthesis_parts = without_trailing_parenthesis
Expand All @@ -30,7 +30,7 @@ pub fn format_commit_line(line: &str, highlight_background: bool) -> String {
.collect_vec();
let current_branch = compute_current_branch(&parenthesis_parts);

let comma = format!("{}, ", YELLOW);
let comma = format!("{YELLOW}, ");
return format!(
"{}{} ({}{}){}",
header,
Expand All @@ -48,26 +48,23 @@ fn format_commit_part(part: &str, current_branch: &Option<String>) -> String {
if part.starts_with("tag: ") {
// Implicitly yellow since both the commas and the surrounding
// parentheses are also yellow.
return format!("{}{}{}", BOLD, part, NORMAL_INTENSITY);
return format!("{BOLD}{part}{NORMAL_INTENSITY}");
}

// FIXME: Can we do this with one readable if-statement instead?
if let Some(current_branch_4_realz) = current_branch {
if current_branch_4_realz == part {
return format!("{}{}{}{}", BOLD, GREEN, part, NORMAL_INTENSITY);
return format!("{BOLD}{GREEN}{part}{NORMAL_INTENSITY}");
}
}

// Handle "HEAD -> current_branch"
if let Some(head_branch) = part.strip_prefix("HEAD -> ") {
return format!(
"{}{}HEAD -> {}{}{}",
BOLD, CYAN, GREEN, head_branch, NORMAL_INTENSITY
);
return format!("{BOLD}{CYAN}HEAD -> {GREEN}{head_branch}{NORMAL_INTENSITY}");
}

// Assume this is a branch, but not the current one
return format!("{}{}{}{}", BOLD, RED, part, NORMAL_INTENSITY);
return format!("{BOLD}{RED}{part}{NORMAL_INTENSITY}");
}

fn compute_current_branch(candidates: &Vec<&str>) -> Option<String> {
Expand Down
21 changes: 9 additions & 12 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,20 +187,20 @@ fn panic_handler(panic_info: &panic::PanicInfo) {
eprintln!("\n\n-v-v-v----------- RIFF CRASHED ---------------v-v-v-\n",);

// Panic message
eprintln!("Panic message: <{:#?}>", panic_info);
eprintln!("Panic message: <{panic_info:#?}>");
eprintln!();

// Backtrace
eprintln!("{:?}", Backtrace::new());

eprintln!("Riff version: {}", GIT_VERSION);
eprintln!("Riff version: {GIT_VERSION}");

eprintln!();
eprintln!("Command line arguments: {:?}", env::args());

eprintln!("\n-^-^-^------- END OF RIFF CRASH REPORT -------^-^-^-\n",);

eprintln!("{}", CRASH_FOOTER);
eprintln!("{CRASH_FOOTER}");
}

fn highlight_stream(input: &mut dyn io::Read, no_pager: bool) {
Expand Down Expand Up @@ -299,15 +299,12 @@ fn exec_diff_highlight(path1: &str, path2: &str, ignore_space_change: bool, no_p
.arg(path2)
.stdout(Stdio::piped());

let pretty_command = format!("{:#?}", command);
let pretty_command = format!("{command:#?}");
let mut diff_subprocess: std::process::Child;
match command.spawn() {
Ok(subprocess) => diff_subprocess = subprocess,
Err(err) => {
eprintln!(
"ERROR: Spawning diff failed:\n {}\n {}\n",
pretty_command, err
);
eprintln!("ERROR: Spawning diff failed:\n {pretty_command}\n {err}\n");
exit(1);
}
}
Expand All @@ -320,7 +317,7 @@ fn exec_diff_highlight(path1: &str, path2: &str, ignore_space_change: bool, no_p
if diff_exit_code != 0 && diff_exit_code != 1 {
// diff exit code was neither 0 (comparees identical) or 1 (differences
// found), this means trouble.
eprintln!("Exit code {}: {}", diff_exit_code, pretty_command);
eprintln!("Exit code {diff_exit_code}: {pretty_command}");
exit(diff_exit_code);
}
}
Expand All @@ -337,7 +334,7 @@ fn main() {
}

if consume("--version", &mut args) {
println!("riff {}", GIT_VERSION);
println!("riff {GIT_VERSION}");
println!();
println!("Source code available at <https://github.com/walles/riff>.");
return;
Expand Down Expand Up @@ -372,7 +369,7 @@ fn main() {
}

if args.len() != 1 {
eprintln!("ERROR: Unknown command line: {:?}", args);
eprintln!("ERROR: Unknown command line: {args:?}");
eprintln!();
print_help(&mut io::stderr());
exit(1);
Expand All @@ -399,7 +396,7 @@ mod tests {
use pretty_assertions::assert_eq;

fn new(text: &str) -> String {
return format!("{}{}{}", NEW, text, NORMAL);
return format!("{NEW}{text}{NORMAL}");
}

#[test]
Expand Down
22 changes: 8 additions & 14 deletions src/refiner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,7 @@ fn simple_format(old_text: &str, new_text: &str) -> (Vec<String>, Vec<String>) {
}
if (!old_text.is_empty()) && !old_text.ends_with('\n') {
old_lines.push(format!(
"{}{}{}",
NO_EOF_NEWLINE_COLOR, NO_EOF_NEWLINE_MARKER, NORMAL
"{NO_EOF_NEWLINE_COLOR}{NO_EOF_NEWLINE_MARKER}{NORMAL}"
));
}

Expand All @@ -45,8 +44,7 @@ fn simple_format(old_text: &str, new_text: &str) -> (Vec<String>, Vec<String>) {
}
if (!new_text.is_empty()) && !new_text.ends_with('\n') {
new_lines.push(format!(
"{}{}{}",
NO_EOF_NEWLINE_COLOR, NO_EOF_NEWLINE_MARKER, NORMAL
"{NO_EOF_NEWLINE_COLOR}{NO_EOF_NEWLINE_MARKER}{NORMAL}"
));
}

Expand Down Expand Up @@ -161,8 +159,7 @@ fn to_lines(old: &str, new: &str) -> (Vec<String>, Vec<String>) {
}
if (!old.is_empty()) && !old.ends_with('\n') {
old_lines.push(format!(
"{}{}{}",
NO_EOF_NEWLINE_COLOR, NO_EOF_NEWLINE_MARKER, NORMAL
"{NO_EOF_NEWLINE_COLOR}{NO_EOF_NEWLINE_MARKER}{NORMAL}"
));
}

Expand All @@ -172,8 +169,7 @@ fn to_lines(old: &str, new: &str) -> (Vec<String>, Vec<String>) {
}
if (!new.is_empty()) && !new.ends_with('\n') {
new_lines.push(format!(
"{}{}{}",
NO_EOF_NEWLINE_COLOR, NO_EOF_NEWLINE_MARKER, NORMAL
"{NO_EOF_NEWLINE_COLOR}{NO_EOF_NEWLINE_MARKER}{NORMAL}"
));
}

Expand Down Expand Up @@ -235,12 +231,10 @@ mod tests {
result,
[
format!(
"{}-{}<{}unchanged text between quotes{}>{}",
OLD, INVERSE_VIDEO, NOT_INVERSE_VIDEO, INVERSE_VIDEO, NORMAL
"{OLD}-{INVERSE_VIDEO}<{NOT_INVERSE_VIDEO}unchanged text between quotes{INVERSE_VIDEO}>{NORMAL}"
),
format!(
"{}+{}[{}unchanged text between quotes{}]{}",
NEW, INVERSE_VIDEO, NOT_INVERSE_VIDEO, INVERSE_VIDEO, NORMAL
"{NEW}+{INVERSE_VIDEO}[{NOT_INVERSE_VIDEO}unchanged text between quotes{INVERSE_VIDEO}]{NORMAL}"
),
]
)
Expand All @@ -249,9 +243,9 @@ mod tests {
#[test]
fn test_almost_empty_changes() {
let result = format("x\n", "");
assert_eq!(result, [format!("{}-x{}", OLD, NORMAL),]);
assert_eq!(result, [format!("{OLD}-x{NORMAL}"),]);

let result = format("", "x\n");
assert_eq!(result, [format!("{}+x{}", NEW, NORMAL),]);
assert_eq!(result, [format!("{NEW}+x{NORMAL}"),]);
}
}
6 changes: 3 additions & 3 deletions src/token_collector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ mod tests {
});

let rendered = test_me.render();
assert_eq!(rendered, format!("{}+hej{}\n", NEW, NORMAL));
assert_eq!(rendered, format!("{NEW}+hej{NORMAL}\n"));
}

#[test]
Expand Down Expand Up @@ -370,7 +370,7 @@ mod tests {
test_me.push(StyledToken::new(" ".to_string(), Style::Old));
let actual = test_me.render();

assert_eq!(actual, format!("{}- {}", OLD, NORMAL));
assert_eq!(actual, format!("{OLD}- {NORMAL}"));
}

#[test]
Expand Down Expand Up @@ -433,7 +433,7 @@ mod tests {
test_me.push(StyledToken::new("\t".to_string(), Style::Old));
let actual = test_me.render();

assert_eq!(actual, format!("{}-x\t{}", OLD, NORMAL));
assert_eq!(actual, format!("{OLD}-x\t{NORMAL}"));
}

#[test]
Expand Down

0 comments on commit f6c6807

Please sign in to comment.