Skip to content

Commit 5c3e7f3

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

File tree

1 file changed

+31
-9
lines changed

1 file changed

+31
-9
lines changed

lister/list.go

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,19 @@ const (
3535
)
3636

3737
func (l *RealLister) List(opts ListOptions) (model.SeshSessions, error) {
38+
allSessions := sessionFromSources(opts, l)
39+
40+
allSessions = removeConfigDuplicates(allSessions)
41+
allSessions = removeZoxideDuplicates(allSessions)
42+
43+
if opts.HideAttached {
44+
allSessions = removeAttachedSession(l, allSessions)
45+
}
46+
47+
return createSeshSessions(allSessions)
48+
}
49+
50+
func sessionFromSources(opts ListOptions, l *RealLister) []model.SeshSession {
3851
allSessions := lo.FlatMap(srcs(opts), func(src string, i int) []model.SeshSession {
3952
sessions, err := srcStrategies[src](l)
4053
if err != nil {
@@ -51,31 +64,40 @@ func (l *RealLister) List(opts ListOptions) (model.SeshSessions, error) {
5164
})
5265
})
5366

54-
grouped := lo.GroupBy(allSessions, func(s model.SeshSession) string {
67+
return allSessions
68+
}
69+
70+
func removeConfigDuplicates(allSessions []model.SeshSession) []model.SeshSession {
71+
configDuplicates := lo.GroupBy(allSessions, func(s model.SeshSession) string {
5572
return s.Name
5673
})
5774

58-
allSessions = lo.MapToSlice(grouped, func(_ string, sessions []model.SeshSession) model.SeshSession {
75+
return lo.MapToSlice(configDuplicates, func(_ string, sessions []model.SeshSession) model.SeshSession {
5976
return lo.MaxBy(sessions, func(a, b model.SeshSession) bool {
6077
return a.Score > b.Score
6178
})
6279
})
80+
}
6381

82+
func removeZoxideDuplicates(allSessions []model.SeshSession) []model.SeshSession {
6483
runningSessionNames := lo.FilterMap(allSessions, func(s model.SeshSession, _ int) (string, bool) {
6584
return s.Name, s.Src == "tmux"
6685
})
6786

68-
allSessions = lo.Filter(allSessions, func(s model.SeshSession, _ int) bool {
87+
return lo.Filter(allSessions, func(s model.SeshSession, _ int) bool {
6988
return s.Src != "zoxide" || !slices.Contains(runningSessionNames, filepath.Base(s.Path))
7089
})
90+
}
7191

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-
}
92+
func removeAttachedSession(l *RealLister, allSessions []model.SeshSession) []model.SeshSession {
93+
attachedSession, _ := GetAttachedTmuxSession(l)
94+
allSessions = lo.Filter(allSessions, func(s model.SeshSession, _ int) bool {
95+
return s.Name != attachedSession.Name
96+
})
97+
return allSessions
98+
}
7899

100+
func createSeshSessions(allSessions []model.SeshSession) (model.SeshSessions, error) {
79101
sort.Slice(allSessions, func(i, j int) bool {
80102
return allSessions[i].Score > allSessions[j].Score
81103
})

0 commit comments

Comments
 (0)