-
-
Notifications
You must be signed in to change notification settings - Fork 94
feat: add sesh window command for tmux window management #345
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
fenngwd
wants to merge
7
commits into
joshmedeski:main
Choose a base branch
from
fenngwd:feat/sesh-window-command
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
2a1d791
feat(model): add TmuxWindow struct
c1fbf34
feat(tmux): add ListWindows method
047bd23
feat(tmux): add SelectWindow method
bde04f8
feat(seshcli): add window command
98b8459
feat(seshcli): register window command in root
8565746
fix: address Copilot review suggestions
d1ae27a
docs: add sesh window usage to README
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| package model | ||
|
|
||
| type TmuxWindow struct { | ||
| Name string | ||
| Path string | ||
| Index int | ||
| Active bool | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,101 @@ | ||
| package seshcli | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "fmt" | ||
| "path/filepath" | ||
| "strings" | ||
|
|
||
| "github.com/spf13/cobra" | ||
| ) | ||
|
|
||
| func NewWindowCommand(base *BaseDeps) *cobra.Command { | ||
| cmd := &cobra.Command{ | ||
| Use: "window", | ||
| Aliases: []string{"w"}, | ||
| Short: "List or switch/create windows in a tmux session", | ||
| RunE: func(cmd *cobra.Command, args []string) error { | ||
| targetSession, _ := cmd.Flags().GetString("session") | ||
| jsonOutput, _ := cmd.Flags().GetBool("json") | ||
|
|
||
| if targetSession == "" { | ||
| if !base.Tmux.IsAttached() { | ||
| return fmt.Errorf("not inside a tmux session, use --session to specify one") | ||
| } | ||
| } else { | ||
| sessions, err := base.Tmux.ListSessions() | ||
| if err != nil { | ||
| return err | ||
| } | ||
| found := false | ||
| for _, s := range sessions { | ||
| if s.Name == targetSession { | ||
| found = true | ||
| break | ||
| } | ||
| } | ||
| if !found { | ||
| return fmt.Errorf("session '%s' not found", targetSession) | ||
| } | ||
| } | ||
|
|
||
| if len(args) == 0 { | ||
| windows, err := base.Tmux.ListWindows(targetSession) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| if jsonOutput { | ||
| out, err := json.Marshal(windows) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| fmt.Println(string(out)) | ||
| return nil | ||
| } | ||
| for _, w := range windows { | ||
| fmt.Println(w.Name) | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| name := strings.Join(args, " ") | ||
|
|
||
| windows, err := base.Tmux.ListWindows(targetSession) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| for _, w := range windows { | ||
| if w.Name == name { | ||
| target := name | ||
| if targetSession != "" { | ||
| target = fmt.Sprintf("%s:%s", targetSession, name) | ||
| } | ||
| if _, err := base.Tmux.SelectWindow(target); err != nil { | ||
| return fmt.Errorf("failed to select window '%s': %w", name, err) | ||
| } | ||
| return nil | ||
| } | ||
| } | ||
|
|
||
| expanded, err := base.Home.ExpandHome(name) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| isDir, absPath := base.Dir.Dir(expanded) | ||
| if !isDir { | ||
| return fmt.Errorf("'%s' is not an existing window or valid directory", name) | ||
| } | ||
|
|
||
| windowName := filepath.Base(absPath) | ||
| if _, err := base.Tmux.NewWindowInSession(windowName, absPath, targetSession); err != nil { | ||
| return fmt.Errorf("failed to create window: %w", err) | ||
| } | ||
| return nil | ||
| }, | ||
| } | ||
|
|
||
| cmd.Flags().StringP("session", "s", "", "target session (default: current attached session)") | ||
| cmd.Flags().BoolP("json", "j", false, "output as json (list mode only)") | ||
|
|
||
| return cmd | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| package tmux | ||
|
|
||
| import ( | ||
| "strings" | ||
|
|
||
| "github.com/joshmedeski/sesh/v2/convert" | ||
| "github.com/joshmedeski/sesh/v2/model" | ||
| ) | ||
|
|
||
| func listWindowsFormat() string { | ||
| variables := []string{ | ||
| "#{window_index}", | ||
| "#{window_name}", | ||
| "#{pane_current_path}", | ||
| "#{window_active}", | ||
| } | ||
| return strings.Join(variables, separator) | ||
| } | ||
|
|
||
| func (t *RealTmux) ListWindows(targetSession string) ([]*model.TmuxWindow, error) { | ||
| var args []string | ||
| args = append(args, "list-windows") | ||
| if targetSession != "" { | ||
| args = append(args, "-t", targetSession) | ||
| } | ||
| args = append(args, "-F", listWindowsFormat()) | ||
|
|
||
| output, err := t.shell.ListCmd("tmux", args...) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| return parseTmuxWindowsOutput(output) | ||
| } | ||
|
|
||
| func parseTmuxWindowsOutput(rawList []string) ([]*model.TmuxWindow, error) { | ||
| windows := make([]*model.TmuxWindow, 0, len(rawList)) | ||
| for _, line := range rawList { | ||
| fields := strings.Split(line, separator) | ||
| if len(fields) != 4 { | ||
| continue | ||
| } | ||
| windows = append(windows, &model.TmuxWindow{ | ||
| Index: convert.StringToInt(fields[0]), | ||
| Name: fields[1], | ||
| Path: fields[2], | ||
| Active: convert.StringToBool(fields[3]), | ||
| }) | ||
| } | ||
| return windows, nil | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| package tmux | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/joshmedeski/sesh/v2/shell" | ||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/mock" | ||
| ) | ||
|
|
||
| func TestListWindows(t *testing.T) { | ||
| t.Run("returns parsed windows", func(t *testing.T) { | ||
| mockShell := &shell.MockShell{} | ||
| tmux := &RealTmux{shell: mockShell} | ||
| mockShell.EXPECT().ListCmd("tmux", "list-windows", "-F", mock.Anything).Return( | ||
| []string{"0::editor::/Users/josh/c/sesh::0", "1::server::/Users/josh/c/sesh::1"}, | ||
| nil, | ||
| ) | ||
| windows, err := tmux.ListWindows("") | ||
| assert.Nil(t, err) | ||
| assert.Len(t, windows, 2) | ||
| assert.Equal(t, "editor", windows[0].Name) | ||
| assert.Equal(t, "/Users/josh/c/sesh", windows[0].Path) | ||
| assert.Equal(t, 0, windows[0].Index) | ||
| assert.False(t, windows[0].Active) | ||
| assert.Equal(t, "server", windows[1].Name) | ||
| assert.True(t, windows[1].Active) | ||
| }) | ||
|
|
||
| t.Run("target session flag is passed when non-empty", func(t *testing.T) { | ||
| mockShell := &shell.MockShell{} | ||
| tmux := &RealTmux{shell: mockShell} | ||
| mockShell.EXPECT().ListCmd("tmux", "list-windows", "-t", "work", "-F", mock.Anything).Return( | ||
| []string{"0::main::/home/user::0"}, | ||
| nil, | ||
| ) | ||
| windows, err := tmux.ListWindows("work") | ||
| assert.Nil(t, err) | ||
| assert.Len(t, windows, 1) | ||
| }) | ||
|
|
||
| t.Run("parseTmuxWindowsOutput", func(t *testing.T) { | ||
| raw := []string{"0::editor::/Users/josh/c/sesh::0", "1::server::/Users/josh/c/sesh::1"} | ||
| windows, err := parseTmuxWindowsOutput(raw) | ||
| assert.Nil(t, err) | ||
| assert.Len(t, windows, 2) | ||
| assert.Equal(t, "editor", windows[0].Name) | ||
| assert.Equal(t, 0, windows[0].Index) | ||
| assert.False(t, windows[0].Active) | ||
| assert.Equal(t, "server", windows[1].Name) | ||
| assert.Equal(t, 1, windows[1].Index) | ||
| assert.True(t, windows[1].Active) | ||
| }) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| package tmux | ||
|
|
||
| func (t *RealTmux) NewWindowInSession(name string, startDir string, targetSession string) (string, error) { | ||
| args := []string{"new-window", "-n", name, "-c", startDir} | ||
| if targetSession != "" { | ||
| args = append(args, "-t", targetSession) | ||
| } | ||
| return t.shell.Cmd("tmux", args...) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| package tmux | ||
|
|
||
| func (t *RealTmux) SelectWindow(targetWindow string) (string, error) { | ||
| return t.shell.Cmd("tmux", "select-window", "-t", targetWindow) | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
--json/-jflag is parsed but intentionally ignored (_ = jsonOutput), so users can enable it with no observable effect. Either implement JSON output for list mode or return a clear error when--jsonis provided to avoid a silent no-op.