upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDanConwayDev <DanConwayDev@protonmail.com>2024-02-20 16:22:10 +0000
committerDanConwayDev <DanConwayDev@protonmail.com>2024-02-20 16:48:48 +0000
commit141ebf0cc0c6cfea640debc9b9073303509a8bc7 (patch)
tree579ef546ea83b13a56066e44c43318ad9ed4f3d0
parentc2817d081700d1fe14d92c51c4e89551182e7fb6 (diff)
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...
-rw-r--r--src/cli_interactor.rs19
-rw-r--r--src/sub_commands/list.rs79
-rw-r--r--test_utils/src/lib.rs11
-rw-r--r--tests/list.rs40
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 {
41 Ok(confirm) 41 Ok(confirm)
42 } 42 }
43 fn choice(&self, parms: PromptChoiceParms) -> Result<usize> { 43 fn choice(&self, parms: PromptChoiceParms) -> Result<usize> {
44 dialoguer::Select::with_theme(&self.theme) 44 let mut choice = dialoguer::Select::with_theme(&self.theme);
45 choice
45 .with_prompt(parms.prompt) 46 .with_prompt(parms.prompt)
46 .report(parms.report) 47 .report(parms.report)
47 .items(&parms.choices) 48 .items(&parms.choices);
48 .interact() 49 if let Some(default) = parms.default {
49 .context("failed to get choice") 50 if std::env::var("NGITTEST").is_err() {
51 choice.default(default);
52 }
53 }
54 choice.interact().context("failed to get choice")
50 } 55 }
51} 56}
52 57
@@ -110,6 +115,7 @@ impl PromptConfirmParms {
110pub struct PromptChoiceParms { 115pub struct PromptChoiceParms {
111 pub prompt: String, 116 pub prompt: String,
112 pub choices: Vec<String>, 117 pub choices: Vec<String>,
118 pub default: Option<usize>,
113 pub report: bool, 119 pub report: bool,
114} 120}
115 121
@@ -128,4 +134,9 @@ impl PromptChoiceParms {
128 self.choices = choices; 134 self.choices = choices;
129 self 135 self
130 } 136 }
137
138 pub fn with_default(mut self, index: usize) -> Self {
139 self.default = Some(index);
140 self
141 }
131} 142}
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<()> {
121 121
122 if no_support_for_patches_as_branch { 122 if no_support_for_patches_as_branch {
123 println!("{patch_text_ref}"); 123 println!("{patch_text_ref}");
124 return match Interactor::default().choice(PromptChoiceParms::default().with_choices( 124 return match Interactor::default().choice(
125 vec![ 125 PromptChoiceParms::default()
126 "learn why 'patch only' proposals can't be checked out".to_string(), 126 .with_default(0)
127 format!("apply to current branch with `git am`"), 127 .with_choices(vec![
128 format!("download to ./patches"), 128 "learn why 'patch only' proposals can't be checked out".to_string(),
129 "back".to_string(), 129 format!("apply to current branch with `git am`"),
130 ], 130 format!("download to ./patches"),
131 ))? { 131 "back".to_string(),
132 ]),
133 )? {
132 0 => { 134 0 => {
133 println!("Some proposals are posted as 'patch only'\n"); 135 println!("Some proposals are posted as 'patch only'\n");
134 println!( 136 println!(
@@ -144,7 +146,9 @@ pub async fn launch(_cli_args: &Cli, _args: &SubCommandArgs) -> Result<()> {
144 "by default ngit posts proposals that support both the branch and patch model so either workflow can be used" 146 "by default ngit posts proposals that support both the branch and patch model so either workflow can be used"
145 ); 147 );
146 Interactor::default().choice( 148 Interactor::default().choice(
147 PromptChoiceParms::default().with_choices(vec!["back".to_string()]), 149 PromptChoiceParms::default()
150 .with_default(0)
151 .with_choices(vec!["back".to_string()]),
148 )?; 152 )?;
149 continue; 153 continue;
150 } 154 }
@@ -180,7 +184,7 @@ pub async fn launch(_cli_args: &Cli, _args: &SubCommandArgs) -> Result<()> {
180 if !git_repo.does_commit_exist(&proposal_base_commit.to_string())? { 184 if !git_repo.does_commit_exist(&proposal_base_commit.to_string())? {
181 println!("your '{main_branch_name}' branch may not be up-to-date."); 185 println!("your '{main_branch_name}' branch may not be up-to-date.");
182 println!("the proposal parent commit doesnt exist in your local repository."); 186 println!("the proposal parent commit doesnt exist in your local repository.");
183 return match Interactor::default().choice(PromptChoiceParms::default().with_choices( 187 return match Interactor::default().choice(PromptChoiceParms::default().with_default(0).with_choices(
184 vec![ 188 vec![
185 format!( 189 format!(
186 "manually run `git pull` on '{main_branch_name}' and select proposal again" 190 "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<()> {
213 // branch doesnt exist 217 // branch doesnt exist
214 if !branch_exists { 218 if !branch_exists {
215 return match Interactor::default() 219 return match Interactor::default()
216 .choice(PromptChoiceParms::default().with_choices(vec![ 220 .choice(PromptChoiceParms::default().with_default(0).with_choices(vec![
217 format!( 221 format!(
218 "create and checkout proposal branch ({} ahead {} behind '{main_branch_name}')", 222 "create and checkout proposal branch ({} ahead {} behind '{main_branch_name}')",
219 most_recent_proposal_patch_chain.len(), 223 most_recent_proposal_patch_chain.len(),
@@ -255,6 +259,7 @@ pub async fn launch(_cli_args: &Cli, _args: &SubCommandArgs) -> Result<()> {
255 println!("branch checked out and up-to-date"); 259 println!("branch checked out and up-to-date");
256 return match Interactor::default().choice( 260 return match Interactor::default().choice(
257 PromptChoiceParms::default() 261 PromptChoiceParms::default()
262 .with_default(0)
258 .with_choices(vec!["exit".to_string(), "back".to_string()]), 263 .with_choices(vec!["exit".to_string(), "back".to_string()]),
259 )? { 264 )? {
260 0 => Ok(()), 265 0 => Ok(()),
@@ -265,18 +270,20 @@ pub async fn launch(_cli_args: &Cli, _args: &SubCommandArgs) -> Result<()> {
265 }; 270 };
266 } 271 }
267 272
268 return match Interactor::default().choice(PromptChoiceParms::default().with_choices( 273 return match Interactor::default().choice(
269 vec![ 274 PromptChoiceParms::default()
270 format!( 275 .with_default(0)
271 "checkout proposal branch ({} ahead {} behind '{main_branch_name}')", 276 .with_choices(vec![
272 most_recent_proposal_patch_chain.len(), 277 format!(
273 proposal_behind_main.len(), 278 "checkout proposal branch ({} ahead {} behind '{main_branch_name}')",
274 ), 279 most_recent_proposal_patch_chain.len(),
275 format!("apply to current branch with `git am`"), 280 proposal_behind_main.len(),
276 format!("download to ./patches"), 281 ),
277 "back".to_string(), 282 format!("apply to current branch with `git am`"),
278 ], 283 format!("download to ./patches"),
279 ))? { 284 "back".to_string(),
285 ]),
286 )? {
280 0 => { 287 0 => {
281 check_clean(&git_repo)?; 288 check_clean(&git_repo)?;
282 git_repo.checkout(&cover_letter.branch_name)?; 289 git_repo.checkout(&cover_letter.branch_name)?;
@@ -304,14 +311,16 @@ pub async fn launch(_cli_args: &Cli, _args: &SubCommandArgs) -> Result<()> {
304 .unwrap_or_default() 311 .unwrap_or_default()
305 .eq(&local_branch_tip.to_string()) 312 .eq(&local_branch_tip.to_string())
306 }) { 313 }) {
307 return match Interactor::default().choice(PromptChoiceParms::default().with_choices( 314 return match Interactor::default().choice(
308 vec![ 315 PromptChoiceParms::default()
309 format!("checkout proposal branch and apply {} appendments", &index,), 316 .with_default(0)
310 format!("apply to current branch with `git am`"), 317 .with_choices(vec![
311 format!("download to ./patches"), 318 format!("checkout proposal branch and apply {} appendments", &index,),
312 "back".to_string(), 319 format!("apply to current branch with `git am`"),
313 ], 320 format!("download to ./patches"),
314 ))? { 321 "back".to_string(),
322 ]),
323 )? {
315 0 => { 324 0 => {
316 check_clean(&git_repo)?; 325 check_clean(&git_repo)?;
317 git_repo.checkout(&cover_letter.branch_name)?; 326 git_repo.checkout(&cover_letter.branch_name)?;
@@ -346,7 +355,7 @@ pub async fn launch(_cli_args: &Cli, _args: &SubCommandArgs) -> Result<()> {
346 .eq(&local_branch_tip.to_string()) 355 .eq(&local_branch_tip.to_string())
347 }) { 356 }) {
348 return match Interactor::default().choice( 357 return match Interactor::default().choice(
349 PromptChoiceParms::default() 358 PromptChoiceParms::default().with_default(0)
350 .with_choices( 359 .with_choices(
351 vec![ 360 vec![
352 format!( 361 format!(
@@ -410,7 +419,7 @@ pub async fn launch(_cli_args: &Cli, _args: &SubCommandArgs) -> Result<()> {
410 local_ahead_of_proposal.len() 419 local_ahead_of_proposal.len()
411 ); 420 );
412 return match Interactor::default().choice( 421 return match Interactor::default().choice(
413 PromptChoiceParms::default() 422 PromptChoiceParms::default().with_default(0)
414 .with_choices( 423 .with_choices(
415 vec![ 424 vec![
416 format!( 425 format!(
@@ -450,7 +459,7 @@ pub async fn launch(_cli_args: &Cli, _args: &SubCommandArgs) -> Result<()> {
450 local_beind_main.len(), 459 local_beind_main.len(),
451 ); 460 );
452 return match Interactor::default().choice( 461 return match Interactor::default().choice(
453 PromptChoiceParms::default() 462 PromptChoiceParms::default().with_default(0)
454 .with_choices( 463 .with_choices(
455 vec![ 464 vec![
456 format!( 465 format!(
@@ -514,7 +523,7 @@ pub async fn launch(_cli_args: &Cli, _args: &SubCommandArgs) -> Result<()> {
514 ); 523 );
515 524
516 return match Interactor::default().choice( 525 return match Interactor::default().choice(
517 PromptChoiceParms::default() 526 PromptChoiceParms::default().with_default(0)
518 .with_choices( 527 .with_choices(
519 vec![ 528 vec![
520 format!( 529 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<'_> {
479 Ok(self) 479 Ok(self)
480 } 480 }
481 481
482 pub fn succeeds_with(&mut self, chosen_index: u64, report: bool) -> Result<&mut Self> { 482 pub fn succeeds_with(
483 &mut self,
484 chosen_index: u64,
485 report: bool,
486 default_index: Option<u64>,
487 ) -> Result<&mut Self> {
488 if default_index.is_some() {
489 println!("TODO: add support for default choice")
490 }
491
483 fn show_options( 492 fn show_options(
484 tester: &mut CliTester, 493 tester: &mut CliTester,
485 choices: &Vec<String>, 494 choices: &Vec<String>,
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 {
266 format!("\"{PROPOSAL_TITLE_3}\""), 266 format!("\"{PROPOSAL_TITLE_3}\""),
267 ], 267 ],
268 )?; 268 )?;
269 c.succeeds_with(0, true)?; 269 c.succeeds_with(0, true, None)?;
270 270
271 p.expect("finding commits...\r\n")?; 271 p.expect("finding commits...\r\n")?;
272 let mut c = p.expect_choice( 272 let mut c = p.expect_choice(
@@ -279,7 +279,7 @@ mod when_main_branch_is_uptodate {
279 format!("back"), 279 format!("back"),
280 ], 280 ],
281 )?; 281 )?;
282 c.succeeds_with(0, false)?; 282 c.succeeds_with(0, false, Some(0))?;
283 p.expect(format!( 283 p.expect(format!(
284 "checked out proposal as '{FEATURE_BRANCH_NAME_1}' branch\r\n" 284 "checked out proposal as '{FEATURE_BRANCH_NAME_1}' branch\r\n"
285 ))?; 285 ))?;
@@ -339,7 +339,7 @@ mod when_main_branch_is_uptodate {
339 format!("\"{PROPOSAL_TITLE_3}\""), 339 format!("\"{PROPOSAL_TITLE_3}\""),
340 ], 340 ],
341 )?; 341 )?;
342 c.succeeds_with(0, true)?; 342 c.succeeds_with(0, true, None)?;
343 p.expect("finding commits...\r\n")?; 343 p.expect("finding commits...\r\n")?;
344 let mut c = p.expect_choice( 344 let mut c = p.expect_choice(
345 "", 345 "",
@@ -351,7 +351,7 @@ mod when_main_branch_is_uptodate {
351 format!("back"), 351 format!("back"),
352 ], 352 ],
353 )?; 353 )?;
354 c.succeeds_with(0, false)?; 354 c.succeeds_with(0, false, None)?;
355 p.expect(format!( 355 p.expect(format!(
356 "checked out proposal as '{FEATURE_BRANCH_NAME_1}' branch\r\n" 356 "checked out proposal as '{FEATURE_BRANCH_NAME_1}' branch\r\n"
357 ))?; 357 ))?;
@@ -455,7 +455,7 @@ mod when_main_branch_is_uptodate {
455 format!("\"{PROPOSAL_TITLE_3}\""), 455 format!("\"{PROPOSAL_TITLE_3}\""),
456 ], 456 ],
457 )?; 457 )?;
458 c.succeeds_with(2, true)?; 458 c.succeeds_with(2, true, None)?;
459 459
460 p.expect("finding commits...\r\n")?; 460 p.expect("finding commits...\r\n")?;
461 let mut c = p.expect_choice( 461 let mut c = p.expect_choice(
@@ -468,7 +468,7 @@ mod when_main_branch_is_uptodate {
468 format!("back"), 468 format!("back"),
469 ], 469 ],
470 )?; 470 )?;
471 c.succeeds_with(0, false)?; 471 c.succeeds_with(0, false, Some(0))?;
472 p.expect(format!( 472 p.expect(format!(
473 "checked out proposal as '{FEATURE_BRANCH_NAME_3}' branch\r\n" 473 "checked out proposal as '{FEATURE_BRANCH_NAME_3}' branch\r\n"
474 ))?; 474 ))?;
@@ -529,7 +529,7 @@ mod when_main_branch_is_uptodate {
529 format!("\"{PROPOSAL_TITLE_3}\""), 529 format!("\"{PROPOSAL_TITLE_3}\""),
530 ], 530 ],
531 )?; 531 )?;
532 c.succeeds_with(2, true)?; 532 c.succeeds_with(2, true, None)?;
533 p.expect("finding commits...\r\n")?; 533 p.expect("finding commits...\r\n")?;
534 let mut c = p.expect_choice( 534 let mut c = p.expect_choice(
535 "", 535 "",
@@ -541,7 +541,7 @@ mod when_main_branch_is_uptodate {
541 format!("back"), 541 format!("back"),
542 ], 542 ],
543 )?; 543 )?;
544 c.succeeds_with(0, false)?; 544 c.succeeds_with(0, false, Some(0))?;
545 p.expect(format!( 545 p.expect(format!(
546 "checked out proposal as '{FEATURE_BRANCH_NAME_3}' branch\r\n" 546 "checked out proposal as '{FEATURE_BRANCH_NAME_3}' branch\r\n"
547 ))?; 547 ))?;
@@ -651,7 +651,7 @@ mod when_main_branch_is_uptodate {
651 format!("add d3.md"), // commit msg title 651 format!("add d3.md"), // commit msg title
652 ], 652 ],
653 )?; 653 )?;
654 c.succeeds_with(3, true)?; 654 c.succeeds_with(3, true, None)?;
655 p.expect("finding commits...\r\n")?; 655 p.expect("finding commits...\r\n")?;
656 let mut c = p.expect_choice( 656 let mut c = p.expect_choice(
657 "", 657 "",
@@ -663,7 +663,7 @@ mod when_main_branch_is_uptodate {
663 format!("back"), 663 format!("back"),
664 ], 664 ],
665 )?; 665 )?;
666 c.succeeds_with(0, false)?; 666 c.succeeds_with(0, false, Some(0))?;
667 p.expect(format!( 667 p.expect(format!(
668 "checked out proposal as '{FEATURE_BRANCH_NAME_4}' branch\r\n" 668 "checked out proposal as '{FEATURE_BRANCH_NAME_4}' branch\r\n"
669 ))?; 669 ))?;
@@ -730,7 +730,7 @@ mod when_main_branch_is_uptodate {
730 format!("add d3.md"), // commit msg title 730 format!("add d3.md"), // commit msg title
731 ], 731 ],
732 )?; 732 )?;
733 c.succeeds_with(3, true)?; 733 c.succeeds_with(3, true, None)?;
734 p.expect("finding commits...\r\n")?; 734 p.expect("finding commits...\r\n")?;
735 let mut c = p.expect_choice( 735 let mut c = p.expect_choice(
736 "", 736 "",
@@ -742,7 +742,7 @@ mod when_main_branch_is_uptodate {
742 format!("back"), 742 format!("back"),
743 ], 743 ],
744 )?; 744 )?;
745 c.succeeds_with(0, false)?; 745 c.succeeds_with(0, false, Some(0))?;
746 p.expect(format!( 746 p.expect(format!(
747 "checked out proposal as '{FEATURE_BRANCH_NAME_4}' branch\r\n" 747 "checked out proposal as '{FEATURE_BRANCH_NAME_4}' branch\r\n"
748 ))?; 748 ))?;
@@ -861,7 +861,7 @@ mod when_main_branch_is_uptodate {
861 format!("\"{PROPOSAL_TITLE_3}\""), 861 format!("\"{PROPOSAL_TITLE_3}\""),
862 ], 862 ],
863 )?; 863 )?;
864 c.succeeds_with(0, true)?; 864 c.succeeds_with(0, true, None)?;
865 p.expect("finding commits...\r\n")?; 865 p.expect("finding commits...\r\n")?;
866 let mut c = p.expect_choice( 866 let mut c = p.expect_choice(
867 "", 867 "",
@@ -872,7 +872,7 @@ mod when_main_branch_is_uptodate {
872 format!("back"), 872 format!("back"),
873 ], 873 ],
874 )?; 874 )?;
875 c.succeeds_with(0, false)?; 875 c.succeeds_with(0, false, Some(0))?;
876 p.expect(format!( 876 p.expect(format!(
877 "checked out proposal as '{FEATURE_BRANCH_NAME_1}' branch\r\n" 877 "checked out proposal as '{FEATURE_BRANCH_NAME_1}' branch\r\n"
878 ))?; 878 ))?;
@@ -940,7 +940,7 @@ mod when_main_branch_is_uptodate {
940 format!("\"{PROPOSAL_TITLE_3}\""), 940 format!("\"{PROPOSAL_TITLE_3}\""),
941 ], 941 ],
942 )?; 942 )?;
943 c.succeeds_with(0, true)?; 943 c.succeeds_with(0, true, None)?;
944 p.expect("finding commits...\r\n")?; 944 p.expect("finding commits...\r\n")?;
945 let mut c = p.expect_choice( 945 let mut c = p.expect_choice(
946 "", 946 "",
@@ -951,7 +951,7 @@ mod when_main_branch_is_uptodate {
951 format!("back"), 951 format!("back"),
952 ], 952 ],
953 )?; 953 )?;
954 c.succeeds_with(0, false)?; 954 c.succeeds_with(0, false, Some(0))?;
955 p.expect(format!( 955 p.expect(format!(
956 "checked out proposal as '{FEATURE_BRANCH_NAME_1}' branch\r\n" 956 "checked out proposal as '{FEATURE_BRANCH_NAME_1}' branch\r\n"
957 ))?; 957 ))?;
@@ -1042,7 +1042,7 @@ mod when_main_branch_is_uptodate {
1042 format!("\"{PROPOSAL_TITLE_3}\""), 1042 format!("\"{PROPOSAL_TITLE_3}\""),
1043 ], 1043 ],
1044 )?; 1044 )?;
1045 c.succeeds_with(0, true)?; 1045 c.succeeds_with(0, true, None)?;
1046 p.expect("finding commits...\r\n")?; 1046 p.expect("finding commits...\r\n")?;
1047 let mut c = p.expect_choice( 1047 let mut c = p.expect_choice(
1048 "", 1048 "",
@@ -1053,7 +1053,7 @@ mod when_main_branch_is_uptodate {
1053 format!("back"), 1053 format!("back"),
1054 ], 1054 ],
1055 )?; 1055 )?;
1056 c.succeeds_with(0, false)?; 1056 c.succeeds_with(0, false, Some(0))?;
1057 p.expect("checked out proposal branch and applied 1 appendments (2 ahead 0 behind 'main')\r\n")?; 1057 p.expect("checked out proposal branch and applied 1 appendments (2 ahead 0 behind 'main')\r\n")?;
1058 p.expect_end()?; 1058 p.expect_end()?;
1059 1059
@@ -1120,7 +1120,7 @@ mod when_main_branch_is_uptodate {
1120 format!("\"{PROPOSAL_TITLE_3}\""), 1120 format!("\"{PROPOSAL_TITLE_3}\""),
1121 ], 1121 ],
1122 )?; 1122 )?;
1123 c.succeeds_with(0, true)?; 1123 c.succeeds_with(0, true, None)?;
1124 p.expect("finding commits...\r\n")?; 1124 p.expect("finding commits...\r\n")?;
1125 let mut c = p.expect_choice( 1125 let mut c = p.expect_choice(
1126 "", 1126 "",
@@ -1131,7 +1131,7 @@ mod when_main_branch_is_uptodate {
1131 format!("back"), 1131 format!("back"),
1132 ], 1132 ],
1133 )?; 1133 )?;
1134 c.succeeds_with(0, false)?; 1134 c.succeeds_with(0, false, Some(0))?;
1135 p.expect("checked out proposal branch and applied 1 appendments (2 ahead 0 behind 'main')\r\n")?; 1135 p.expect("checked out proposal branch and applied 1 appendments (2 ahead 0 behind 'main')\r\n")?;
1136 p.expect_end()?; 1136 p.expect_end()?;
1137 1137