Skip to content

Commit

Permalink
fix/chore: up/down replaced with top/bottom + focus-pane exits with 1
Browse files Browse the repository at this point in the history
Top/bottom matches the tmux verbiage more

focus-pane now exits with 1 and doesn't allow wrapping
Handle failures like:
run-shell "~/.local/bin/tmux-tools focus-pane right || exit 0"
  • Loading branch information
distek committed Mar 6, 2024
1 parent 3e25ced commit 9eff4e6
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 56 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Some tools/wrappers around tmux to make my life easier

Pane movement. Not perfect but does it well enough for my use. Will probably improve upon it at some point.

`tmux-tools wm [up|down|left|right]`
`tmux-tools wm [top|bottom|left|right]`

TODO:

Expand Down Expand Up @@ -95,7 +95,7 @@ Kills all non-`(attached)` sessions on `-S` socket

Focus pane in a given direction:

`tmux-tools focus-pane {left | down | up | right}`
`tmux-tools focus-pane {left | bottom | top | right}`

## TODO

Expand Down
36 changes: 8 additions & 28 deletions cmd/misc.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package cmd

import (
"fmt"
"log"
"os"
"strings"
Expand Down Expand Up @@ -43,15 +42,15 @@ var cleanCmd = &cobra.Command{
}

var focusPaneCmd = &cobra.Command{
Use: "focus-pane {left | down | up | right}",
Short: "Focus pane in a given direction (left, down, up, right)",
ValidArgs: []string{"left", "down", "up", "right"},
Use: "focus-pane {left | bottom | top | bottom}",
Short: "Focus pane in a given direction (left, bottom, top, right) (doesn't wrap)",
ValidArgs: []string{"left", "bottom", "top", "right"},
Args: cobra.MatchAll(cobra.ExactArgs(1), cobra.OnlyValidArgs),
Run: func(cmd *cobra.Command, args []string) {
dir := args[0]

switch dir {
case "left", "down", "up", "right":
case "left", "right", "top", "bottom":
default:
log.Printf("Provide only one of %v", cmd.ValidArgs)
_ = cmd.Usage()
Expand All @@ -63,35 +62,16 @@ var focusPaneCmd = &cobra.Command{
log.Fatal(err)
}

dstP, exists, err := lib.GetPaneInDir(p, dir)
neighbors, err := lib.GetNeighbors(p)
if err != nil {
log.Fatal(err)
}

if !exists {
var vimDir string
switch dir {
case "left":
vimDir = "h"
case "down":
vimDir = "j"
case "up":
vimDir = "k"
case "right":
vimDir = "l"
}

_, _, err := lib.Tmux(lib.GlobalArgs, "send-keys", map[string]string{
fmt.Sprintf("M-%s", vimDir): "",
}, "")
if err != nil {
log.Fatal(err)
}

return
if !neighbors.Panes[dir].Exists {
log.Fatalf("no pane in dir %s", dir)
}

err = lib.FocusPane(dstP)
err = lib.FocusPane(neighbors.Panes[dir].Pane)
if err != nil {
log.Fatal(err)
}
Expand Down
36 changes: 18 additions & 18 deletions cmd/wm.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import (
)

var wmCmd = &cobra.Command{
Use: "wm {left | down | up | right}",
Use: "wm {left | bottom | top | right}",
Short: "Window manager",
ValidArgs: []string{"left", "down", "up", "right"},
ValidArgs: []string{"left", "bottom", "top", "right"},
Args: cobra.MatchAll(cobra.ExactArgs(1), cobra.OnlyValidArgs),
Run: func(cmd *cobra.Command, args []string) {
if len(args) != 1 {
Expand Down Expand Up @@ -52,10 +52,10 @@ func splitFull(pane lib.Pane, dir string) {
extraArgs := make(map[string]string, 2)

switch dir {
case "up":
case "top":
extraArgs["-b"] = ""
extraArgs["-v"] = ""
case "down":
case "bottom":
extraArgs["-v"] = ""
case "left":
extraArgs["-b"] = ""
Expand Down Expand Up @@ -100,10 +100,10 @@ func splitHalf(dst, src lib.Pane, dir string) {

extraArgs := make(map[string]string, 2)
switch dir {
case "up":
case "top":
extraArgs["-b"] = ""
extraArgs["-v"] = ""
case "down":
case "bottom":
extraArgs["-v"] = ""
case "left":
extraArgs["-b"] = ""
Expand Down Expand Up @@ -140,35 +140,35 @@ func moveWindowInDir(dir string) {

// favor splitting to the right or bottom when merging panes
switch dir {
case "up":
if neighbors.Panes["up"].Exists {
splitHalf(neighbors.Panes["up"].Pane, currPane, "right")
case "top":
if neighbors.Panes["top"].Exists {
splitHalf(neighbors.Panes["top"].Pane, currPane, "right")
} else {
if panesLen == 2 || neighbors.Panes["left"].Exists || neighbors.Panes["right"].Exists {
splitFull(currPane, "up")
splitFull(currPane, "top")
}
}
case "down":
if neighbors.Panes["down"].Exists {
splitHalf(neighbors.Panes["down"].Pane, currPane, "right")
case "bottom":
if neighbors.Panes["bottom"].Exists {
splitHalf(neighbors.Panes["bottom"].Pane, currPane, "right")
} else {
if panesLen == 2 || neighbors.Panes["left"].Exists || neighbors.Panes["right"].Exists {
splitFull(currPane, "down")
splitFull(currPane, "bottom")
}
}
case "left":
if neighbors.Panes["left"].Exists {
splitHalf(neighbors.Panes["left"].Pane, currPane, "down")
splitHalf(neighbors.Panes["left"].Pane, currPane, "bottom")
} else {
if panesLen == 2 || neighbors.Panes["up"].Exists || neighbors.Panes["down"].Exists {
if panesLen == 2 || neighbors.Panes["top"].Exists || neighbors.Panes["bottom"].Exists {
splitFull(currPane, "left")
}
}
case "right":
if neighbors.Panes["right"].Exists {
splitHalf(neighbors.Panes["right"].Pane, currPane, "down")
splitHalf(neighbors.Panes["right"].Pane, currPane, "bottom")
} else {
if panesLen == 2 || neighbors.Panes["up"].Exists || neighbors.Panes["down"].Exists {
if panesLen == 2 || neighbors.Panes["top"].Exists || neighbors.Panes["bottom"].Exists {
splitFull(currPane, "right")
}
}
Expand Down
39 changes: 31 additions & 8 deletions lib/pane.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,8 @@ func getNeighborDirs(pane Pane) map[string]bool {

ret["left"] = split[0] == "0"
ret["right"] = split[1] == "0"
ret["up"] = split[2] == "0"
ret["down"] = split[3] == "0"
ret["top"] = split[2] == "0"
ret["bottom"] = split[3] == "0"

return ret
}
Expand All @@ -138,8 +138,31 @@ func GetPaneInDir(pane Pane, dir string) (Pane, bool, error) {
}

o, e, err := Tmux(GlobalArgs, "display-message", map[string]string{
"-p": fmt.Sprintf("\"#{pane_at_%s}\"", dir),
}, "")
if err != nil {
log.Println(e)
return Pane{}, false, fmt.Errorf("lib: GetPaneInDir: Tmux: command failed: %s", err)
}

if o == "1" {
return Pane{}, false, nil
}

var ofDir string

switch dir {
case "left", "right":
ofDir = dir
case "bottom":
ofDir = "down"
case "top":
ofDir = "up"
}

o, e, err = Tmux(GlobalArgs, "display-message", map[string]string{
"-p": "",
"-t": fmt.Sprintf("{%s-of}", dir),
"-t": fmt.Sprintf("{%s-of}", ofDir),
"-F": paneFmtLine,
}, "")
if err != nil {
Expand Down Expand Up @@ -212,8 +235,8 @@ var dirArgs = map[string][]string{
"bottom-right": {"\"#{pane_at_bottom},#{pane_at_right}\"", "11"},
"left": {"\"#{pane_at_left}\"", "1"},
"right": {"\"#{pane_at_right}\"", "1"},
"up": {"\"#{pane_at_top}\"", "1"},
"down": {"\"#{pane_at_bottom}\"", "1"},
"top": {"\"#{pane_at_top}\"", "1"},
"bottom": {"\"#{pane_at_bottom}\"", "1"},
}

// GetFurthestPaneInDir - valid args are:
Expand All @@ -223,8 +246,8 @@ var dirArgs = map[string][]string{
// bottom-right,
// left,
// right,
// up,
// and down
// top,
// and bottom
func GetFurthestPaneInDir(dir string) (Pane, error) {
panes, err := GetPanes()
if err != nil {
Expand Down Expand Up @@ -265,7 +288,7 @@ type Neighbors struct {
Panes map[string]Neighbor
}

// Get neighboring panes ("left", "down", "up", "right")
// Get neighboring panes ("left", "bottom", "top", "right")
func GetNeighbors(pane Pane) (Neighbors, error) {
ret := Neighbors{}

Expand Down

0 comments on commit 9eff4e6

Please sign in to comment.