Skip to content

Commit

Permalink
Add Target field on window and pane to refactor select-window / selec…
Browse files Browse the repository at this point in the history
…t-pane
  • Loading branch information
alexandrebodin committed Jul 5, 2018
1 parent 3b1fe24 commit 0da8a4b
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 35 deletions.
4 changes: 2 additions & 2 deletions __examples__/test.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ 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 = 3
select-pane = 6

# option to clear panes after init
clear-panes=true
# clear-panes=true

# run scripts just after window is initialised
# and before panes are created
Expand Down
37 changes: 22 additions & 15 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package main
import (
"fmt"
"log"
"strconv"
"os"

"github.com/BurntSushi/toml"
"github.com/alecthomas/kingpin"
Expand Down Expand Up @@ -35,38 +35,45 @@ func main() {
}

runningSessions, err := ListSessions()
checkError(err)
if err != nil {
log.Fatal(err)
}

options, err := GetOptions()
checkError(err)

sess := newSession(conf)
sess.TmuxOptions = options
if err != nil {
log.Fatal(err)
}

sess := newSession(conf, options)
if _, ok := runningSessions[sess.Name]; ok {
log.Fatalf("Session %s is already running", 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)
os.Exit(-1)
}
}

err = sess.start()
checkError(err)

if conf.SelectWindow != "" {
_, err := Exec("select-window", "-t", sess.Name+":"+conf.SelectWindow)
w, err := sess.selectWindow(conf.SelectWindow)
checkError(err)

if conf.SelectPane != 0 {
index := strconv.Itoa(conf.SelectPane + (options.PaneBaseIndex - 1))
_, err := Exec("select-pane", "-t", sess.Name+":"+conf.SelectWindow+"."+index)
_, 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)
}
}

err = sess.attach()
checkError(err)
}

func checkError(err error) {
if err != nil {
log.Fatal(err)
}
}
12 changes: 11 additions & 1 deletion pane.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,24 @@
package main

import "strconv"

type pane struct {
Dir string
Zoom bool
Split string
Scripts []string
Window *window
Target string
}

func newPane(win *window, config paneConfig) *pane {
func newPane(win *window, config paneConfig, index int) *pane {
normalizedIndex := strconv.Itoa(index + win.Sess.TmuxOptions.PaneBaseIndex)
pane := &pane{
Window: win,
Zoom: config.Zoom,
Split: config.Split,
Scripts: config.Scripts,
Target: win.Target + "." + normalizedIndex,
}

if config.Dir != "" {
Expand All @@ -23,3 +28,8 @@ func newPane(win *window, config paneConfig) *pane {
}
return pane
}

func (p *pane) selectPane() error {
_, err := Exec("select-pane", "-t", p.Target)
return err
}
13 changes: 12 additions & 1 deletion session.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,13 @@ type session struct {
WindowScripts []string
}

func newSession(config sessionConfig) *session {
func newSession(config sessionConfig, options *Options) *session {
sess := &session{
Name: config.Name,
Dir: lookupDir(config.Dir),
ClearPanes: config.ClearPanes,
WindowScripts: config.WindowScripts,
TmuxOptions: options,
}

for _, winConfig := range config.Windows {
Expand Down Expand Up @@ -97,3 +98,13 @@ func (sess *session) attach() error {

return nil
}

func (sess *session) selectWindow(name string) (*window, error) {
for _, w := range sess.Windows {
if w.Name == name {
return w, w.selectWindow()
}
}

return nil, fmt.Errorf("Window %s not found", name)
}
45 changes: 29 additions & 16 deletions window.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package main

import (
"strconv"
"fmt"
"strings"
)

Expand All @@ -14,6 +14,7 @@ type window struct {
Scripts []string
Panes []*pane
PaneScripts []string
Target string
}

func newWindow(sess *session, config windowConfig) *window {
Expand All @@ -24,6 +25,7 @@ func newWindow(sess *session, config windowConfig) *window {
Sync: config.Sync,
Scripts: config.Scripts,
PaneScripts: config.PaneScripts,
Target: sess.Name + ":" + config.Name,
}

if config.Dir != "" {
Expand All @@ -32,8 +34,8 @@ func newWindow(sess *session, config windowConfig) *window {
win.Dir = sess.Dir
}

for _, paneConfig := range config.Panes {
win.Panes = append(win.Panes, newPane(win, paneConfig))
for idx, paneConfig := range config.Panes {
win.Panes = append(win.Panes, newPane(win, paneConfig, idx))
}

return win
Expand Down Expand Up @@ -83,34 +85,32 @@ func (w *window) init() error {
}

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

return nil
}

func (w *window) runPaneScripts() error {
for idx, pane := range w.Panes {
paneTarget := w.Sess.Name + ":" + w.Name + "." + strconv.Itoa(idx+w.Sess.TmuxOptions.PaneBaseIndex)

for _, pane := range w.Panes {
for _, script := range w.PaneScripts {
err := SendKeys(paneTarget, script)
err := SendKeys(pane.Target, script)
if err != nil {
return err
}
}

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

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

firstPane := w.Panes[0]
if firstPane.Dir != "" && firstPane.Dir != w.Dir { // we need to move the pane
err := SendKeys(w.Sess.Name+":"+w.Name+"."+strconv.Itoa(w.Sess.TmuxOptions.PaneBaseIndex), "cd "+firstPane.Dir)
err := SendKeys(firstPane.Target, "cd "+firstPane.Dir)
if err != nil {
return err
}
}

for _, pane := range w.Panes[1:] {
args := []string{"split-window", "-t", w.Sess.Name + ":" + w.Name, "-c", pane.Dir}
args := []string{"split-window", "-t", w.Target, "-c", pane.Dir}

if pane.Split != "" {
args = append(args, strings.Split(pane.Split, " ")...)
Expand All @@ -151,18 +151,17 @@ func (w *window) renderPane() error {

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

return nil
}

func (w *window) zoomPanes() error {
for idx, pane := range w.Panes {
for _, pane := range w.Panes {
if pane.Zoom {
index := strconv.Itoa(idx + w.Sess.TmuxOptions.PaneBaseIndex)
_, err := Exec("resize-pane", "-t", w.Sess.Name+":"+w.Name+"."+index, "-Z")
_, err := Exec("resize-pane", "-t", pane.Target, "-Z")
if err != nil {
return err
}
Expand All @@ -173,3 +172,17 @@ func (w *window) zoomPanes() error {

return nil
}

func (w *window) selectWindow() error {
_, err := 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)
}

p := w.Panes[index-1]
return p, p.selectPane()
}

0 comments on commit 0da8a4b

Please sign in to comment.