From fe9384abdfded80736fddb7c282975a0471d3dd2 Mon Sep 17 00:00:00 2001 From: Ales Katona Date: Wed, 20 Jul 2022 12:10:52 -0700 Subject: [PATCH] add profile list --- src/main.rs | 3 +++ src/vcs.rs | 25 ++++++++++++++++++++++++- src/vcs/cli.rs | 2 ++ 3 files changed, 29 insertions(+), 1 deletion(-) diff --git a/src/main.rs b/src/main.rs index ac3e9c6..f6aaa02 100644 --- a/src/main.rs +++ b/src/main.rs @@ -35,6 +35,9 @@ fn main() { vcs.set_profile(&profile) .expect("Error setting vcs profile"); } + Commands::Profile(ProfileCommand::List) => { + vcs.list_profiles().expect("Error setting vcs profile"); + } Commands::Status { verbose } => { if verbose { vcs.list(); diff --git a/src/vcs.rs b/src/vcs.rs index dabfd50..fede651 100644 --- a/src/vcs.rs +++ b/src/vcs.rs @@ -146,7 +146,26 @@ impl Vcs { min_msrv } - pub fn save_profile(&self, profile_name: &str) -> Result<(), Error> { + pub fn list_profiles(&self) -> Result<(), Error> { + let current_profile = self.current_profile()?; + let mut prefix = ""; + print!("["); + + for profile in &self.profiles { + if current_profile.as_ref() == Some(profile) { + print!("{}{}", prefix, profile.as_str().bold().with(PROFILE_COLOR)); + } else { + print!("{}{}", prefix, profile.as_str().with(PROFILE_COLOR)); + } + prefix = ", "; + } + + println!("]"); + + Ok(()) + } + + pub fn save_profile(&mut self, profile_name: &str) -> Result<(), Error> { let vcs_path = self.work_dir.join("Cargo_vcs.toml"); let vcs = match fs::read_to_string(&vcs_path) { Ok(val) => toml::from_str(&val)?, @@ -175,6 +194,10 @@ impl Vcs { let new_contents = toml::to_string(&vcs_main)?; fs::write(vcs_path, new_contents)?; + if !&self.profiles.iter().any(|p| p == profile_name) { + self.profiles.push(profile_name.into()); + } + println!( "Workspace state saved as {}", profile_name.with(PROFILE_COLOR), diff --git a/src/vcs/cli.rs b/src/vcs/cli.rs index 4847a17..60ace4e 100644 --- a/src/vcs/cli.rs +++ b/src/vcs/cli.rs @@ -36,6 +36,8 @@ pub enum ProfileCommand { /// Switch workspace to given profile #[clap(arg_required_else_help = true)] Set { profile: String }, + /// Lists all saved profiles + List, } #[derive(Debug, Parser)]