Skip to content

Commit 7ad0f33

Browse files
author
unknown
committed
Implement sort by newest also for 'versions --short'
1 parent 5ddd406 commit 7ad0f33

File tree

2 files changed

+35
-28
lines changed

2 files changed

+35
-28
lines changed

src/bin/conserve.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ enum Command {
146146
#[structopt(long, short = "q")]
147147
short: bool,
148148
/// Sort bands to show most recent first.
149-
#[structopt(long, short = "n", conflicts_with = "short" )]
149+
#[structopt(long, short = "n")]
150150
newest: bool,
151151
/// Show size of stored trees.
152152
#[structopt(long, short = "z", conflicts_with = "short")]
@@ -363,7 +363,7 @@ impl Command {
363363
ui::enable_progress(false);
364364
let archive = Archive::open_path(archive)?;
365365
if *short {
366-
output::show_brief_version_list(&archive, &mut stdout)?;
366+
output::show_brief_version_list(&archive, *newest, &mut stdout)?;
367367
} else {
368368
output::show_verbose_version_list(&archive, *newest,*sizes, &mut stdout)?;
369369
}

src/output.rs

Lines changed: 33 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,13 @@ use chrono::Local;
2222

2323
use crate::*;
2424

25-
pub fn show_brief_version_list(archive: &Archive, w: &mut dyn Write) -> Result<()> {
26-
for band_id in archive.list_band_ids()? {
27-
writeln!(w, "{}", band_id)?
25+
pub fn show_brief_version_list(
26+
archive: &Archive,
27+
sort_recent_first: bool,
28+
w: &mut dyn Write
29+
) -> Result<()> {
30+
for info in get_band_infos(archive, sort_recent_first)? {
31+
writeln!(w, "{}", info.id)?
2832
}
2933
Ok(())
3034
}
@@ -35,29 +39,7 @@ pub fn show_verbose_version_list(
3539
show_sizes: bool,
3640
w: &mut dyn Write,
3741
) -> Result<()> {
38-
let mut band_infos = vec![];
39-
for band_id in archive.list_band_ids()? {
40-
let band = match Band::open(&archive, &band_id) {
41-
Ok(band) => band,
42-
Err(e) => {
43-
ui::problem(&format!("Failed to open band {:?}: {:?}", band_id, e));
44-
continue;
45-
}
46-
};
47-
let info = match band.get_info() {
48-
Ok(info) => info,
49-
Err(e) => {
50-
ui::problem(&format!("Failed to read band tail {:?}: {:?}", band_id, e));
51-
continue;
52-
}
53-
};
54-
band_infos.push(info);
55-
}
56-
if sort_recent_first {
57-
band_infos.sort_unstable_by_key(|info| std::cmp::Reverse(info.start_time));
58-
}
59-
60-
for info in band_infos {
42+
for info in get_band_infos(archive, sort_recent_first)? {
6143
let band_id = &info.id;
6244
let is_complete_str = if info.is_closed {
6345
"complete"
@@ -111,3 +93,28 @@ pub fn show_entry_names<E: Entry, I: Iterator<Item = E>>(it: I, w: &mut dyn Writ
11193
}
11294
Ok(())
11395
}
96+
97+
fn get_band_infos(archive: &Archive, sort_recent_first: bool) -> Result<Vec<band::Info>> {
98+
let mut band_infos = vec![];
99+
for band_id in archive.list_band_ids()? {
100+
let band = match Band::open(&archive, &band_id) {
101+
Ok(band) => band,
102+
Err(e) => {
103+
ui::problem(&format!("Failed to open band {:?}: {:?}", band_id, e));
104+
continue;
105+
}
106+
};
107+
let info = match band.get_info() {
108+
Ok(info) => info,
109+
Err(e) => {
110+
ui::problem(&format!("Failed to read band tail {:?}: {:?}", band_id, e));
111+
continue;
112+
}
113+
};
114+
band_infos.push(info);
115+
}
116+
if sort_recent_first {
117+
band_infos.sort_unstable_by_key(|info| std::cmp::Reverse(info.start_time));
118+
}
119+
Ok(band_infos)
120+
}

0 commit comments

Comments
 (0)