Skip to content

Commit

Permalink
feat: Add tmux session switcher
Browse files Browse the repository at this point in the history
  • Loading branch information
NoUseFreak committed Jan 8, 2024
1 parent d034921 commit f823a79
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 27 deletions.
61 changes: 34 additions & 27 deletions internal/pkg/command/go.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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
}
1 change: 1 addition & 0 deletions internal/pkg/command/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ func Execute() {
rootCmd.AddCommand(getVersionCmd())
rootCmd.AddCommand(getOrgCmd())
rootCmd.AddCommand(getWDIDCmd())
rootCmd.AddCommand(getTmuxCmd())

cobra.OnInitialize(config.InitConfig)

Expand Down
54 changes: 54 additions & 0 deletions internal/pkg/command/tmux.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit f823a79

Please sign in to comment.