Skip to content

Commit

Permalink
Rework print module
Browse files Browse the repository at this point in the history
  • Loading branch information
HanabishiRecca committed Jun 1, 2023
1 parent 6512e1d commit b9cb2ed
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 39 deletions.
2 changes: 1 addition & 1 deletion src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ fn run(
}: Config,
) -> R<()> {
set_color_mode(color_mode);
header(format_args!("Checking AUR updates..."));
print_header("Checking AUR updates...");
check_updates(find_foreign_packages(ignores, ignore_groups)?)
}

Expand Down
12 changes: 4 additions & 8 deletions src/app/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,8 @@ use Status::*;

fn print_status(status: Status) {
match status {
HasUpdate(name, ver, new_ver) => update(
format_args!("{name}"),
format_args!("{ver}"),
format_args!("{new_ver}"),
),
NotInAUR(name) => package(format_args!("{name}"), format_args!("is not in AUR")),
HasUpdate(name, ver, new_ver) => print_update(name, ver, new_ver),
NotInAUR(name) => print_package(name, "is not in AUR"),
_ => {}
}
}
Expand All @@ -49,15 +45,15 @@ fn count_updates(state: &[Status]) -> usize {

pub fn check_updates(pkgs: Vec<(String, String)>) -> R<()> {
if pkgs.is_empty() {
message(format_args!("no packages to check"));
print_message("no packages to check");
return Ok(());
}

let updates = request_updates(pkgs.iter())?;
let state = gen_state(pkgs, updates);

if count_updates(&state) == 0 {
message(format_args!("no updates"));
print_message("no updates");
}

state.into_iter().for_each(print_status);
Expand Down
2 changes: 1 addition & 1 deletion src/app/local.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ pub fn find_foreign_packages(

alpm.set_event_cb((), |e, _| {
if let Event::DatabaseMissing(event) = e.event() {
warning(format_args!(
print_warning(format_args!(
"database file for '{}' does not exist (use 'pacman -Sy' to download)",
event.dbname()
))
Expand Down
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ fn main() -> ExitCode {
set_color_mode(ColorMode::Auto);
match run_app() {
Err(e) => {
error(format_args!("{e}"));
print_error(e);
ExitCode::FAILURE
}
_ => ExitCode::SUCCESS,
Expand Down
53 changes: 25 additions & 28 deletions src/print.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::{
fmt::Arguments,
fmt::Display,
sync::atomic::{AtomicBool, Ordering::Relaxed},
};

Expand Down Expand Up @@ -30,52 +30,49 @@ pub fn set_color_mode(mode: ColorMode) {
}

macro_rules! print_to {
($p: ident, $pl: ident, $c: expr, $n: expr) => {{
($p: ident, $n: expr, $c: expr) => {
match COLOR.load(Relaxed) {
true => {
$p!($c);
$pl!("\x1b[0m");
}
false => $pl!($n),
};
}};
true => $p!($c),
false => $p!($n),
}
};
}

macro_rules! P {
($c: expr, $n: expr $(,)?) => {
print_to!(print, println, $c, $n)
($n: expr, $c: expr $(,)?) => {
print_to!(println, $n, $c)
};
}

macro_rules! E {
($c: expr, $n: expr $(,)?) => {
print_to!(eprint, eprintln, $c, $n)
macro_rules! PE {
($n: expr, $c: expr $(,)?) => {
print_to!(eprintln, $n, $c)
};
}

pub fn header(s: Arguments) {
P!("\x1b[1;34m::\x1b[0;1m {s}", ":: {s}")
pub fn print_header(s: impl Display) {
P!(":: {s}", "\x1b[1;34m::\x1b[0;1m {s}\x1b[0m");
}

pub fn message(s: Arguments) {
P!("\x1b[0m {s}", " {s}")
pub fn print_message(s: impl Display) {
P!(" {s}", "\x1b[0m {s}\x1b[0m");
}

pub fn update(name: Arguments, ver: Arguments, new_ver: Arguments) {
pub fn print_update(name: impl Display, ver: impl Display, new_ver: impl Display) {
P!(
"\x1b[0;1m{name} \x1b[1;31m{ver}\x1b[0m => \x1b[1;32m{new_ver}",
"{name} {ver} => {new_ver}"
)
"{name} {ver} => {new_ver}",
"\x1b[0;1m{name} \x1b[1;31m{ver}\x1b[0m => \x1b[1;32m{new_ver}\x1b[0m",
);
}

pub fn package(name: Arguments, s: Arguments) {
P!("\x1b[0;1m{name}\x1b[0m {s}", "{name} {s}")
pub fn print_package(name: impl Display, s: impl Display) {
P!("{name} {s}", "\x1b[0;1m{name}\x1b[0m {s}\x1b[0m");
}

pub fn error(e: Arguments) {
E!("\x1b[1;31merror:\x1b[0m {e}", "error: {e}")
pub fn print_error(e: impl Display) {
PE!("error: {e}", "\x1b[1;31merror:\x1b[0m {e}\x1b[0m");
}

pub fn warning(w: Arguments) {
E!("\x1b[1;33mwarning:\x1b[0m {w}", "warning: {w}")
pub fn print_warning(w: impl Display) {
PE!("warning: {w}", "\x1b[1;33mwarning:\x1b[0m {w}\x1b[0m");
}

0 comments on commit b9cb2ed

Please sign in to comment.