Skip to content

Commit

Permalink
tmux: added ACtionSessions and ActionWindows
Browse files Browse the repository at this point in the history
  • Loading branch information
rsteube committed May 9, 2022
1 parent 0424efe commit 0a40da5
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 3 deletions.
5 changes: 5 additions & 0 deletions completers/tmux_completer/cmd/killSession_generated.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cmd

import (
"github.com/rsteube/carapace"
"github.com/rsteube/carapace-bin/pkg/actions/tools/tmux"
"github.com/spf13/cobra"
)

Expand All @@ -16,4 +17,8 @@ func init() {

killSessionCmd.Flags().StringS("t", "t", "", "target-session")
rootCmd.AddCommand(killSessionCmd)

carapace.Gen(killSessionCmd).FlagCompletion(carapace.ActionMap{
"t": tmux.ActionWindows(),
})
}
5 changes: 5 additions & 0 deletions completers/tmux_completer/cmd/killWindow_generated.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cmd

import (
"github.com/rsteube/carapace"
"github.com/rsteube/carapace-bin/pkg/actions/tools/tmux"
"github.com/spf13/cobra"
)

Expand All @@ -17,4 +18,8 @@ func init() {
killWindowCmd.Flags().BoolS("a", "a", false, "TODO description")
killWindowCmd.Flags().StringS("t", "t", "", "target-window")
rootCmd.AddCommand(killWindowCmd)

carapace.Gen(killWindowCmd).FlagCompletion(carapace.ActionMap{
"t": tmux.ActionWindows(),
})
}
4 changes: 2 additions & 2 deletions completers/tmux_completer/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package cmd

import (
"github.com/rsteube/carapace"
"github.com/rsteube/carapace-bin/completers/tmux_completer/cmd/action"
"github.com/rsteube/carapace-bin/pkg/actions/tools/tmux"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -36,7 +36,7 @@ func init() {
carapace.Gen(rootCmd).FlagCompletion(carapace.ActionMap{
"S": carapace.ActionFiles(),
"T": carapace.ActionMultiParts(",", func(c carapace.Context) carapace.Action {
return action.ActionFeatures().Invoke(c).Filter(c.Parts).ToA()
return tmux.ActionFeatures().Invoke(c).Filter(c.Parts).ToA()
}),
"c": carapace.ActionFiles(),
"f": carapace.ActionFiles(),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package action
package tmux

import "github.com/rsteube/carapace"

// ActionFeatures completes features
// `256 (Supports 256 colours with the SGR escape sequences.)
// `RGB (Supports RGB colour with the SGR escape sequences.)
func ActionFeatures() carapace.Action {
return carapace.ActionValuesDescribed(
"256", "Supports 256 colours with the SGR escape sequences.",
Expand Down
24 changes: 24 additions & 0 deletions pkg/actions/tools/tmux/session.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package tmux

import (
"strings"

"github.com/rsteube/carapace"
)

// ActionSessions completes sessions
// 0 (elvish- (1 panes) [106x28] [layout c97e,106x28,0,0,1] @1)
// 1 (elvish* (1 panes) [106x28] [layout c97f,106x28,0,0,2] @2 (active))
func ActionSessions() carapace.Action {
return carapace.ActionExecCommand("tmux", "list-sessions")(func(output []byte) carapace.Action {
lines := strings.Split(string(output), "\n")

vals := make([]string, 0)
for _, line := range lines {
if splitted := strings.SplitN(line, ": ", 2); len(splitted) == 2 {
vals = append(vals, splitted...)
}
}
return carapace.ActionValuesDescribed(vals...)
})
}
2 changes: 2 additions & 0 deletions pkg/actions/tools/tmux/tmux.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// package tmux contains tmux related actions
package tmux
24 changes: 24 additions & 0 deletions pkg/actions/tools/tmux/window.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package tmux

import (
"strings"

"github.com/rsteube/carapace"
)

// ActionWindows completes windows
// 0 (elvish- (1 panes) [106x28] [layout c97e,106x28,0,0,1] @1)
// 1 (elvish* (1 panes) [106x28] [layout c97f,106x28,0,0,2] @2 (active))
func ActionWindows() carapace.Action {
return carapace.ActionExecCommand("tmux", "list-windows")(func(output []byte) carapace.Action {
lines := strings.Split(string(output), "\n")

vals := make([]string, 0)
for _, line := range lines {
if splitted := strings.SplitN(line, ": ", 2); len(splitted) == 2 {
vals = append(vals, splitted...)
}
}
return carapace.ActionValuesDescribed(vals...)
})
}

0 comments on commit 0a40da5

Please sign in to comment.