1- use crate :: { command, config:: Config , git, session, timer} ;
2- use anyhow:: { anyhow , Result } ;
1+ use crate :: { command, config:: Config , git, prompt :: Prompter , session, timer} ;
2+ use anyhow:: { Result , anyhow } ;
33use clap:: Parser ;
44use session:: State ;
55
66#[ derive( Parser , Debug ) ]
77pub struct StartOpts {
88 /// How long you want this work session to last
99 #[ clap( name = "MINUTES" ) ]
10- minutes : Option < i64 > ,
10+ pub minutes : Option < i64 > ,
1111}
1212
1313pub struct Start < ' a > {
1414 git : & ' a dyn git:: Git ,
1515 store : & ' a dyn session:: Store ,
16+ prompter : & ' a dyn Prompter ,
1617 opts : StartOpts ,
1718 config : Config ,
1819}
@@ -21,12 +22,14 @@ impl<'a> Start<'a> {
2122 pub fn new (
2223 git : & ' a impl git:: Git ,
2324 store : & ' a impl session:: Store ,
25+ prompter : & ' a impl Prompter ,
2426 opts : StartOpts ,
2527 config : Config ,
2628 ) -> Start < ' a > {
2729 Self {
2830 git,
2931 store,
32+ prompter,
3033 opts,
3134 config,
3235 }
@@ -48,11 +51,9 @@ impl<'a> Start<'a> {
4851 State :: Working { driver } => {
4952 log:: warn!( "{} has not run mob next" , driver) ;
5053 let selections = & [ "Retry" , "Take turn with the risk of losing work" ] ;
51- let selection = dialoguer:: Select :: new ( )
52- . with_prompt ( "What do you want to do?" )
53- . default ( 0 )
54- . items ( & selections[ ..] )
55- . interact ( ) ?;
54+ let selection =
55+ self . prompter
56+ . select_with_prompt ( "What do you want to do?" , selections, 0 ) ?;
5657
5758 match selection {
5859 0 => return self . run ( ) ,
@@ -83,27 +84,29 @@ impl<'a> Start<'a> {
8384
8485 log:: warn!( "Working tree is dirty:\n {}" , status) ;
8586
86- let selection = dialoguer:: Select :: new ( )
87- . default ( 0 )
88- . items ( & [ "Quit" , "Stash changes" , "Discard changes" ] )
89- . interact ( ) ?;
87+ let selection = self
88+ . prompter
89+ . select ( & [ "Quit" , "Stash changes" , "Discard changes" ] , 0 ) ?;
9090
9191 match selection {
9292 0 => Err ( anyhow ! ( "Working tree is not clean" ) ) ,
93- 1 => self . git . run ( & [ "stash" ] ) ,
94- 2 => self . git . run ( & [ "reset" , "HEAD" , "--hard" ] ) ,
93+ 1 => self . git . run ( & [ "stash" , "--include-untracked" ] ) ,
94+ 2 => self
95+ . git
96+ . run ( & [ "reset" , "HEAD" , "--hard" ] )
97+ . and_then ( |_| self . git . run ( & [ "clean" , "-fdx" ] ) ) ,
9598 _ => unreachable ! ( "could not come here" ) ,
9699 }
97100 }
98101
99102 fn take_over ( & self , from : & str , session : session:: Session ) -> Result < ( ) > {
100103 let take_and_remove = format ! ( "Take turn and remove {} from the mob" , from) ;
101104 let selections = & [ "Take turn" , take_and_remove. as_str ( ) , "Abort" ] ;
102- let selection = dialoguer :: Select :: new ( )
103- . with_prompt ( format ! ( "It's {}s turn. What do you want to do?" , from) . as_str ( ) )
104- . default ( 0 )
105- . items ( & selections [ .. ] )
106- . interact ( ) ?;
105+ let selection = self . prompter . select_with_prompt (
106+ & format ! ( "It's {}s turn. What do you want to do?" , from) ,
107+ selections ,
108+ 0 ,
109+ ) ?;
107110
108111 match selection {
109112 0 => self . start ( session) ,
@@ -158,7 +161,7 @@ impl<'a> Start<'a> {
158161
159162 let settings = match session. settings {
160163 Some ( settings) => settings,
161- None => session:: Settings :: ask ( ) ?,
164+ None => session:: Settings :: ask ( self . prompter ) ?,
162165 } ;
163166
164167 let default_branches = session:: Branches {
@@ -170,20 +173,26 @@ impl<'a> Start<'a> {
170173 ..session. branches
171174 } ;
172175
173- let branches = session:: Branches :: ask ( default_branches) ?;
176+ let branches = session:: Branches :: ask ( self . prompter , default_branches) ?;
174177
175178 let remote_branches = branches. with_remote ( & self . config . remote ) ;
176179
177180 self . git . run ( & [ "fetch" , "--all" , "--prune" ] ) ?;
178181
179- if !self . git . has_branch ( remote_branches. base_branch . as_str ( ) ) ? {
182+ println ! (
183+ "Git status: {:?}, {}" ,
184+ self . git. current_branch( ) ?,
185+ remote_branches. base_branch
186+ ) ;
187+
188+ if !self . git . has_branch ( & remote_branches. base_branch ) ? {
180189 return Err ( anyhow ! (
181190 "You need to push your branch `{}` first" ,
182191 branches. base_branch
183192 ) ) ;
184193 }
185194
186- self . git . run ( & [ "checkout" , branches. base_branch . as_str ( ) ] ) ?;
195+ self . git . run ( & [ "checkout" , & branches. base_branch ] ) ?;
187196 self . git
188197 . run ( & [ "merge" , remote_branches. base_branch . as_str ( ) , "--ff-only" ] ) ?;
189198
@@ -235,11 +244,7 @@ impl<'a> Start<'a> {
235244 "Remove local branch and checkout remote" ,
236245 "Delete local and remote branch and start fresh" ,
237246 ] ;
238- let selection = dialoguer:: Select :: new ( )
239- . with_prompt ( prompt)
240- . default ( 0 )
241- . items ( & selections[ ..] )
242- . interact ( ) ?;
247+ let selection = self . prompter . select_with_prompt ( & prompt, selections, 0 ) ?;
243248
244249 match selection {
245250 0 => {
@@ -270,11 +275,7 @@ impl<'a> Start<'a> {
270275 branches. branch
271276 ) ;
272277 let selections = & [ "Push local branch" , "Delete local branch and start fresh" ] ;
273- let selection = dialoguer:: Select :: new ( )
274- . with_prompt ( prompt)
275- . default ( 0 )
276- . items ( & selections[ ..] )
277- . interact ( ) ?;
278+ let selection = self . prompter . select_with_prompt ( & prompt, selections, 0 ) ?;
278279
279280 if selection == 0 {
280281 self . git . run ( & [
@@ -297,11 +298,7 @@ impl<'a> Start<'a> {
297298 "Checkout remote branch" ,
298299 "Delete remote branch and start fresh" ,
299300 ] ;
300- let selection = dialoguer:: Select :: new ( )
301- . with_prompt ( prompt)
302- . default ( 0 )
303- . items ( & selections[ ..] )
304- . interact ( ) ?;
301+ let selection = self . prompter . select_with_prompt ( & prompt, selections, 0 ) ?;
305302
306303 match selection {
307304 0 => {
0 commit comments