Skip to content

Commit

Permalink
feat: add hide-attached flag (#51)
Browse files Browse the repository at this point in the history
Adds a --hide-attached flag to the sesh list command to hide the currently attached tmux session.
  • Loading branch information
markfeinstein authored Jan 26, 2024
1 parent b6f0de4 commit a2d2af4
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 13 deletions.
10 changes: 9 additions & 1 deletion cmds/choose.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ func Choose() *cli.Command {
Aliases: []string{"z"},
Usage: "show zoxide results",
},
&cli.BoolFlag{
Name: "hide-attached",
Aliases: []string{"H"},
Usage: "don't show currently attached sessions",
},
},
Action: func(cCtx *cli.Context) error {
cmd := exec.Command("fzf")
Expand All @@ -48,7 +53,10 @@ func Choose() *cli.Command {
return err
}

sessions := session.List(session.Srcs{
o := session.Options{
HideAttached: cCtx.Bool("hide-attached"),
}
sessions := session.List(o, session.Srcs{
Tmux: cCtx.Bool("tmux"),
Zoxide: cCtx.Bool("zoxide"),
})
Expand Down
14 changes: 11 additions & 3 deletions cmds/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import (
"fmt"
"strings"

"github.com/joshmedeski/sesh/session"
cli "github.com/urfave/cli/v2"

"github.com/urfave/cli/v2"
"github.com/joshmedeski/sesh/session"
)

func List() *cli.Command {
Expand All @@ -26,9 +26,17 @@ func List() *cli.Command {
Aliases: []string{"z"},
Usage: "show zoxide results",
},
&cli.BoolFlag{
Name: "hide-attached",
Aliases: []string{"H"},
Usage: "don't show currently attached sessions",
},
},
Action: func(cCtx *cli.Context) error {
sessions := session.List(session.Srcs{
o := session.Options{
HideAttached: cCtx.Bool("hide-attached"),
}
sessions := session.List(o, session.Srcs{
Tmux: cCtx.Bool("tmux"),
Zoxide: cCtx.Bool("zoxide"),
})
Expand Down
10 changes: 8 additions & 2 deletions session/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,19 @@ import (
"github.com/joshmedeski/sesh/zoxide"
)

func List(srcs Srcs) []string {
type Options struct {
HideAttached bool
}

func List(o Options, srcs Srcs) []string {
var sessions []string
anySrcs := checkAnyTrue(srcs)

tmuxSessions := make([]*tmux.TmuxSession, 0)
if !anySrcs || srcs.Tmux {
tmuxList, err := tmux.List()
tmuxList, err := tmux.List(tmux.Options{
HideAttached: o.HideAttached,
})
tmuxSessions = append(tmuxSessions, tmuxList...)
if err != nil {
fmt.Println("Error:", err)
Expand Down
13 changes: 10 additions & 3 deletions tmux/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,14 +101,21 @@ func format() string {
return strings.Join(variables, " ")
}

func processSessions(sessionList []string) []*TmuxSession {
type Options struct {
HideAttached bool
}

func processSessions(o Options, sessionList []string) []*TmuxSession {
sessions := make([]*TmuxSession, 0, len(sessionList))
for _, line := range sessionList {
fields := strings.Split(line, " ") // Strings split by single space

if len(fields) != 21 {
continue
}
if o.HideAttached && fields[2] == "1" {
continue
}

session := &TmuxSession{
Activity: convert.StringToTime(fields[0]),
Expand Down Expand Up @@ -147,7 +154,7 @@ func sortSessions(sessions []*TmuxSession) []*TmuxSession {
return sessions
}

func List() ([]*TmuxSession, error) {
func List(o Options) ([]*TmuxSession, error) {
format := format()
output, err := tmuxCmd([]string{"list-sessions", "-F", format})
cleanOutput := strings.TrimSpace(output)
Expand All @@ -156,7 +163,7 @@ func List() ([]*TmuxSession, error) {
}
sessionList := strings.TrimSpace(string(output))
lines := strings.Split(sessionList, "\n")
sessions := processSessions(lines)
sessions := processSessions(o, lines)

return sortSessions(sessions), nil
}
14 changes: 12 additions & 2 deletions tmux/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ func BenchmarkFormat(i *testing.B) {
func TestProcessSessions(t *testing.T) {
testCases := map[string]struct {
Input []string
Options Options
Expected []*TmuxSession
}{
"Single active session": {
Expand All @@ -36,6 +37,15 @@ func TestProcessSessions(t *testing.T) {
},
Expected: make([]*TmuxSession, 1),
},
"Hide single active session": {
Input: []string{
"1705879337 1 /dev/ttys000 1705878987 1 0 $2 1705879328 0 0 session-1 /some/test/path 1 1",
},
Options: Options{
HideAttached: true,
},
Expected: make([]*TmuxSession, 0),
},
"Single inactive session": {
Input: []string{
"1705879002 0 1705878987 1 0 $2 1705878987 0 0 session-1 /some/test/path 1 1",
Expand Down Expand Up @@ -70,15 +80,15 @@ func TestProcessSessions(t *testing.T) {

for name, tc := range testCases {
t.Run(name, func(t *testing.T) {
got := processSessions(tc.Input)
got := processSessions(tc.Options, tc.Input)
require.Equal(t, len(tc.Expected), len(got))
})
}
}

func BenchmarkProcessSessions(b *testing.B) {
for n := 0; n < b.N; n++ {
processSessions([]string{
processSessions(Options{}, []string{
"1705879337 1 /dev/ttys000 1705878987 1 0 $2 1705879328 0 0 session-1 /some/test/path 1 1",
"1705879337 1 /dev/ttys000 1705878987 1 0 $2 1705879328 0 0 session-1 /some/test/path 1 1",
})
Expand Down
2 changes: 1 addition & 1 deletion tmux/tmux.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func isAttached() bool {
}

func IsSession(session string) (bool, string) {
sessions, err := List()
sessions, err := List(Options{})
if err != nil {
return false, ""
}
Expand Down
2 changes: 1 addition & 1 deletion tmux/tmux_test.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package tmux

func SessionsList() {
List()
List(Options{})
}

0 comments on commit a2d2af4

Please sign in to comment.