Skip to content

Commit

Permalink
generations: fix generation list sorting to ensure numerical order
Browse files Browse the repository at this point in the history
  • Loading branch information
NotAShelf authored and viperML committed Jan 22, 2025
1 parent f4da5cf commit e691846
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/generations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ pub fn describe(generation_dir: &Path, current_profile: &Path) -> Option<Generat
})
}

pub fn print_info(generations: Vec<GenerationInfo>) {
pub fn print_info(mut generations: Vec<GenerationInfo>) {
// Get path information for the *current generation* from /run/current-system
// and split it by whitespace to get the size (second part). This should be
// safe enough, in theory.
Expand All @@ -152,11 +152,15 @@ pub fn print_info(generations: Vec<GenerationInfo>) {
"Unknown".to_string()
};

// Sort generations by numeric value of the generation number
generations.sort_by_key(|gen| gen.number.parse::<u64>().unwrap_or(0));

// Retrieve the current generation
let current_generation = generations
.iter()
.max_by_key(|gen| gen.number.parse::<u64>().unwrap_or(0));

debug!(?current_generation);

if let Some(current) = current_generation {
println!("NixOS {}", current.nixos_version);
} else {
Expand All @@ -166,6 +170,7 @@ pub fn print_info(generations: Vec<GenerationInfo>) {
println!("Closure Size: {}", closure);
println!();

// Determine column widths for pretty printing
let max_nixos_version_len = generations
.iter()
.map(|g| g.nixos_version.len())
Expand All @@ -189,6 +194,7 @@ pub fn print_info(generations: Vec<GenerationInfo>) {
width_kernel = max_kernel_len
);

// Print generations in descending order
for generation in generations.iter().rev() {
let date_str = &generation.date;
let date = DateTime::parse_from_rfc3339(date_str)
Expand Down

0 comments on commit e691846

Please sign in to comment.