From f823a796e8c20a2d40f70505ee2ee8bec96b788a Mon Sep 17 00:00:00 2001 From: Dries De Peuter Date: Mon, 8 Jan 2024 19:10:51 +0100 Subject: [PATCH] feat: Add tmux session switcher --- internal/pkg/command/go.go | 61 ++++++++++++++++++++---------------- internal/pkg/command/root.go | 1 + internal/pkg/command/tmux.go | 54 +++++++++++++++++++++++++++++++ 3 files changed, 89 insertions(+), 27 deletions(-) create mode 100644 internal/pkg/command/tmux.go diff --git a/internal/pkg/command/go.go b/internal/pkg/command/go.go index 11e2ede..0b8f398 100644 --- a/internal/pkg/command/go.go +++ b/internal/pkg/command/go.go @@ -14,36 +14,15 @@ import ( func getGoCmd() *cobra.Command { cmd := &cobra.Command{ - Use: "go [search]", - Aliases: []string{"cd"}, - Short: "Fuzzyfind a project and jump to it", + Use: "go [search]", + Aliases: []string{"cd"}, + Short: "Fuzzyfind a project and jump to it", Run: func(cmd *cobra.Command, args []string) { - baseDir := config.GetBaseDir() - if baseDir == "" { - logrus.Fatal("Basedir not set. Run `ph setup` to set it.") - } - var mut sync.RWMutex - paths := []string{} - go func(paths *[]string) { - err := repo.GetRepoPathsAsync(baseDir, paths) - if err != nil { - logrus.Fatal(fmt.Errorf("failed to get repo paths: %w", err)) - } - }(&paths) - - idx, err := fuzzyfinder.Find( - &paths, - func(i int) string { - return paths[i] - }, - fuzzyfinder.WithQuery(strings.Join(args, " ")), - fuzzyfinder.WithSelectOne(), - fuzzyfinder.WithHotReloadLock(mut.RLocker()), - ) + path, err := findPath(strings.Join(args, " ")) switch err { case nil: - logrus.Infof("Jumping to %s", paths[idx]) - fmt.Fprintf(CmdOutput, "cd %s\n", paths[idx]) + logrus.Infof("Jumping to %s", path) + fmt.Fprintf(CmdOutput, "cd %s\n", path) case fuzzyfinder.ErrAbort: logrus.Fatal("aborted") default: @@ -54,3 +33,31 @@ func getGoCmd() *cobra.Command { return cmd } +func findPath(query string) (string, error) { + baseDir := config.GetBaseDir() + if baseDir == "" { + logrus.Fatal("Basedir not set. Run `ph setup` to set it.") + } + var mut sync.RWMutex + paths := []string{} + go func(paths *[]string) { + err := repo.GetRepoPathsAsync(baseDir, paths) + if err != nil { + logrus.Fatal(fmt.Errorf("failed to get repo paths: %w", err)) + } + }(&paths) + + idx, err := fuzzyfinder.Find( + &paths, + func(i int) string { + return paths[i] + }, + fuzzyfinder.WithQuery(query), + fuzzyfinder.WithSelectOne(), + fuzzyfinder.WithHotReloadLock(mut.RLocker()), + ) + if err != nil { + return "", err + } + return paths[idx], nil +} diff --git a/internal/pkg/command/root.go b/internal/pkg/command/root.go index ccc2a9f..9d53d9c 100644 --- a/internal/pkg/command/root.go +++ b/internal/pkg/command/root.go @@ -43,6 +43,7 @@ func Execute() { rootCmd.AddCommand(getVersionCmd()) rootCmd.AddCommand(getOrgCmd()) rootCmd.AddCommand(getWDIDCmd()) + rootCmd.AddCommand(getTmuxCmd()) cobra.OnInitialize(config.InitConfig) diff --git a/internal/pkg/command/tmux.go b/internal/pkg/command/tmux.go new file mode 100644 index 0000000..5e49b2c --- /dev/null +++ b/internal/pkg/command/tmux.go @@ -0,0 +1,54 @@ +package command + +import ( + "fmt" + "os" + "os/exec" + "path/filepath" + "strings" + + "github.com/ktr0731/go-fuzzyfinder" + "github.com/sirupsen/logrus" + "github.com/spf13/cobra" +) + +func getTmuxCmd() *cobra.Command { + cmd := &cobra.Command{ + Use: "tmux [search]", + Aliases: []string{"cd"}, + Short: "tmux session of result", + Run: func(cmd *cobra.Command, args []string) { + path, err := findPath(strings.Join(args, " ")) + name := strings.ReplaceAll(filepath.Base(path), ".", "_") + + if err != nil { + if err == fuzzyfinder.ErrAbort { + logrus.Fatal("aborted") + } else { + logrus.Fatal(fmt.Errorf("failed to find repo: %w", err)) + } + os.Exit(1) + + } + + isRunning := exec.Command("pgrep", "tmux").Run() == nil + + if !isRunning { + logrus.Infof("Starting tmux with new session '%s' %s", name, path) + fmt.Fprintf(CmdOutput, "tmux new-session -s %s -c %s; ", name, path) + os.Exit(0) + } + + hasSession := exec.Command("tmux", "has-session", "-t", name).Run() == nil + + if !hasSession { + logrus.Infof("Create new session '%s' %s", name, path) + fmt.Fprintf(CmdOutput, "tmux new-session -ds %s -c %s; ", name, path) + } + + logrus.Infof("Switch to session '%s' %s", name, path) + fmt.Fprintf(CmdOutput, "tmux switch-client -t %s; ", name) + }, + } + return cmd +}