Skip to content

Commit a788dee

Browse files
committed
refactor: extract functions
1 parent 35f319c commit a788dee

File tree

1 file changed

+33
-10
lines changed

1 file changed

+33
-10
lines changed

lister/list.go

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,28 @@ const (
3535
)
3636

3737
func (l *RealLister) List(opts ListOptions) (model.SeshSessions, error) {
38+
allSessions := sessionFromSources(opts, l)
39+
40+
// deduplicate sessions with the same name (e.g. config)
41+
allSessions = removeConfigDuplicates(allSessions)
42+
allSessions = removeZoxideDuplicates(allSessions)
43+
44+
if opts.HideAttached {
45+
allSessions = removeAttachedSession(l, allSessions)
46+
}
47+
48+
return createSeshSessions(allSessions)
49+
}
50+
51+
func removeAttachedSession(l *RealLister, allSessions []model.SeshSession) []model.SeshSession {
52+
attachedSession, _ := GetAttachedTmuxSession(l)
53+
allSessions = lo.Filter(allSessions, func(s model.SeshSession, _ int) bool {
54+
return s.Name != attachedSession.Name
55+
})
56+
return allSessions
57+
}
58+
59+
func sessionFromSources(opts ListOptions, l *RealLister) []model.SeshSession {
3860
allSessions := lo.FlatMap(srcs(opts), func(src string, i int) []model.SeshSession {
3961
sessions, err := srcStrategies[src](l)
4062
if err != nil {
@@ -51,31 +73,32 @@ func (l *RealLister) List(opts ListOptions) (model.SeshSessions, error) {
5173
})
5274
})
5375

54-
grouped := lo.GroupBy(allSessions, func(s model.SeshSession) string {
76+
return allSessions
77+
}
78+
79+
func removeConfigDuplicates(allSessions []model.SeshSession) []model.SeshSession {
80+
configDuplicates := lo.GroupBy(allSessions, func(s model.SeshSession) string {
5581
return s.Name
5682
})
5783

58-
allSessions = lo.MapToSlice(grouped, func(_ string, sessions []model.SeshSession) model.SeshSession {
84+
return lo.MapToSlice(configDuplicates, func(_ string, sessions []model.SeshSession) model.SeshSession {
5985
return lo.MaxBy(sessions, func(a, b model.SeshSession) bool {
6086
return a.Score > b.Score
6187
})
6288
})
89+
}
6390

91+
func removeZoxideDuplicates(allSessions []model.SeshSession) []model.SeshSession {
6492
runningSessionNames := lo.FilterMap(allSessions, func(s model.SeshSession, _ int) (string, bool) {
6593
return s.Name, s.Src == "tmux"
6694
})
6795

68-
allSessions = lo.Filter(allSessions, func(s model.SeshSession, _ int) bool {
96+
return lo.Filter(allSessions, func(s model.SeshSession, _ int) bool {
6997
return s.Src != "zoxide" || !slices.Contains(runningSessionNames, filepath.Base(s.Path))
7098
})
99+
}
71100

72-
if opts.HideAttached {
73-
attachedSession, _ := GetAttachedTmuxSession(l)
74-
allSessions = lo.Filter(allSessions, func(s model.SeshSession, _ int) bool {
75-
return s.Name != attachedSession.Name
76-
})
77-
}
78-
101+
func createSeshSessions(allSessions []model.SeshSession) (model.SeshSessions, error) {
79102
sort.Slice(allSessions, func(i, j int) bool {
80103
return allSessions[i].Score > allSessions[j].Score
81104
})

0 commit comments

Comments
 (0)