1
Fork 0
mirror of https://github.com/RGBCube/dix synced 2025-07-28 04:07:46 +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::{
fmt,
io,
};
use std::fmt;
use rustc_hash::{
FxBuildHasher,
@ -14,6 +11,8 @@ use crate::{
Version,
};
const HEADER_STYLE: yansi::Style = yansi::Style::new().bold().underline();
#[derive(Default)]
struct Diff<T> {
old: T,
@ -42,10 +41,10 @@ impl fmt::Display for DiffStatus {
}
pub fn diff<'a>(
writer: &mut dyn io::Write,
writer: &mut dyn fmt::Write,
paths_old: impl Iterator<Item = &'a StorePath>,
paths_new: impl Iterator<Item = &'a StorePath>,
) -> io::Result<()> {
) -> fmt::Result {
let mut paths =
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))
});
let mut last_status = None::<DiffStatus>;
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}")?;
writeln!(writer)?;
}

View file

@ -1,5 +1,8 @@
use std::{
fmt::Write as _,
fmt::{
self,
Write as _,
},
io::{
self,
Write as _,
@ -21,6 +24,14 @@ use dix::{
};
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)]
#[command(version, about)]
struct Cli {
@ -92,7 +103,7 @@ fn real_main() -> Result<()> {
drop(connection);
let mut out = io::stdout();
let mut out = WriteFmt(io::stdout());
#[expect(clippy::pattern_type_mismatch)]
diff(