Skip to content

Commit 33fa497

Browse files
committed
refactor(tmux): optimize blacklist processing
Use a map to store blacklisted session names for faster lookup and simplify the listTmux function logic.
1 parent fc7a553 commit 33fa497

File tree

1 file changed

+24
-19
lines changed

1 file changed

+24
-19
lines changed

lister/tmux.go

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -20,36 +20,41 @@ func isBlacklisted(blacklist []string, name string) bool {
2020
return false
2121
}
2222

23+
func createBlacklistSet(blacklist []string) map[string]struct{} {
24+
blacklistSet := make(map[string]struct{}, len(blacklist))
25+
for _, name := range blacklist {
26+
blacklistSet[name] = struct{}{}
27+
}
28+
return blacklistSet
29+
}
30+
2331
func listTmux(l *RealLister) (model.SeshSessions, error) {
2432
tmuxSessions, err := l.tmux.ListSessions()
2533
if err != nil {
2634
return model.SeshSessions{}, fmt.Errorf("couldn't list tmux sessions: %q", err)
2735
}
28-
numOfSessions := len(tmuxSessions)
29-
orderedIndex := make([]string, numOfSessions)
30-
directory := make(model.SeshSessionMap)
31-
for i, session := range tmuxSessions {
32-
key := tmuxKey(session.Name)
33-
orderedIndex[i] = key
34-
directory[key] = model.SeshSession{
35-
Src: "tmux",
36-
Name: session.Name,
37-
Path: session.Path,
38-
Attached: session.Attached,
39-
Windows: session.Windows,
40-
}
41-
}
4236

43-
finalOrderedIndex := []string{}
44-
for _, key := range orderedIndex {
45-
if !isBlacklisted(l.config.Blacklist, directory[key].Name) {
46-
finalOrderedIndex = append(finalOrderedIndex, key)
37+
blacklistSet := createBlacklistSet(l.config.Blacklist)
38+
directory := make(map[string]model.SeshSession)
39+
orderedIndex := []string{}
40+
41+
for _, session := range tmuxSessions {
42+
if _, blacklisted := blacklistSet[session.Name]; !blacklisted {
43+
key := tmuxKey(session.Name)
44+
orderedIndex = append(orderedIndex, key)
45+
directory[key] = model.SeshSession{
46+
Src: "tmux",
47+
Name: session.Name,
48+
Path: session.Path,
49+
Attached: session.Attached,
50+
Windows: session.Windows,
51+
}
4752
}
4853
}
4954

5055
return model.SeshSessions{
5156
Directory: directory,
52-
OrderedIndex: finalOrderedIndex,
57+
OrderedIndex: orderedIndex,
5358
}, nil
5459
}
5560

0 commit comments

Comments
 (0)