Skip to content

Commit 577dbb2

Browse files
committed
feat: enable to change tmux session even in another tmux session.
1 parent ed642e6 commit 577dbb2

File tree

3 files changed

+30
-4
lines changed

3 files changed

+30
-4
lines changed

cmd/sessionizer.go

-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package cmd
33
import (
44
"context"
55
"errors"
6-
"fmt"
76
"os"
87
"os/signal"
98

@@ -22,7 +21,6 @@ func Core() int {
2221

2322
cmd := newCmd()
2423
if err := cmd.Run(ctx, os.Args); err != nil {
25-
fmt.Println(err)
2624
return ExitCodeError
2725
}
2826
return ExitCodeOK

handler/handler.go

+30-1
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,9 @@ func (sh *SessionHandler) NewSession() error {
128128
return fmt.Errorf("failed to create new project from projects: %w", err)
129129
}
130130
sessionName := strings.Trim(fzfOut.String(), "\n")
131+
if sh.isInSession() {
132+
return sh.switchToNewClient(sessionName, projectHashMap[sessionName])
133+
}
131134
if sh.sessionExists(sessionName) {
132135
return sh.attach(sessionName)
133136
}
@@ -161,6 +164,25 @@ func (sh *SessionHandler) attach(sessionName string) error {
161164
return nil
162165
}
163166

167+
func (sh *SessionHandler) switchClient(sessionName string) error {
168+
cmd := sh.newTmuxCmd("tmux", "switch-client", "-t", sessionName)
169+
if err := cmd.Run(); err != nil {
170+
return fmt.Errorf("failed to execute tmux switch-client -t: %w", err)
171+
}
172+
return nil
173+
}
174+
175+
func (sh *SessionHandler) switchToNewClient(sessionName, path string) error {
176+
if sh.sessionExists(sessionName) {
177+
return sh.switchClient(sessionName)
178+
}
179+
cmd := sh.newTmuxCmd("tmux", "new-session", "-ds", sessionName, "-c", path)
180+
if err := cmd.Run(); err != nil {
181+
return fmt.Errorf("failed to execute tmux new-sessio -ds %v -c %v: %w", sessionName, path, err)
182+
}
183+
return sh.switchClient(sessionName)
184+
}
185+
164186
func (sh *SessionHandler) toFzf(input bytes.Buffer) (bytes.Buffer, error) {
165187
fzfCmd := exec.Command("fzf")
166188
fzfCmd.Stdin = &input
@@ -184,8 +206,15 @@ func (sh *SessionHandler) GrabExistingSession() error {
184206
return fmt.Errorf("failed to execute fzf command: %w", err)
185207
}
186208
selected := strings.TrimSpace(fzfOut.String())
209+
if sh.isInSession() {
210+
return sh.switchClient(selected)
211+
}
187212
if err := sh.attach(selected); err != nil {
188-
return fmt.Errorf("failed to attache an existing session: %w", err)
213+
return fmt.Errorf("failed to attach an existing session: %w", err)
189214
}
190215
return nil
191216
}
217+
218+
func (sh *SessionHandler) isInSession() bool {
219+
return len(os.Getenv("TMUX")) > 0
220+
}

main.go

-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,5 @@ import (
1010
func main() {
1111
logger := slog.New(slog.NewJSONHandler(os.Stdout, nil))
1212
slog.SetDefault(logger)
13-
1413
os.Exit(cmd.Core())
1514
}

0 commit comments

Comments
 (0)