Skip to content

Instantly share code, notes, and snippets.

@CoderPuppy
Last active December 24, 2019 19:52
Show Gist options
  • Select an option

  • Save CoderPuppy/09f711b6dbec9a09fcf1 to your computer and use it in GitHub Desktop.

Select an option

Save CoderPuppy/09f711b6dbec9a09fcf1 to your computer and use it in GitHub Desktop.
Utility for tmux
package main
import "os"
import "os/exec"
import "fmt"
import "flag"
import "strings"
import "syscall"
import "path/filepath"
import "github.com/keegancsmith/shell"
const app = "/usr/bin/tmux"
const debug = false
var flags = flag.NewFlagSet("t", flag.ContinueOnError)
func printUsage() {
fmt.Println(
`SYNOPSIS
t rename-window <new-name>
t rename-session <new-name>
t join-session [session]
t kill-window [window]
t kill-session [session...]
t list-session
t new-window [-a] [prog...]
t link-window <src> [dst]
t unlink-window [window]
t switch-session [-k] <session>`)
}
func main() {
flags.Parse(os.Args[1:])
if debug {
fmt.Println("args:", flags.Args())
}
command := flags.Arg(0)
if debug {
fmt.Println("command:", command)
}
switch command {
case "h":
fallthrough
case "help":
printUsage()
case "rw":
fallthrough
case "rename-window":
kexec(app, "rename-window", strings.Join(flags.Args()[1:], " "))
case "r":
fallthrough
case "rs":
fallthrough
case "rename":
fallthrough
case "rename-session":
kexec(app, "rename-session", strings.Join(flags.Args()[1:], " "))
case "j":
fallthrough
case "join":
fallthrough
case "join-session":
sessionName := flags.Arg(1)
if len(sessionName) == 0 {
wd, err := os.Getwd()
if err == nil {
sessionName = filepath.Base(wd)
} else {
fmt.Printf("os.Getwd: %s\n", err)
}
}
cmd := exec.Command(app, "has-session", "-t", sessionName)
out, err := cmd.CombinedOutput()
_ = out
if err != nil {
// if string(out[0:19]) == "session not found: " {
cmd := exec.Command(app, "new-session", "-d", "-s", sessionName)
out, err := cmd.CombinedOutput()
if err != nil {
fmt.Println("new-session failed")
fmt.Printf("Output: %s\n", out)
fmt.Printf("Error: %s\n", err)
os.Exit(1)
}
// } else {
// fmt.Println("has-session failed")
// fmt.Printf("Output: %s\n", out)
// fmt.Printf("Error: %s\n", err)
// os.Exit(1)
// }
}
kexec(app, "attach-session", "-t", sessionName)
case "k":
fallthrough
case "kill-window":
args := []string{"kill-window"}
if len(flags.Arg(1)) > 0 {
args = append(args, "-t", flags.Arg(1))
}
kexec(app, args...)
case "ks":
fallthrough
case "kill-session":
if len(flags.Args()) > 1 {
for _, name := range flags.Args()[1:] {
cmd := exec.Command(app, "kill-session", "-t", name)
out, err := cmd.CombinedOutput()
if err != nil {
fmt.Printf("new-session failed on %s\n", name)
fmt.Printf("Output: %s\n", out)
fmt.Printf("Error: %s\n", err)
os.Exit(1)
}
}
} else {
kexec(app, "kill-session")
}
case "l":
fallthrough
case "ls":
fallthrough
case "list-session":
kexec(app, "list-sessions")
case "w":
fallthrough
case "window":
fallthrough
case "new-window":
args := []string{"new-window"}
lflags := flag.NewFlagSet("new-window", flag.ContinueOnError)
active := lflags.Bool("a", false, "Activate the new window")
lflags.Parse(flags.Args()[1:])
if !(*active) {
args = append(args, "-d")
}
if len(lflags.Args()) == 0 {
args = append(args, "zsh")
} else {
args = append(args, shell.Sprintf("zsh -c %s", strings.Join(lflags.Args(), " ")))
}
kexec(app, args...)
case "d":
fallthrough
case "dt":
fallthrough
case "detach":
kexec(app, "detach")
case "ln":
fallthrough
case "link":
fallthrough
case "link-window":
args := []string{"link-window", "-s", flags.Arg(1)}
if len(flags.Arg(2)) > 0 {
args = append(args, "-t", flags.Arg(2))
}
kexec(app, args...)
case "unln":
fallthrough
case "unlink":
fallthrough
case "unlink-window":
args := []string{"unlink-window"}
if len(flags.Arg(1)) > 0 {
args = append(args, "-t", flags.Arg(1))
}
kexec(app, args...)
case "s":
fallthrough
case "switch":
fallthrough
case "switch-session":
lflags := flag.NewFlagSet("switch-session", flag.ContinueOnError)
kill := lflags.Bool("k", false, "Kill the current session")
lflags.Parse(flags.Args()[1:])
cmd := exec.Command(app, "display-message", "-p", "#S")
out, err := cmd.CombinedOutput()
if err != nil {
fmt.Println("display-message failed")
fmt.Printf("Output: %s\n", out)
fmt.Printf("Error: %s\n", err)
os.Exit(1)
}
currentSession := strings.TrimSuffix(string(out[:]), "\n")
sessionName := lflags.Arg(0)
cmd = exec.Command(app, "switch-client", "-t", sessionName)
out, err = cmd.CombinedOutput()
if err != nil {
fmt.Println("switch-client failed")
fmt.Printf("Output: %s\n", out)
fmt.Printf("Error: %s\n", err)
os.Exit(1)
}
if *kill {
cmd := exec.Command(app, "kill-session", "-t", currentSession)
out, err := cmd.CombinedOutput()
if err != nil {
fmt.Println("kill-session failed")
fmt.Printf("Output: %s\n", out)
fmt.Printf("Error: %s\n", err)
}
}
default:
printUsage()
}
}
func kexec(cmd string, arg ...string) {
syscall.Exec(cmd, append([]string{cmd}, arg...), os.Environ())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment