Skip to content

Commit

Permalink
add package tmux for executing commands, minor error refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
alexandrebodin committed Jul 6, 2018
1 parent 0da8a4b commit e031ccf
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 47 deletions.
2 changes: 1 addition & 1 deletion __examples__/test.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ select-window = "docker"
# selects the pane to start in.
# must select a window first, otherwise ignored
# first panel is 1 and so on...
select-pane = 6
# select-pane = 6

# option to clear panes after init
# clear-panes=true
Expand Down
18 changes: 8 additions & 10 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/BurntSushi/toml"
"github.com/alecthomas/kingpin"
"github.com/alexandrebodin/go-findup"
"github.com/alexandrebodin/tmuxctl/tmux"
)

var (
Expand All @@ -26,34 +27,34 @@ func main() {

filePath, err := findup.Find(*config)
if err != nil {
log.Fatal(err)
log.Fatalf("Error locating config file %v\n", err)
}

var conf sessionConfig
if _, err := toml.DecodeFile(filePath, &conf); err != nil {
log.Fatalf("Error loading configuration %v\n", err)
}

runningSessions, err := ListSessions()
runningSessions, err := tmux.ListSessions()
if err != nil {
log.Fatal(err)
log.Fatalf("Error listing running sessions %v\n", err)
}

options, err := GetOptions()
options, err := tmux.GetOptions()
if err != nil {
log.Fatal(err)
log.Fatalf("Error getting tmux options %v\n", err)
}

sess := newSession(conf, options)
if _, ok := runningSessions[sess.Name]; ok {
log.Fatalf("Session %s is already running", sess.Name)
log.Fatalf("Session %s is already running\n", sess.Name)
}

checkError := func(err error) {
if err != nil {
log.Println(err)
// kill session if an error occurs after starting it
Exec("kill-session", "-t", sess.Name)
tmux.Exec("kill-session", "-t", sess.Name)
os.Exit(-1)
}
}
Expand All @@ -67,9 +68,6 @@ func main() {

if conf.SelectPane != 0 {
_, err := w.selectPane(conf.SelectPane)
// index := strconv.Itoa(conf.SelectPane + (options.PaneBaseIndex - 1))
// target := fmt.Sprintf("%s:%s.%s", sess.Name, conf.SelectWindow, index)
// _, err := Exec("select-pane", "-t", target)
checkError(err)
}
}
Expand Down
8 changes: 6 additions & 2 deletions pane.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package main

import "strconv"
import (
"strconv"

"github.com/alexandrebodin/tmuxctl/tmux"
)

type pane struct {
Dir string
Expand Down Expand Up @@ -30,6 +34,6 @@ func newPane(win *window, config paneConfig, index int) *pane {
}

func (p *pane) selectPane() error {
_, err := Exec("select-pane", "-t", p.Target)
_, err := tmux.Exec("select-pane", "-t", p.Target)
return err
}
37 changes: 20 additions & 17 deletions session.go
Original file line number Diff line number Diff line change
@@ -1,22 +1,25 @@
package main

import (
"errors"
"fmt"
"os"
"os/exec"
"syscall"

"github.com/alexandrebodin/tmuxctl/tmux"
)

type session struct {
TmuxOptions *Options
TmuxOptions *tmux.Options
Name string
Dir string
Windows []*window
ClearPanes bool
WindowScripts []string
}

func newSession(config sessionConfig, options *Options) *session {
func newSession(config sessionConfig, options *tmux.Options) *session {
sess := &session{
Name: config.Name,
Dir: lookupDir(config.Dir),
Expand All @@ -27,58 +30,58 @@ func newSession(config sessionConfig, options *Options) *session {

for _, winConfig := range config.Windows {
window := newWindow(sess, winConfig)
sess.addWindow(window)
sess.Windows = append(sess.Windows, window)
}

return sess
}

func (sess *session) addWindow(w *window) {
sess.Windows = append(sess.Windows, w)
}

func (sess *session) start() error {
// get term size
width, height, err := getTermSize()
if err != nil {
return err
}

if len(sess.Windows) == 0 {
return errors.New("session has no window")
}

firstWindow := sess.Windows[0]
_, err = Exec("new-session", "-d", "-s", sess.Name, "-c", sess.Dir, "-n", firstWindow.Name, "-x", width, "-y", height)
_, err = tmux.Exec("new-session", "-d", "-s", sess.Name, "-c", sess.Dir, "-n", firstWindow.Name, "-x", width, "-y", height)
if err != nil {
return fmt.Errorf("error starting session %v", err)
return fmt.Errorf("starting session: %v", err)
}

if firstWindow.Dir != sess.Dir {
cdCmd := fmt.Sprintf("cd %s", firstWindow.Dir)
err := SendKeys(sess.Name+":"+firstWindow.Name, cdCmd)
err := tmux.SendKeys(sess.Name+":"+firstWindow.Name, cdCmd)
if err != nil {
return fmt.Errorf("error moving to dir %s, %v", firstWindow.Dir, err)
return fmt.Errorf("moving window to dir %s: %v", firstWindow.Dir, err)
}
}

if len(sess.Windows) > 1 {
for _, win := range sess.Windows[1:] {
err := win.start()
if err != nil {
return fmt.Errorf("Error starting window %v", err)
return fmt.Errorf("starting window %s: %v", win.Name, err)
}
}
}

for _, win := range sess.Windows {

for _, script := range sess.WindowScripts {
err := SendKeys(sess.Name+":"+win.Name, script)
err := tmux.SendKeys(sess.Name+":"+win.Name, script)
if err != nil {
return err
}
}

err := win.init()
if err != nil {
return fmt.Errorf("Error initializing window %v", err)
return fmt.Errorf("initializing window: %v", err)
}
}

Expand All @@ -88,12 +91,12 @@ func (sess *session) start() error {
func (sess *session) attach() error {
tmux, err := exec.LookPath("tmux")
if err != nil {
return fmt.Errorf("Error looking up tmux %v", err)
return fmt.Errorf("looking up tmux: %v", err)
}

args := []string{"tmux", "attach", "-t", sess.Name}
if sysErr := syscall.Exec(tmux, args, os.Environ()); sysErr != nil {
return fmt.Errorf("Error attaching to session %s: %v", sess.Name, sysErr)
return fmt.Errorf("attaching to session: %v", sysErr)
}

return nil
Expand All @@ -106,5 +109,5 @@ func (sess *session) selectWindow(name string) (*window, error) {
}
}

return nil, fmt.Errorf("Window %s not found", name)
return nil, fmt.Errorf("window %s not found", name)
}
2 changes: 1 addition & 1 deletion commands.go → tmux/commands.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package tmux

import (
"bytes"
Expand Down
37 changes: 21 additions & 16 deletions window.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package main
import (
"fmt"
"strings"

"github.com/alexandrebodin/tmuxctl/tmux"
)

type window struct {
Expand Down Expand Up @@ -42,16 +44,18 @@ func newWindow(sess *session, config windowConfig) *window {
}

func (w *window) start() error {
args := []string{"new-window", "-t", w.Sess.Name, "-n", w.Name, "-c", w.Dir}
_, err := Exec(args...)
return err
_, err := tmux.Exec("new-window", "-t", w.Sess.Name, "-n", w.Name, "-c", w.Dir)
if err != nil {
return fmt.Errorf("starting window: %v", err)
}
return nil
}

func (w *window) runScripts() error {
for _, script := range w.Scripts {
err := SendKeys(w.Sess.Name+":"+w.Name, script)
err := tmux.SendKeys(w.Sess.Name+":"+w.Name, script)
if err != nil {
return err
return fmt.Errorf("run window scripts: %v", err)
}
}
return nil
Expand Down Expand Up @@ -85,7 +89,7 @@ func (w *window) init() error {
}

if w.Sync {
_, err := Exec("set-window-option", "-t", w.Target, "synchronize-panes")
_, err := tmux.Exec("set-window-option", "-t", w.Target, "synchronize-panes")
return err
}

Expand All @@ -95,22 +99,22 @@ func (w *window) init() error {
func (w *window) runPaneScripts() error {
for _, pane := range w.Panes {
for _, script := range w.PaneScripts {
err := SendKeys(pane.Target, script)
err := tmux.SendKeys(pane.Target, script)
if err != nil {
return err
}
}

for _, script := range pane.Scripts {
err := SendKeys(pane.Target, script)
err := tmux.SendKeys(pane.Target, script)
if err != nil {
return err
}
}

// clearing panes
if w.Sess.ClearPanes {
err := SendRawKeys(pane.Target, "C-l")
err := tmux.SendRawKeys(pane.Target, "C-l")
if err != nil {
return err
}
Expand All @@ -128,7 +132,7 @@ func (w *window) renderPane() error {

firstPane := w.Panes[0]
if firstPane.Dir != "" && firstPane.Dir != w.Dir { // we need to move the pane
err := SendKeys(firstPane.Target, "cd "+firstPane.Dir)
err := tmux.SendKeys(firstPane.Target, "cd "+firstPane.Dir)
if err != nil {
return err
}
Expand All @@ -140,7 +144,7 @@ func (w *window) renderPane() error {
if pane.Split != "" {
args = append(args, strings.Split(pane.Split, " ")...)
}
_, err := Exec(args...)
_, err := tmux.Exec(args...)
if err != nil {
return err
}
Expand All @@ -151,7 +155,7 @@ func (w *window) renderPane() error {

func (w *window) renderLayout() error {
if w.Layout != "" {
_, err := Exec("select-layout", "-t", w.Target, w.Layout)
_, err := tmux.Exec("select-layout", "-t", w.Target, w.Layout)
return err
}

Expand All @@ -161,26 +165,27 @@ func (w *window) renderLayout() error {
func (w *window) zoomPanes() error {
for _, pane := range w.Panes {
if pane.Zoom {
_, err := Exec("resize-pane", "-t", pane.Target, "-Z")
_, err := tmux.Exec("resize-pane", "-t", pane.Target, "-Z")
if err != nil {
return err
}

return nil // stop after first pane zoomed
// stop after first pane zoomed
return nil
}
}

return nil
}

func (w *window) selectWindow() error {
_, err := Exec("select-window", "-t", w.Target)
_, err := tmux.Exec("select-window", "-t", w.Target)
return err
}

func (w *window) selectPane(index int) (*pane, error) {
if index > len(w.Panes) {
return nil, fmt.Errorf("Pane %d not found", index)
return nil, fmt.Errorf("pane %d not found", index)
}

p := w.Panes[index-1]
Expand Down

0 comments on commit e031ccf

Please sign in to comment.