Skip to content

Commit

Permalink
support for extra tmux argument
Browse files Browse the repository at this point in the history
  • Loading branch information
fzerorubigd committed Nov 8, 2014
1 parent 201fffd commit 154690d
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 22 deletions.
12 changes: 10 additions & 2 deletions tmass.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
func main() {
var (
tmuxCmd string
tmuxArgs string
forceNew bool
layoutDir string
rename bool
Expand Down Expand Up @@ -62,7 +63,7 @@ use --forcenew to overwrite this`,

sess.ForceNew = forceNew

if err := tmux.BuildSession(sess, tmuxCmd, rename); err != nil {
if err := tmux.BuildSession(sess, tmuxCmd,strings.Split(tmuxArgs, " "), rename); err != nil {
log.Fatal(err)
}
log.Print(colorstring.Color("[green]Session has been loaded"))
Expand Down Expand Up @@ -107,7 +108,7 @@ use --forcenew to overwrite this`,
log.Fatalf("file already exists: %s", filename)
}

s, err := tmux.LoadSessionFromTmux(tmuxCmd, layout)
s, err := tmux.LoadSessionFromTmux(tmuxCmd,strings.Split(tmuxArgs, " "), layout)
if err != nil {
log.Fatal(err)
}
Expand Down Expand Up @@ -138,6 +139,13 @@ use --forcenew to overwrite this`,
`The tmux command to use, just if tmux is not in the $PATH`,
)

root.PersistentFlags().StringVar(
&tmuxArgs,
"tmux-args",
"",
`Extra arguments to use with tmux`,
)

root.PersistentFlags().StringVarP(
&layoutDir,
"layout-dir",
Expand Down
39 changes: 20 additions & 19 deletions tmux/tmux.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,13 @@ func (m *Command) String() string {
return strings.Join(m.Parts, " ")
}

func (m *Command) Execute(base string) (string, error) {
func (m *Command) Execute(base string, args []string) (string, error) {
//fmt.Println(m.String())
out, err := exec.Command(base, m.Parts...).Output()
args = append(args, m.Parts...)
out, err := exec.Command(base, args...).Output()

if err != nil {
err = fmt.Errorf("failed to execute %s %s : %s \n %s", base, m.String(), err.Error(), string(out))
err = fmt.Errorf("failed to execute %s %s : %s \n %s", base, strings.Join(args, " "), err.Error(), string(out))
}
return strings.TrimSpace(string(out)), err
}
Expand All @@ -76,7 +77,7 @@ func (m *Command) Clear() {
m.Parts = nil
}

func BuildSession(s *Session, tmux string, rename bool) error {
func BuildSession(s *Session, tmux string, args []string, rename bool) error {
if s.Name == "" {
s.Name = "tmass-session-" + strconv.Itoa(rand.Int())
}
Expand All @@ -101,7 +102,7 @@ func BuildSession(s *Session, tmux string, rename bool) error {
for i := range s.Windows {
if s.ForceNew && i == 0 { // First window is created when new session is started
cmd.Add("-n", s.Windows[i].Name, "-c", s.Windows[i].RealPane[0].Root)
if _, err := cmd.Execute(tmux); err != nil {
if _, err := cmd.Execute(tmux, args); err != nil {
return err
}
// tmux -P switch for new window return data for window not the first pane
Expand All @@ -110,30 +111,30 @@ func BuildSession(s *Session, tmux string, rename bool) error {
} else {
// If this is a rename session Command
if i == 0 {
if _, err := cmd.Execute(tmux); err != nil {
if _, err := cmd.Execute(tmux, args); err != nil {
return err
}
}
c := Command{}
c.Add("new-window", "-P", "-t", s.Name, "-n", s.Windows[i].Name, "-c", s.Windows[i].RealPane[0].Root)
if n, err := c.Execute(tmux); err != nil {
if n, err := c.Execute(tmux, args); err != nil {
return err
} else {
s.Windows[i].RealPane[0].identifier = n
}
}
cf, err := BuildPane(&s.Windows[i], tmux, s)
cf, err := BuildPane(&s.Windows[i], tmux, args, s)
if err != nil {
return err
}
// The problem is, layout may contain the focus. so for setting focus, we need to call it after setting layout
c := Command{[]string{"select-layout", s.Windows[i].Layout}}
if _, err := c.Execute(tmux); err != nil {
if _, err := c.Execute(tmux, args); err != nil {
return err
}

if cf != nil {
if _, err := cf.Execute(tmux); err != nil {
if _, err := cf.Execute(tmux, args); err != nil {
return err
}
}
Expand All @@ -142,13 +143,13 @@ func BuildSession(s *Session, tmux string, rename bool) error {
return nil
}

func BuildPane(w *Window, tmux string, s *Session) (*Command, error) {
func BuildPane(w *Window, tmux string, args []string, s *Session) (*Command, error) {

cf := Command{}
for i, p := range w.RealPane {
if i > 0 { // The first pane is created when the window is created
c0 := Command{[]string{"split-window", "-P", "-c", p.Root}}
if n, err := c0.Execute(tmux); err != nil {
if n, err := c0.Execute(tmux, args); err != nil {
return nil, err
} else {
p.identifier = n
Expand All @@ -157,10 +158,10 @@ func BuildPane(w *Window, tmux string, s *Session) (*Command, error) {
}
c1 := Command{[]string{"send-keys", "-t", p.identifier, strings.Join(p.Commands, ";")}}
c2 := Command{[]string{"send-keys", "-t", p.identifier, "Enter"}}
if _, err := c1.Execute(tmux); err != nil {
if _, err := c1.Execute(tmux, args); err != nil {
return nil, err
}
if _, err := c2.Execute(tmux); err != nil {
if _, err := c2.Execute(tmux, args); err != nil {
return nil, err
}
if p.Focus {
Expand All @@ -175,12 +176,12 @@ func BuildPane(w *Window, tmux string, s *Session) (*Command, error) {
return nil, nil
}

func LoadSessionFromTmux(tmux, session string) (*Session, error) {
func LoadSessionFromTmux(tmux string, args []string, session string) (*Session, error) {
sess := Session{Name: session}
sess.Windows = make([]Window, 0)
cmd := Command{}
cmd.Add("list-window", "-t", session, "-F", "#S:#I|#{window_name}|#{window_layout}")
if out, err := cmd.Execute(tmux); err != nil {
if out, err := cmd.Execute(tmux, args); err != nil {
return nil, err
} else {
for _, s := range strings.Split(out, "\n") {
Expand All @@ -190,7 +191,7 @@ func LoadSessionFromTmux(tmux, session string) (*Session, error) {
continue
}

if w, err := LoadWindowFromTmux(tmux, parts[0], parts[1], parts[2]); err != nil {
if w, err := LoadWindowFromTmux(tmux, args, parts[0], parts[1], parts[2]); err != nil {
return nil, err
} else {
sess.Windows = append(sess.Windows, *w)
Expand All @@ -202,12 +203,12 @@ func LoadSessionFromTmux(tmux, session string) (*Session, error) {

}

func LoadWindowFromTmux(tmux, window, name, layout string) (*Window, error) {
func LoadWindowFromTmux(tmux string, args []string, window, name, layout string) (*Window, error) {
// The real pane is not used here. ignore it
w := Window{Name: name, Layout: layout, Panes: make([]interface{}, 0)}
cmd := Command{}
cmd.Add("list-pane", "-t", window, "-F", "#{pane_current_path}|#{pane_current_command}|#{pane_active}")
if out, err := cmd.Execute(tmux); err != nil {
if out, err := cmd.Execute(tmux, args); err != nil {
return nil, err
} else {
for _, s := range strings.Split(out, "\n") {
Expand Down
1 change: 1 addition & 0 deletions tools/bash_tmass
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ _tmass()
local first=${COMP_WORDS[1]}

local sec; sec='--tmux - Tmux executable if its not in PATH or name is different from tmux
--tmux-args - extra arguments to tmux
--layout-dir - layout directory to load/save from/to'
case "$first" in
save)
Expand Down
3 changes: 2 additions & 1 deletion tools/zsh_tmass
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ case "$state" in
;;
(args)
local sec; sec=(
'--tmux=:Tmux executable if its not in PATH or name is different from tmux'
'--tmux=: Tmux executable if its not in PATH or name is different from tmux'
'--tmux-args=: extra argument for tmux"
'--layout-dir=: layout directory to load/save from/to'
)

Expand Down

0 comments on commit 154690d

Please sign in to comment.