diff --git a/cmds/choose.go b/cmds/choose.go index f35304c..7b0478d 100644 --- a/cmds/choose.go +++ b/cmds/choose.go @@ -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") @@ -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"), }) diff --git a/cmds/list.go b/cmds/list.go index 59f5087..5c73875 100644 --- a/cmds/list.go +++ b/cmds/list.go @@ -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 { @@ -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"), }) diff --git a/session/list.go b/session/list.go index 0d3e7e9..db72aef 100644 --- a/session/list.go +++ b/session/list.go @@ -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) diff --git a/tmux/list.go b/tmux/list.go index c0b5b73..76c11b1 100644 --- a/tmux/list.go +++ b/tmux/list.go @@ -101,7 +101,11 @@ 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 @@ -109,6 +113,9 @@ func processSessions(sessionList []string) []*TmuxSession { if len(fields) != 21 { continue } + if o.HideAttached && fields[2] == "1" { + continue + } session := &TmuxSession{ Activity: convert.StringToTime(fields[0]), @@ -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) @@ -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 } diff --git a/tmux/list_test.go b/tmux/list_test.go index 8fa633c..f016459 100644 --- a/tmux/list_test.go +++ b/tmux/list_test.go @@ -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": { @@ -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", @@ -70,7 +80,7 @@ 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)) }) } @@ -78,7 +88,7 @@ func TestProcessSessions(t *testing.T) { 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", }) diff --git a/tmux/tmux.go b/tmux/tmux.go index b69176e..4a786e5 100644 --- a/tmux/tmux.go +++ b/tmux/tmux.go @@ -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, "" } diff --git a/tmux/tmux_test.go b/tmux/tmux_test.go index 3ec4420..c2fe275 100644 --- a/tmux/tmux_test.go +++ b/tmux/tmux_test.go @@ -1,5 +1,5 @@ package tmux func SessionsList() { - List() + List(Options{}) }