1
Fork 0
mirror of https://github.com/RGBCube/dix synced 2025-07-28 12:17:45 +00:00

feat: write diff status header

This commit is contained in:
RGBCube 2025-05-09 01:30:27 +03:00 committed by bloxx12
parent faa6634142
commit 47bd2d9657
2 changed files with 27 additions and 8 deletions

View file

@ -1,7 +1,4 @@
use std::{ use std::fmt;
fmt,
io,
};
use rustc_hash::{ use rustc_hash::{
FxBuildHasher, FxBuildHasher,
@ -14,6 +11,8 @@ use crate::{
Version, Version,
}; };
const HEADER_STYLE: yansi::Style = yansi::Style::new().bold().underline();
#[derive(Default)] #[derive(Default)]
struct Diff<T> { struct Diff<T> {
old: T, old: T,
@ -42,10 +41,10 @@ impl fmt::Display for DiffStatus {
} }
pub fn diff<'a>( pub fn diff<'a>(
writer: &mut dyn io::Write, writer: &mut dyn fmt::Write,
paths_old: impl Iterator<Item = &'a StorePath>, paths_old: impl Iterator<Item = &'a StorePath>,
paths_new: impl Iterator<Item = &'a StorePath>, paths_new: impl Iterator<Item = &'a StorePath>,
) -> io::Result<()> { ) -> fmt::Result {
let mut paths = let mut paths =
FxHashMap::<&str, Diff<Vec<Option<&Version>>>>::with_hasher(FxBuildHasher); FxHashMap::<&str, Diff<Vec<Option<&Version>>>>::with_hasher(FxBuildHasher);
@ -92,7 +91,16 @@ pub fn diff<'a>(
a_status.cmp(&b_status).then_with(|| a_name.cmp(b_name)) a_status.cmp(&b_status).then_with(|| a_name.cmp(b_name))
}); });
let mut last_status = None::<DiffStatus>;
for (name, _versions, status) in diffs { for (name, _versions, status) in diffs {
if last_status != Some(status) {
last_status = Some(status);
HEADER_STYLE.fmt_prefix(writer)?;
writeln!(writer, "{status:?} packages:")?;
HEADER_STYLE.fmt_suffix(writer)?;
}
write!(writer, "{status} {name}")?; write!(writer, "{status} {name}")?;
writeln!(writer)?; writeln!(writer)?;
} }

View file

@ -1,5 +1,8 @@
use std::{ use std::{
fmt::Write as _, fmt::{
self,
Write as _,
},
io::{ io::{
self, self,
Write as _, Write as _,
@ -21,6 +24,14 @@ use dix::{
}; };
use yansi::Paint as _; use yansi::Paint as _;
struct WriteFmt<W: io::Write>(W);
impl<W: io::Write> fmt::Write for WriteFmt<W> {
fn write_str(&mut self, string: &str) -> fmt::Result {
self.0.write_all(string.as_bytes()).map_err(|_| fmt::Error)
}
}
#[derive(clap::Parser, Debug)] #[derive(clap::Parser, Debug)]
#[command(version, about)] #[command(version, about)]
struct Cli { struct Cli {
@ -92,7 +103,7 @@ fn real_main() -> Result<()> {
drop(connection); drop(connection);
let mut out = io::stdout(); let mut out = WriteFmt(io::stdout());
#[expect(clippy::pattern_type_mismatch)] #[expect(clippy::pattern_type_mismatch)]
diff( diff(