mirror of
https://github.com/jimeh/go-tmux.git
synced 2026-02-19 12:56:45 +00:00
Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
048455a6bc
|
3
options.go
Normal file
3
options.go
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
package tmux
|
||||||
|
|
||||||
|
type Options map[string]string
|
||||||
@@ -1,21 +1,21 @@
|
|||||||
package tmux
|
package tmux
|
||||||
|
|
||||||
// OptionsScope represents one of the five scopes that Tmux holds options
|
// Scope represents one of the five scopes that Tmux holds options
|
||||||
// within.
|
// within.
|
||||||
type OptionsScope int
|
type Scope int
|
||||||
|
|
||||||
const (
|
const (
|
||||||
Server OptionsScope = iota + 1
|
Server Scope = iota + 1
|
||||||
GlobalSession
|
GlobalSession
|
||||||
Session
|
Session
|
||||||
GlobalWindow
|
GlobalWindow
|
||||||
Window
|
Window
|
||||||
)
|
)
|
||||||
|
|
||||||
// OptionsScopeFlags converts a given OptionsScope to the command line flags
|
// ScopeToFlags converts a given OptionsScope to the command line flags
|
||||||
// needed to restrict "set-option" and "show-options" commands to the scope in
|
// needed to restrict "set-option" and "show-options" commands to the scope in
|
||||||
// question.
|
// question.
|
||||||
func OptionsScopeFlags(scope OptionsScope) string {
|
func ScopeToFlags(scope Scope) string {
|
||||||
switch scope {
|
switch scope {
|
||||||
case 0, Session:
|
case 0, Session:
|
||||||
return ""
|
return ""
|
||||||
@@ -6,9 +6,9 @@ import (
|
|||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestOptionsScopeFlags(t *testing.T) {
|
func TestScopeToFlags(t *testing.T) {
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
scope OptionsScope
|
scope Scope
|
||||||
flags string
|
flags string
|
||||||
}{
|
}{
|
||||||
{0, ""},
|
{0, ""},
|
||||||
@@ -22,6 +22,6 @@ func TestOptionsScopeFlags(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
assert.Equal(t, tt.flags, OptionsScopeFlags(tt.scope))
|
assert.Equal(t, tt.flags, ScopeToFlags(tt.scope))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
30
tmux.go
30
tmux.go
@@ -4,10 +4,10 @@ import (
|
|||||||
"bufio"
|
"bufio"
|
||||||
"bytes"
|
"bytes"
|
||||||
"regexp"
|
"regexp"
|
||||||
|
"strconv"
|
||||||
)
|
)
|
||||||
|
|
||||||
var optMatcher = regexp.MustCompile(`^\s*([@\w-][\w-]+)\s+(.*)$`)
|
var optMatcher = regexp.MustCompile(`^\s*([@\w-][\w-]+)\s+(.*)$`)
|
||||||
var quote = []byte(`"`)
|
|
||||||
|
|
||||||
// Tmux enables easily running tmux commands.
|
// Tmux enables easily running tmux commands.
|
||||||
type Tmux struct {
|
type Tmux struct {
|
||||||
@@ -49,8 +49,10 @@ func (s *Tmux) Args() []string {
|
|||||||
return args
|
return args
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Tmux) GetOptions(scope OptionsScope) (map[string]string, error) {
|
// GetOptions uses the "show-options" command to get all options within given
|
||||||
out, err := s.Exec("show-options", OptionsScopeFlags(scope))
|
// SCOPE.
|
||||||
|
func (s *Tmux) GetOptions(scope Scope) (Options, error) {
|
||||||
|
out, err := s.Exec("show-options", ScopeToFlags(scope))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -58,24 +60,24 @@ func (s *Tmux) GetOptions(scope OptionsScope) (map[string]string, error) {
|
|||||||
return s.parseOptions(out), nil
|
return s.parseOptions(out), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Tmux) parseOptions(options []byte) map[string]string {
|
func (s *Tmux) parseOptions(options []byte) Options {
|
||||||
scanner := bufio.NewScanner(bytes.NewBuffer(options))
|
scanner := bufio.NewScanner(bytes.NewBuffer(options))
|
||||||
result := map[string]string{}
|
result := Options{}
|
||||||
|
|
||||||
for scanner.Scan() {
|
for scanner.Scan() {
|
||||||
match := optMatcher.FindSubmatch(scanner.Bytes())
|
match := optMatcher.FindSubmatch(scanner.Bytes())
|
||||||
if len(match) > 2 {
|
if len(match) > 2 {
|
||||||
result[string(match[1])] = string(s.unwrap(match[2], quote))
|
key := string(match[1])
|
||||||
|
val := string(match[2])
|
||||||
|
|
||||||
|
unquoted, err := strconv.Unquote(val)
|
||||||
|
if err == nil {
|
||||||
|
result[key] = unquoted
|
||||||
|
} else {
|
||||||
|
result[key] = val
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Tmux) unwrap(input, wrap []byte) []byte {
|
|
||||||
if bytes.HasPrefix(input, wrap) && bytes.HasSuffix(input, wrap) {
|
|
||||||
return bytes.TrimSuffix(bytes.TrimPrefix(input, wrap), wrap)
|
|
||||||
}
|
|
||||||
|
|
||||||
return input
|
|
||||||
}
|
|
||||||
|
|||||||
30
tmux_test.go
30
tmux_test.go
@@ -152,49 +152,57 @@ func TestTmuxArgs(t *testing.T) {
|
|||||||
func TestTmuxGetOptions(t *testing.T) {
|
func TestTmuxGetOptions(t *testing.T) {
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
flags string
|
flags string
|
||||||
scope OptionsScope
|
scope Scope
|
||||||
opts map[string]string
|
opts Options
|
||||||
out []byte
|
out []byte
|
||||||
error error
|
error error
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
opts: map[string]string{"hello-world": "FooBar"},
|
opts: Options{"hello-world": "FooBar"},
|
||||||
out: []byte(`hello-world FooBar`),
|
out: []byte(`hello-world FooBar`),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
scope: Server,
|
scope: Server,
|
||||||
flags: "-s",
|
flags: "-s",
|
||||||
opts: map[string]string{"hello-world": "Foo Bar"},
|
opts: Options{"hello-world": "Foo Bar"},
|
||||||
out: []byte(`hello-world "Foo Bar"`),
|
out: []byte(`hello-world "Foo Bar"`),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
scope: GlobalSession,
|
scope: GlobalSession,
|
||||||
flags: "-g",
|
flags: "-g",
|
||||||
opts: map[string]string{"hello-world": "Foo Bar"},
|
opts: Options{"hello-world": "Foo Bar"},
|
||||||
out: []byte(`hello-world "Foo Bar"`),
|
out: []byte(`hello-world "Foo Bar"`),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
scope: GlobalWindow,
|
scope: GlobalWindow,
|
||||||
flags: "-gw",
|
flags: "-gw",
|
||||||
opts: map[string]string{"hello-world": " Foo Bar "},
|
opts: Options{"hello-world": " Foo Bar "},
|
||||||
out: []byte(`hello-world " Foo Bar "`),
|
out: []byte(`hello-world " Foo Bar "`),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
scope: Window,
|
scope: Window,
|
||||||
flags: "-w",
|
flags: "-w",
|
||||||
opts: map[string]string{"@foo": "bar"},
|
opts: Options{"@foo": "bar"},
|
||||||
out: []byte(`@foo bar`),
|
out: []byte(`@foo bar`),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
opts: map[string]string{
|
scope: Window,
|
||||||
"@foo": "bar",
|
flags: "-w",
|
||||||
"@themepack": "powerline/default/green",
|
opts: Options{"status-left": "\"#H\" >>"},
|
||||||
"status-left": "This Is Left",
|
out: []byte(`status-left "\"#H\" >>"`),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
opts: Options{
|
||||||
|
"@foo": "bar",
|
||||||
|
"@themepack": "powerline/default/green",
|
||||||
|
"status-left": "This Is Left",
|
||||||
|
"status-right": "\"#H\" >>",
|
||||||
},
|
},
|
||||||
out: []byte(`
|
out: []byte(`
|
||||||
@foo bar
|
@foo bar
|
||||||
@themepack "powerline/default/green"
|
@themepack "powerline/default/green"
|
||||||
status-left This Is Left
|
status-left This Is Left
|
||||||
|
status-right "\"#H\" >>"
|
||||||
`),
|
`),
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user