Skip to content

Commit

Permalink
refactor(list): sort list output
Browse files Browse the repository at this point in the history
  • Loading branch information
QaidVoid committed Oct 30, 2024
1 parent 89ed804 commit 2c8d894
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 21 deletions.
12 changes: 3 additions & 9 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ mod misc;
mod registry;

pub async fn init() -> Result<()> {
config::init();
let args = Args::parse();
let registry = PackageRegistry::new().await?;
config::init();
setup_required_paths().await?;
let registry = PackageRegistry::new().await?;

let path_env = env::var("PATH")?;
if !path_env.split(':').any(|p| Path::new(p) == *BIN_PATH) {
Expand Down Expand Up @@ -53,13 +53,7 @@ pub async fn init() -> Result<()> {
let portable_config = portable_config.map(|p| p.unwrap_or_default());

registry
.install_packages(
&packages,
force,
portable,
portable_home,
portable_config,
)
.install_packages(&packages, force, portable, portable_home, portable_config)
.await?;
}
Commands::Sync => {
Expand Down
10 changes: 3 additions & 7 deletions src/registry/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ impl PackageRegistry {

let formatted_name = format!(
"{} ({}#{})",
package.name.clone().color(Color::BrightGreen),
package.bin_name.clone().color(Color::BrightGreen),
package.clone().full_name('/').color(Color::BrightCyan),
pkg.collection.clone().color(Color::BrightRed)
);
Expand Down Expand Up @@ -300,21 +300,17 @@ impl PackageRegistry {
}
for resolved_package in packages {
let package = resolved_package.package.clone();
let variant_prefix = package
.variant
.map(|variant| format!("{}-", variant))
.unwrap_or_default();
let installed_guard = self.installed_packages.lock().await;
let install_prefix = if installed_guard.is_installed(&resolved_package) {
"+"
} else {
"-"
};
println!(
"[{0}] [{1}] {2}{3}:{3}-{4} ({5})",
"[{0}] [{1}] {2}:{3}-{4} ({5})",
install_prefix.color(Color::Red),
resolved_package.collection.color(Color::BrightGreen),
variant_prefix.color(Color::Blue),
package.full_name('/').color(Color::Blue),
package.name.color(Color::Blue),
package.version.color(Color::Green),
package.size.color(Color::Magenta)
Expand Down
5 changes: 3 additions & 2 deletions src/registry/package/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ pub struct Package {
pub category: String,
pub extra_bins: String,
pub icon: String,
pub bin_id: Option<String>,
pub variant: Option<String>,
}

Expand All @@ -56,7 +57,7 @@ impl ResolvedPackage {
portable: Option<String>,
portable_home: Option<String>,
portable_config: Option<String>,
multi_progress: Arc<MultiProgress>
multi_progress: Arc<MultiProgress>,
) -> Result<()> {
let install_path = self.package.get_install_path(&self.package.bsum);
let mut installer = Installer::new(self, install_path);
Expand All @@ -69,7 +70,7 @@ impl ResolvedPackage {
portable,
portable_home,
portable_config,
multi_progress
multi_progress,
)
.await?;
Ok(())
Expand Down
2 changes: 1 addition & 1 deletion src/registry/package/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ impl Updater {
None,
None,
None,
multi_progress.clone()
multi_progress.clone(),
)
.await?;
update_count += 1;
Expand Down
15 changes: 13 additions & 2 deletions src/registry/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,8 @@ impl PackageStorage {
}

pub fn list_packages(&self, collection: Option<&str>) -> Vec<ResolvedPackage> {
self.repository
let mut packages: Vec<ResolvedPackage> = self
.repository
.iter()
.flat_map(|(repo_name, repo_packages)| {
repo_packages
Expand All @@ -241,7 +242,17 @@ impl PackageStorage {
})
})
})
.collect()
.collect();

packages.sort_by(|a, b| {
let collection_cmp = a.collection.cmp(&b.collection);
if collection_cmp == std::cmp::Ordering::Equal {
a.package.full_name('-').cmp(&b.package.full_name('-'))
} else {
collection_cmp
}
});
packages
}

pub fn get_packages(&self, query: &PackageQuery) -> Option<Vec<ResolvedPackage>> {
Expand Down

0 comments on commit 2c8d894

Please sign in to comment.