Skip to content

Commit a2d2af4

Browse files
feat: add hide-attached flag (#51)
Adds a --hide-attached flag to the sesh list command to hide the currently attached tmux session.
1 parent b6f0de4 commit a2d2af4

File tree

7 files changed

+52
-13
lines changed

7 files changed

+52
-13
lines changed

cmds/choose.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@ func Choose() *cli.Command {
3131
Aliases: []string{"z"},
3232
Usage: "show zoxide results",
3333
},
34+
&cli.BoolFlag{
35+
Name: "hide-attached",
36+
Aliases: []string{"H"},
37+
Usage: "don't show currently attached sessions",
38+
},
3439
},
3540
Action: func(cCtx *cli.Context) error {
3641
cmd := exec.Command("fzf")
@@ -48,7 +53,10 @@ func Choose() *cli.Command {
4853
return err
4954
}
5055

51-
sessions := session.List(session.Srcs{
56+
o := session.Options{
57+
HideAttached: cCtx.Bool("hide-attached"),
58+
}
59+
sessions := session.List(o, session.Srcs{
5260
Tmux: cCtx.Bool("tmux"),
5361
Zoxide: cCtx.Bool("zoxide"),
5462
})

cmds/list.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ import (
44
"fmt"
55
"strings"
66

7-
"github.com/joshmedeski/sesh/session"
7+
cli "github.com/urfave/cli/v2"
88

9-
"github.com/urfave/cli/v2"
9+
"github.com/joshmedeski/sesh/session"
1010
)
1111

1212
func List() *cli.Command {
@@ -26,9 +26,17 @@ func List() *cli.Command {
2626
Aliases: []string{"z"},
2727
Usage: "show zoxide results",
2828
},
29+
&cli.BoolFlag{
30+
Name: "hide-attached",
31+
Aliases: []string{"H"},
32+
Usage: "don't show currently attached sessions",
33+
},
2934
},
3035
Action: func(cCtx *cli.Context) error {
31-
sessions := session.List(session.Srcs{
36+
o := session.Options{
37+
HideAttached: cCtx.Bool("hide-attached"),
38+
}
39+
sessions := session.List(o, session.Srcs{
3240
Tmux: cCtx.Bool("tmux"),
3341
Zoxide: cCtx.Bool("zoxide"),
3442
})

session/list.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,19 @@ import (
88
"github.com/joshmedeski/sesh/zoxide"
99
)
1010

11-
func List(srcs Srcs) []string {
11+
type Options struct {
12+
HideAttached bool
13+
}
14+
15+
func List(o Options, srcs Srcs) []string {
1216
var sessions []string
1317
anySrcs := checkAnyTrue(srcs)
1418

1519
tmuxSessions := make([]*tmux.TmuxSession, 0)
1620
if !anySrcs || srcs.Tmux {
17-
tmuxList, err := tmux.List()
21+
tmuxList, err := tmux.List(tmux.Options{
22+
HideAttached: o.HideAttached,
23+
})
1824
tmuxSessions = append(tmuxSessions, tmuxList...)
1925
if err != nil {
2026
fmt.Println("Error:", err)

tmux/list.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,14 +101,21 @@ func format() string {
101101
return strings.Join(variables, " ")
102102
}
103103

104-
func processSessions(sessionList []string) []*TmuxSession {
104+
type Options struct {
105+
HideAttached bool
106+
}
107+
108+
func processSessions(o Options, sessionList []string) []*TmuxSession {
105109
sessions := make([]*TmuxSession, 0, len(sessionList))
106110
for _, line := range sessionList {
107111
fields := strings.Split(line, " ") // Strings split by single space
108112

109113
if len(fields) != 21 {
110114
continue
111115
}
116+
if o.HideAttached && fields[2] == "1" {
117+
continue
118+
}
112119

113120
session := &TmuxSession{
114121
Activity: convert.StringToTime(fields[0]),
@@ -147,7 +154,7 @@ func sortSessions(sessions []*TmuxSession) []*TmuxSession {
147154
return sessions
148155
}
149156

150-
func List() ([]*TmuxSession, error) {
157+
func List(o Options) ([]*TmuxSession, error) {
151158
format := format()
152159
output, err := tmuxCmd([]string{"list-sessions", "-F", format})
153160
cleanOutput := strings.TrimSpace(output)
@@ -156,7 +163,7 @@ func List() ([]*TmuxSession, error) {
156163
}
157164
sessionList := strings.TrimSpace(string(output))
158165
lines := strings.Split(sessionList, "\n")
159-
sessions := processSessions(lines)
166+
sessions := processSessions(o, lines)
160167

161168
return sortSessions(sessions), nil
162169
}

tmux/list_test.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ func BenchmarkFormat(i *testing.B) {
2828
func TestProcessSessions(t *testing.T) {
2929
testCases := map[string]struct {
3030
Input []string
31+
Options Options
3132
Expected []*TmuxSession
3233
}{
3334
"Single active session": {
@@ -36,6 +37,15 @@ func TestProcessSessions(t *testing.T) {
3637
},
3738
Expected: make([]*TmuxSession, 1),
3839
},
40+
"Hide single active session": {
41+
Input: []string{
42+
"1705879337 1 /dev/ttys000 1705878987 1 0 $2 1705879328 0 0 session-1 /some/test/path 1 1",
43+
},
44+
Options: Options{
45+
HideAttached: true,
46+
},
47+
Expected: make([]*TmuxSession, 0),
48+
},
3949
"Single inactive session": {
4050
Input: []string{
4151
"1705879002 0 1705878987 1 0 $2 1705878987 0 0 session-1 /some/test/path 1 1",
@@ -70,15 +80,15 @@ func TestProcessSessions(t *testing.T) {
7080

7181
for name, tc := range testCases {
7282
t.Run(name, func(t *testing.T) {
73-
got := processSessions(tc.Input)
83+
got := processSessions(tc.Options, tc.Input)
7484
require.Equal(t, len(tc.Expected), len(got))
7585
})
7686
}
7787
}
7888

7989
func BenchmarkProcessSessions(b *testing.B) {
8090
for n := 0; n < b.N; n++ {
81-
processSessions([]string{
91+
processSessions(Options{}, []string{
8292
"1705879337 1 /dev/ttys000 1705878987 1 0 $2 1705879328 0 0 session-1 /some/test/path 1 1",
8393
"1705879337 1 /dev/ttys000 1705878987 1 0 $2 1705879328 0 0 session-1 /some/test/path 1 1",
8494
})

tmux/tmux.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ func isAttached() bool {
4141
}
4242

4343
func IsSession(session string) (bool, string) {
44-
sessions, err := List()
44+
sessions, err := List(Options{})
4545
if err != nil {
4646
return false, ""
4747
}

tmux/tmux_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
package tmux
22

33
func SessionsList() {
4-
List()
4+
List(Options{})
55
}

0 commit comments

Comments
 (0)