commit 2c7cef5e5aa682f57c8b15e39d26503763bf8048 Author: Patryk Hegenberg Date: Fri Jun 20 12:50:47 2025 +0200 first commit diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..e421aab --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module tmux_wrapper + +go 1.23.4 diff --git a/main.go b/main.go new file mode 100644 index 0000000..06f854f --- /dev/null +++ b/main.go @@ -0,0 +1,80 @@ +package main + +import ( + "bufio" + "fmt" + "os" + "os/exec" + "strings" +) + +const sessionName = "dev-session" + +func main() { + if sessionExists(sessionName) { + handleExistingSession() + } else { + createNewSession() + } +} + +func sessionExists(name string) bool { + cmd := exec.Command("tmux", "has-session", "-t", name) + return cmd.Run() == nil +} + +func handleExistingSession() { + fmt.Printf("A session named '%s' already exists.\n", sessionName) + fmt.Print("Do you want to attach to it (a), kill it and create a new one (n), or cancel (c)? ") + + reader := bufio.NewReader(os.Stdin) + choice, _ := reader.ReadString('\n') + choice = strings.TrimSpace(strings.ToLower(choice)) + + switch choice { + case "a": + attachToSession() + case "n": + killSession() + createNewSession() + default: + fmt.Println("Operation cancelled.") + } +} + +func attachToSession() { + cmd := exec.Command("tmux", "attach-session", "-t", sessionName) + cmd.Stdin = os.Stdin + cmd.Stdout = os.Stdout + cmd.Stderr = os.Stderr + if err := cmd.Run(); err != nil { + fmt.Printf("Error attaching to session: %v\n", err) + } +} + +func killSession() { + cmd := exec.Command("tmux", "kill-session", "-t", sessionName) + if err := cmd.Run(); err != nil { + fmt.Printf("Error killing session: %v\n", err) + } +} + +func createNewSession() { + cmd := exec.Command("tmux", "new-session", "-d", "-s", sessionName) + if err := cmd.Run(); err != nil { + fmt.Printf("Error starting tmux session: %v\n", err) + return + } + + cmd = exec.Command("tmux", "send-keys", "-t", sessionName, "$EDITOR .", "C-m") + if err := cmd.Run(); err != nil { + fmt.Printf("Error opening editor: %v\n", err) + // cmd = exec.Command("tmux", "send-keys", "-t", sessionName, "nvim-lazy .", "C-m") + // if err := cmd.Run(); err != nil { + // fmt.Printf("Error opening LazyVim editor: %v\n", err) + // } + return + } + + attachToSession() +}