Skip to content

Commit 3163020

Browse files
authored
✨ Add ability to configure sorting of sources (#241)
* sorting the sources * moving sorting to the sources file * tests for sortSources() * using sort_order from toml configuration * readme documentation * remove code comment * returning a new slice, using stable sort
1 parent 8ab747c commit 3163020

File tree

5 files changed

+93
-0
lines changed

5 files changed

+93
-0
lines changed

README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,28 @@ blacklist = ["scratch"]
234234
> [!NOTE]
235235
> Works great with [tmux-floatx](https://github.com/omerxx/tmux-floax)
236236
237+
### Sorting
238+
239+
If you'd like to change the order of the sessions shown, you can configure `sort_order` in your `sesh.toml` file
240+
241+
```toml
242+
sort_order = [
243+
"tmuxinator", # show first
244+
"config",
245+
"tmux",
246+
"zoxide", # show last
247+
]
248+
```
249+
250+
The default order is `tmux`, `config`, `tmuxinator`, and then `zoxide`.
251+
252+
You can omit session types if you only care about the order of specific ones.
253+
254+
```toml
255+
sort_order = [
256+
"config", # resulting order: config, tmux, tmuxinator, zoxide
257+
]
258+
```
237259
### Default Session
238260

239261
The default session can be configured to run a command when connecting to a session. This is useful for running a dev server or starting a tmux plugin.

lister/list.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ func (l *RealLister) List(opts ListOptions) (model.SeshSessions, error) {
3030
fullOrderedIndex := make([]string, 0)
3131

3232
srcsOrderedIndex := srcs(opts)
33+
srcsOrderedIndex = sortSources(srcsOrderedIndex, l.config.SortOrder)
3334

3435
for _, src := range srcsOrderedIndex {
3536
sessions, err := srcStrategies[src](l)

lister/srcs.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,35 @@
11
package lister
22

3+
import (
4+
"cmp"
5+
"math"
6+
"slices"
7+
"strings"
8+
)
9+
10+
// returns a sorted list of sources based on the provided sort order.
11+
func sortSources(sources, sortOrder []string) []string {
12+
if sortOrder == nil || len(sortOrder) == 0 {
13+
return sources
14+
}
15+
m := make(map[string]int)
16+
for i, s := range sortOrder {
17+
m[strings.ToLower(s)] = i
18+
}
19+
getOrder := func(s string) int {
20+
if order, exists := m[strings.ToLower(s)]; exists {
21+
return order
22+
} else {
23+
return math.MaxInt
24+
}
25+
}
26+
result := slices.Clone(sources)
27+
slices.SortStableFunc(result, func(a, b string) int {
28+
return cmp.Compare(getOrder(a), getOrder(b))
29+
})
30+
return result
31+
}
32+
333
func srcs(opts ListOptions) []string {
434
var srcs []string
535
count := 0

lister/srcs_test.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,45 @@ import (
66
"github.com/stretchr/testify/assert"
77
)
88

9+
func TestSortSources(t *testing.T) {
10+
defaultSources := []string{"tmux", "config", "tmuxinator", "zoxide"}
11+
tests := map[string]struct {
12+
sortOrder []string
13+
expected []string
14+
}{
15+
"a normal configuration": {
16+
sortOrder: []string{"tmuxinator", "zoxide", "config", "tmux"},
17+
expected: []string{"tmuxinator", "zoxide", "config", "tmux"},
18+
},
19+
"empty configuration": {
20+
sortOrder: []string{},
21+
expected: []string{"tmux", "config", "tmuxinator", "zoxide"},
22+
},
23+
"partial configuration": {
24+
sortOrder: []string{"tmuxinator"},
25+
expected: []string{"tmuxinator", "tmux", "config", "zoxide"},
26+
},
27+
"superfluous elements": {
28+
sortOrder: []string{"tmuxinator", "apple", "zoxide", "banana", "config", "chocolate", "tmux"},
29+
expected: []string{"tmuxinator", "zoxide", "config", "tmux"},
30+
},
31+
"configuration with capitalization": {
32+
sortOrder: []string{"tMuxiNator", "Zoxide", "conFIg", "tmux"},
33+
expected: []string{"tmuxinator", "zoxide", "config", "tmux"},
34+
},
35+
"configuration with duplicate elements": {
36+
sortOrder: []string{"tmuxinator", "zoxide", "tmuxinator", "config", "tmuxinator", "tmux", "tmuxinator", "tmuxinator"},
37+
expected: []string{"zoxide", "config", "tmux", "tmuxinator"},
38+
},
39+
}
40+
for name, tt := range tests {
41+
t.Run(name, func(t *testing.T) {
42+
actual := sortSources(defaultSources, tt.sortOrder)
43+
assert.Equal(t, tt.expected, actual)
44+
})
45+
}
46+
}
47+
948
func TestSrcs(t *testing.T) {
1049
tests := []struct {
1150
name string

model/config.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ type (
66
DefaultSessionConfig DefaultSessionConfig `toml:"default_session"`
77
Blacklist []string `toml:"blacklist"`
88
SessionConfigs []SessionConfig `toml:"session"`
9+
SortOrder []string `toml:"sort_order"`
910
}
1011

1112
DefaultSessionConfig struct {

0 commit comments

Comments
 (0)