From 141ebf0cc0c6cfea640debc9b9073303509a8bc7 Mon Sep 17 00:00:00 2001 From: DanConwayDev Date: Tue, 20 Feb 2024 16:22:10 +0000 Subject: feat(list): set checkout branch as default choice instead of no default. note: I spent hours trying to get CliTester to support default choices and gave up. I have a stashed the attempt and am moving on... --- src/cli_interactor.rs | 19 +++++++++--- src/sub_commands/list.rs | 79 +++++++++++++++++++++++++++--------------------- test_utils/src/lib.rs | 11 ++++++- tests/list.rs | 40 ++++++++++++------------ 4 files changed, 89 insertions(+), 60 deletions(-) diff --git a/src/cli_interactor.rs b/src/cli_interactor.rs index a702a54..dc15c87 100644 --- a/src/cli_interactor.rs +++ b/src/cli_interactor.rs @@ -41,12 +41,17 @@ impl InteractorPrompt for Interactor { Ok(confirm) } fn choice(&self, parms: PromptChoiceParms) -> Result { - dialoguer::Select::with_theme(&self.theme) + let mut choice = dialoguer::Select::with_theme(&self.theme); + choice .with_prompt(parms.prompt) .report(parms.report) - .items(&parms.choices) - .interact() - .context("failed to get choice") + .items(&parms.choices); + if let Some(default) = parms.default { + if std::env::var("NGITTEST").is_err() { + choice.default(default); + } + } + choice.interact().context("failed to get choice") } } @@ -110,6 +115,7 @@ impl PromptConfirmParms { pub struct PromptChoiceParms { pub prompt: String, pub choices: Vec, + pub default: Option, pub report: bool, } @@ -128,4 +134,9 @@ impl PromptChoiceParms { self.choices = choices; self } + + pub fn with_default(mut self, index: usize) -> Self { + self.default = Some(index); + self + } } diff --git a/src/sub_commands/list.rs b/src/sub_commands/list.rs index 008872b..b556d5a 100644 --- a/src/sub_commands/list.rs +++ b/src/sub_commands/list.rs @@ -121,14 +121,16 @@ pub async fn launch(_cli_args: &Cli, _args: &SubCommandArgs) -> Result<()> { if no_support_for_patches_as_branch { println!("{patch_text_ref}"); - return match Interactor::default().choice(PromptChoiceParms::default().with_choices( - vec![ - "learn why 'patch only' proposals can't be checked out".to_string(), - format!("apply to current branch with `git am`"), - format!("download to ./patches"), - "back".to_string(), - ], - ))? { + return match Interactor::default().choice( + PromptChoiceParms::default() + .with_default(0) + .with_choices(vec![ + "learn why 'patch only' proposals can't be checked out".to_string(), + format!("apply to current branch with `git am`"), + format!("download to ./patches"), + "back".to_string(), + ]), + )? { 0 => { println!("Some proposals are posted as 'patch only'\n"); println!( @@ -144,7 +146,9 @@ pub async fn launch(_cli_args: &Cli, _args: &SubCommandArgs) -> Result<()> { "by default ngit posts proposals that support both the branch and patch model so either workflow can be used" ); Interactor::default().choice( - PromptChoiceParms::default().with_choices(vec!["back".to_string()]), + PromptChoiceParms::default() + .with_default(0) + .with_choices(vec!["back".to_string()]), )?; continue; } @@ -180,7 +184,7 @@ pub async fn launch(_cli_args: &Cli, _args: &SubCommandArgs) -> Result<()> { if !git_repo.does_commit_exist(&proposal_base_commit.to_string())? { println!("your '{main_branch_name}' branch may not be up-to-date."); println!("the proposal parent commit doesnt exist in your local repository."); - return match Interactor::default().choice(PromptChoiceParms::default().with_choices( + return match Interactor::default().choice(PromptChoiceParms::default().with_default(0).with_choices( vec![ format!( "manually run `git pull` on '{main_branch_name}' and select proposal again" @@ -213,7 +217,7 @@ pub async fn launch(_cli_args: &Cli, _args: &SubCommandArgs) -> Result<()> { // branch doesnt exist if !branch_exists { return match Interactor::default() - .choice(PromptChoiceParms::default().with_choices(vec![ + .choice(PromptChoiceParms::default().with_default(0).with_choices(vec![ format!( "create and checkout proposal branch ({} ahead {} behind '{main_branch_name}')", most_recent_proposal_patch_chain.len(), @@ -255,6 +259,7 @@ pub async fn launch(_cli_args: &Cli, _args: &SubCommandArgs) -> Result<()> { println!("branch checked out and up-to-date"); return match Interactor::default().choice( PromptChoiceParms::default() + .with_default(0) .with_choices(vec!["exit".to_string(), "back".to_string()]), )? { 0 => Ok(()), @@ -265,18 +270,20 @@ pub async fn launch(_cli_args: &Cli, _args: &SubCommandArgs) -> Result<()> { }; } - return match Interactor::default().choice(PromptChoiceParms::default().with_choices( - vec![ - format!( - "checkout proposal branch ({} ahead {} behind '{main_branch_name}')", - most_recent_proposal_patch_chain.len(), - proposal_behind_main.len(), - ), - format!("apply to current branch with `git am`"), - format!("download to ./patches"), - "back".to_string(), - ], - ))? { + return match Interactor::default().choice( + PromptChoiceParms::default() + .with_default(0) + .with_choices(vec![ + format!( + "checkout proposal branch ({} ahead {} behind '{main_branch_name}')", + most_recent_proposal_patch_chain.len(), + proposal_behind_main.len(), + ), + format!("apply to current branch with `git am`"), + format!("download to ./patches"), + "back".to_string(), + ]), + )? { 0 => { check_clean(&git_repo)?; git_repo.checkout(&cover_letter.branch_name)?; @@ -304,14 +311,16 @@ pub async fn launch(_cli_args: &Cli, _args: &SubCommandArgs) -> Result<()> { .unwrap_or_default() .eq(&local_branch_tip.to_string()) }) { - return match Interactor::default().choice(PromptChoiceParms::default().with_choices( - vec![ - format!("checkout proposal branch and apply {} appendments", &index,), - format!("apply to current branch with `git am`"), - format!("download to ./patches"), - "back".to_string(), - ], - ))? { + return match Interactor::default().choice( + PromptChoiceParms::default() + .with_default(0) + .with_choices(vec![ + format!("checkout proposal branch and apply {} appendments", &index,), + format!("apply to current branch with `git am`"), + format!("download to ./patches"), + "back".to_string(), + ]), + )? { 0 => { check_clean(&git_repo)?; git_repo.checkout(&cover_letter.branch_name)?; @@ -346,7 +355,7 @@ pub async fn launch(_cli_args: &Cli, _args: &SubCommandArgs) -> Result<()> { .eq(&local_branch_tip.to_string()) }) { return match Interactor::default().choice( - PromptChoiceParms::default() + PromptChoiceParms::default().with_default(0) .with_choices( vec![ format!( @@ -410,7 +419,7 @@ pub async fn launch(_cli_args: &Cli, _args: &SubCommandArgs) -> Result<()> { local_ahead_of_proposal.len() ); return match Interactor::default().choice( - PromptChoiceParms::default() + PromptChoiceParms::default().with_default(0) .with_choices( vec![ format!( @@ -450,7 +459,7 @@ pub async fn launch(_cli_args: &Cli, _args: &SubCommandArgs) -> Result<()> { local_beind_main.len(), ); return match Interactor::default().choice( - PromptChoiceParms::default() + PromptChoiceParms::default().with_default(0) .with_choices( vec![ format!( @@ -514,7 +523,7 @@ pub async fn launch(_cli_args: &Cli, _args: &SubCommandArgs) -> Result<()> { ); return match Interactor::default().choice( - PromptChoiceParms::default() + PromptChoiceParms::default().with_default(0) .with_choices( vec![ format!( diff --git a/test_utils/src/lib.rs b/test_utils/src/lib.rs index 089b052..3808a02 100644 --- a/test_utils/src/lib.rs +++ b/test_utils/src/lib.rs @@ -479,7 +479,16 @@ impl CliTesterChoicePrompt<'_> { Ok(self) } - pub fn succeeds_with(&mut self, chosen_index: u64, report: bool) -> Result<&mut Self> { + pub fn succeeds_with( + &mut self, + chosen_index: u64, + report: bool, + default_index: Option, + ) -> Result<&mut Self> { + if default_index.is_some() { + println!("TODO: add support for default choice") + } + fn show_options( tester: &mut CliTester, choices: &Vec, diff --git a/tests/list.rs b/tests/list.rs index 0c138d5..7762b1c 100644 --- a/tests/list.rs +++ b/tests/list.rs @@ -266,7 +266,7 @@ mod when_main_branch_is_uptodate { format!("\"{PROPOSAL_TITLE_3}\""), ], )?; - c.succeeds_with(0, true)?; + c.succeeds_with(0, true, None)?; p.expect("finding commits...\r\n")?; let mut c = p.expect_choice( @@ -279,7 +279,7 @@ mod when_main_branch_is_uptodate { format!("back"), ], )?; - c.succeeds_with(0, false)?; + c.succeeds_with(0, false, Some(0))?; p.expect(format!( "checked out proposal as '{FEATURE_BRANCH_NAME_1}' branch\r\n" ))?; @@ -339,7 +339,7 @@ mod when_main_branch_is_uptodate { format!("\"{PROPOSAL_TITLE_3}\""), ], )?; - c.succeeds_with(0, true)?; + c.succeeds_with(0, true, None)?; p.expect("finding commits...\r\n")?; let mut c = p.expect_choice( "", @@ -351,7 +351,7 @@ mod when_main_branch_is_uptodate { format!("back"), ], )?; - c.succeeds_with(0, false)?; + c.succeeds_with(0, false, None)?; p.expect(format!( "checked out proposal as '{FEATURE_BRANCH_NAME_1}' branch\r\n" ))?; @@ -455,7 +455,7 @@ mod when_main_branch_is_uptodate { format!("\"{PROPOSAL_TITLE_3}\""), ], )?; - c.succeeds_with(2, true)?; + c.succeeds_with(2, true, None)?; p.expect("finding commits...\r\n")?; let mut c = p.expect_choice( @@ -468,7 +468,7 @@ mod when_main_branch_is_uptodate { format!("back"), ], )?; - c.succeeds_with(0, false)?; + c.succeeds_with(0, false, Some(0))?; p.expect(format!( "checked out proposal as '{FEATURE_BRANCH_NAME_3}' branch\r\n" ))?; @@ -529,7 +529,7 @@ mod when_main_branch_is_uptodate { format!("\"{PROPOSAL_TITLE_3}\""), ], )?; - c.succeeds_with(2, true)?; + c.succeeds_with(2, true, None)?; p.expect("finding commits...\r\n")?; let mut c = p.expect_choice( "", @@ -541,7 +541,7 @@ mod when_main_branch_is_uptodate { format!("back"), ], )?; - c.succeeds_with(0, false)?; + c.succeeds_with(0, false, Some(0))?; p.expect(format!( "checked out proposal as '{FEATURE_BRANCH_NAME_3}' branch\r\n" ))?; @@ -651,7 +651,7 @@ mod when_main_branch_is_uptodate { format!("add d3.md"), // commit msg title ], )?; - c.succeeds_with(3, true)?; + c.succeeds_with(3, true, None)?; p.expect("finding commits...\r\n")?; let mut c = p.expect_choice( "", @@ -663,7 +663,7 @@ mod when_main_branch_is_uptodate { format!("back"), ], )?; - c.succeeds_with(0, false)?; + c.succeeds_with(0, false, Some(0))?; p.expect(format!( "checked out proposal as '{FEATURE_BRANCH_NAME_4}' branch\r\n" ))?; @@ -730,7 +730,7 @@ mod when_main_branch_is_uptodate { format!("add d3.md"), // commit msg title ], )?; - c.succeeds_with(3, true)?; + c.succeeds_with(3, true, None)?; p.expect("finding commits...\r\n")?; let mut c = p.expect_choice( "", @@ -742,7 +742,7 @@ mod when_main_branch_is_uptodate { format!("back"), ], )?; - c.succeeds_with(0, false)?; + c.succeeds_with(0, false, Some(0))?; p.expect(format!( "checked out proposal as '{FEATURE_BRANCH_NAME_4}' branch\r\n" ))?; @@ -861,7 +861,7 @@ mod when_main_branch_is_uptodate { format!("\"{PROPOSAL_TITLE_3}\""), ], )?; - c.succeeds_with(0, true)?; + c.succeeds_with(0, true, None)?; p.expect("finding commits...\r\n")?; let mut c = p.expect_choice( "", @@ -872,7 +872,7 @@ mod when_main_branch_is_uptodate { format!("back"), ], )?; - c.succeeds_with(0, false)?; + c.succeeds_with(0, false, Some(0))?; p.expect(format!( "checked out proposal as '{FEATURE_BRANCH_NAME_1}' branch\r\n" ))?; @@ -940,7 +940,7 @@ mod when_main_branch_is_uptodate { format!("\"{PROPOSAL_TITLE_3}\""), ], )?; - c.succeeds_with(0, true)?; + c.succeeds_with(0, true, None)?; p.expect("finding commits...\r\n")?; let mut c = p.expect_choice( "", @@ -951,7 +951,7 @@ mod when_main_branch_is_uptodate { format!("back"), ], )?; - c.succeeds_with(0, false)?; + c.succeeds_with(0, false, Some(0))?; p.expect(format!( "checked out proposal as '{FEATURE_BRANCH_NAME_1}' branch\r\n" ))?; @@ -1042,7 +1042,7 @@ mod when_main_branch_is_uptodate { format!("\"{PROPOSAL_TITLE_3}\""), ], )?; - c.succeeds_with(0, true)?; + c.succeeds_with(0, true, None)?; p.expect("finding commits...\r\n")?; let mut c = p.expect_choice( "", @@ -1053,7 +1053,7 @@ mod when_main_branch_is_uptodate { format!("back"), ], )?; - c.succeeds_with(0, false)?; + c.succeeds_with(0, false, Some(0))?; p.expect("checked out proposal branch and applied 1 appendments (2 ahead 0 behind 'main')\r\n")?; p.expect_end()?; @@ -1120,7 +1120,7 @@ mod when_main_branch_is_uptodate { format!("\"{PROPOSAL_TITLE_3}\""), ], )?; - c.succeeds_with(0, true)?; + c.succeeds_with(0, true, None)?; p.expect("finding commits...\r\n")?; let mut c = p.expect_choice( "", @@ -1131,7 +1131,7 @@ mod when_main_branch_is_uptodate { format!("back"), ], )?; - c.succeeds_with(0, false)?; + c.succeeds_with(0, false, Some(0))?; p.expect("checked out proposal branch and applied 1 appendments (2 ahead 0 behind 'main')\r\n")?; p.expect_end()?; -- cgit v1.2.3