@@ -15,8 +15,6 @@ import (
15
15
"github.com/manifoldco/promptui"
16
16
)
17
17
18
- var configFiles = []string {"./.tmux-sessionizer" , "~/.tmux-sessionizer" }
19
-
20
18
type ISessionHandler interface {
21
19
NewSession (ctx context.Context ) error
22
20
GrabExistingSession (ctx context.Context ) error
@@ -53,6 +51,7 @@ func (sh *SessionHandler) newTmuxCmd(name string, args ...string) *exec.Cmd {
53
51
}
54
52
55
53
func (sh * SessionHandler ) readConfig () * config {
54
+ var configFiles = []string {"./.tmux-sessionizer" , "~/.tmux-sessionizer" }
56
55
for _ , cf := range configFiles {
57
56
config , err := sh .parseConfig (cf )
58
57
if err == nil {
@@ -235,16 +234,17 @@ func (sh *SessionHandler) GrabExistingSession(ctx context.Context) error {
235
234
return nil
236
235
}
237
236
238
- func (sh * SessionHandler ) CreateNewProjectSession (ctx context.Context ) error {
239
- parentDirCandidatesSet := make (map [string ]struct {}, 0 )
237
+ func (sh * SessionHandler ) buildParentDirSet () (map [string ]struct {}, error ) {
238
+ var configFiles = []string {"./.tmux-sessionizer" , "~/.tmux-sessionizer" }
239
+ parentDirSet := make (map [string ]struct {}, 0 )
240
240
for _ , configFile := range configFiles {
241
241
path , err := sh .expandPath (configFile )
242
242
if err != nil {
243
- return fmt .Errorf ("failed to expand path: %w" , err )
243
+ return nil , fmt .Errorf ("failed to expand path: %w" , err )
244
244
}
245
245
file , err := os .Open (path )
246
246
if err != nil {
247
- return fmt .Errorf ("failed to open config file: %w" , err )
247
+ return nil , fmt .Errorf ("failed to open config file: %w" , err )
248
248
}
249
249
defer file .Close ()
250
250
@@ -257,33 +257,48 @@ func (sh *SessionHandler) CreateNewProjectSession(ctx context.Context) error {
257
257
projects := strings .Split (raw , "," )
258
258
for _ , pr := range projects {
259
259
trimmed := strings .TrimSpace (pr )
260
- parentDirCandidatesSet [trimmed ] = struct {}{}
260
+ parentDirSet [trimmed ] = struct {}{}
261
261
}
262
262
}
263
263
}
264
264
}
265
+ return parentDirSet , nil
266
+ }
265
267
266
- parentDirCandidates := make ([]string , 0 )
267
- for key , _ := range parentDirCandidatesSet {
268
- parentDirCandidates = append (parentDirCandidates , key )
268
+ func (sh * SessionHandler ) getNewProjectInfo (parentDirs []string ) (string , string , error ) {
269
+ chooseParentDirPrompt := promptui.Select {
270
+ Label : "Which project do you choose to create a new project (session) ?" ,
271
+ Items : parentDirs ,
272
+ }
273
+ _ , parentDir , err := chooseParentDirPrompt .Run ()
274
+ if err != nil {
275
+ return "" , "" , fmt .Errorf ("failed to choose a parent directory: %w" , err )
269
276
}
270
277
271
- for {
272
- chooseParentDirPrompt := promptui. Select {
273
- Label : "Which project do you choose to create a new project (session) ?" ,
274
- Items : parentDirCandidates ,
275
- }
276
- _ , parentDir , err := chooseParentDirPrompt . Run ( )
277
- if err != nil {
278
- return fmt . Errorf ( "failed to choose a parent directory: %w" , err )
279
- }
278
+ newProjectNamePrompt := promptui. Prompt {
279
+ Label : "Enter a new project name" ,
280
+ }
281
+ newProjectName , err := newProjectNamePrompt . Run ()
282
+ if err != nil {
283
+ return "" , "" , fmt . Errorf ( "failed to get a new project name: %w" , err )
284
+ }
285
+ return parentDir , newProjectName , nil
286
+ }
280
287
281
- newProjectNamePrompt := promptui.Prompt {
282
- Label : "Enter a new project name" ,
283
- }
284
- newProjectName , err := newProjectNamePrompt .Run ()
288
+ func (sh * SessionHandler ) CreateNewProjectSession (ctx context.Context ) error {
289
+ parentDirSet , err := sh .buildParentDirSet ()
290
+ if err != nil {
291
+ return fmt .Errorf ("failed to build parent directory set: %w" , err )
292
+ }
293
+ parentDirs := make ([]string , 0 )
294
+ for key := range parentDirSet {
295
+ parentDirs = append (parentDirs , key )
296
+ }
297
+
298
+ for {
299
+ parentDir , newProjectName , err := sh .getNewProjectInfo (parentDirs )
285
300
if err != nil {
286
- return fmt .Errorf ("failed to get a new project name : %w" , err )
301
+ return fmt .Errorf ("faield to get new project information interactively : %w" , err )
287
302
}
288
303
newProjectPath , err := sh .expandPath (filepath .Join (parentDir , fmt .Sprintf ("%v/" , newProjectName )))
289
304
if err != nil {
0 commit comments