Skip to content

Commit

Permalink
splitp: support redirects
Browse files Browse the repository at this point in the history
  • Loading branch information
rsteube committed Aug 9, 2023
1 parent bd350ea commit 1db08e2
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 7 deletions.
17 changes: 13 additions & 4 deletions action.go
Original file line number Diff line number Diff line change
Expand Up @@ -285,17 +285,26 @@ func (a Action) split(pipelines bool) Action {
tokens = tokens.CurrentPipeline()
}

context := NewContext(tokens.Strings()...)
current := tokens.CurrentToken()
prefix := c.Value[:current.Index]
originalValue := c.Value
context := NewContext(tokens.FilterRedirects().Words().Strings()...)
prefix := originalValue[:tokens.Words().CurrentToken().Index]
c.Args = context.Args
c.Parts = []string{}
c.Value = context.Value

if pipelines { // support redirects
if len(tokens) > 1 && tokens[len(tokens)-2].WordbreakType.IsRedirect() {
LOG.Printf("completing files for redirect arg %#v", tokens.Words().CurrentToken().Value)
prefix = originalValue[:tokens.CurrentToken().Index]
c.Value = tokens.CurrentToken().Value
a = ActionFiles()
}
}

invoked := a.Invoke(c)
for index, value := range invoked.rawValues {
if !invoked.meta.Nospace.Matches(value.Value) || strings.Contains(value.Value, " ") { // TODO special characters
switch current.State {
switch tokens.CurrentToken().State {
case shlex.QUOTING_ESCAPING_STATE:
invoked.rawValues[index].Value = fmt.Sprintf(`"%v"`, strings.Replace(value.Value, `"`, `\"`, -1))
case shlex.QUOTING_STATE:
Expand Down
89 changes: 89 additions & 0 deletions example/cmd/modifier_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -343,3 +343,92 @@ func TestSplit(t *testing.T) {
Tag("files"))
})
}

func TestSplitP(t *testing.T) {
os.Unsetenv("LS_COLORS")
sandbox.Package(t, "github.com/rsteube/carapace/example")(func(s *sandbox.Sandbox) {
s.Files("subdir/file1.txt", "")

s.Run("modifier", "--splitp", "pos1>").
Expect(carapace.ActionValues(
"subdir/",
).NoSpace('*').
StyleF(style.ForPath).
Prefix("pos1>").
Tag("files").
Usage("SplitP()"))

s.Run("modifier", "--splitp", "pos1>subdir/").
Expect(carapace.ActionValues(
"file1.txt",
).NoSpace('*').
StyleF(style.ForPath).
Prefix("pos1>subdir/").
Suffix(" ").
Tag("files").
Usage("SplitP()"))

s.Run("modifier", "--splitp", "pos1>subdir/file1.txt --b").
Expect(carapace.ActionValuesDescribed(
"--bool", "bool flag",
).NoSpace('*').
Prefix("pos1>subdir/file1.txt ").
Suffix(" ").
Tag("flags").
Usage("SplitP()"))

s.Run("modifier", "--splitp", "pos1 1>").
Expect(carapace.ActionValues(
"subdir/",
).NoSpace('*').
StyleF(style.ForPath).
Prefix("pos1 1>").
Tag("files").
Usage("SplitP()"))

s.Run("modifier", "--splitp", "<> subdir/file1.txt ").
Expect(carapace.ActionValues(
"pos1",
"positional1",
).NoSpace('*').
Prefix("<> subdir/file1.txt ").
Suffix(" ").
Usage("SplitP()"))

s.Run("modifier", "--splitp", "pos1|").
Expect(carapace.ActionValues(
"pos1",
"positional1",
).NoSpace('*').
Prefix("pos1|").
Suffix(" ").
Usage("SplitP()"))

s.Run("modifier", "--splitp", "pos1|&").
Expect(carapace.ActionValues(
"pos1",
"positional1",
).NoSpace('*').
Prefix("pos1|&").
Suffix(" ").
Usage("SplitP()"))

s.Run("modifier", "--splitp", "pos1 ;").
Expect(carapace.ActionValues(
"pos1",
"positional1",
).NoSpace('*').
Prefix("pos1 ;").
Suffix(" ").
Usage("SplitP()"))

s.Run("modifier", "--splitp", "pos1 | ").
Expect(carapace.ActionValues(
"pos1",
"positional1",
).NoSpace('*').
Prefix("pos1 | ").
Suffix(" ").
Usage("SplitP()"))
})
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/rsteube/carapace
go 1.15

require (
github.com/rsteube/carapace-shlex v0.0.2
github.com/rsteube/carapace-shlex v0.0.3
github.com/spf13/cobra v1.7.0
github.com/spf13/pflag v1.0.5
gopkg.in/yaml.v3 v3.0.1
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/rsteube/carapace-shlex v0.0.2 h1:ziSfpJviJkhaSmYvm6weFArTKsMDhd+Dji9HgpTwRCA=
github.com/rsteube/carapace-shlex v0.0.2/go.mod h1:zPw1dOFwvLPKStUy9g2BYKanI6bsQMATzDMYQQybo3o=
github.com/rsteube/carapace-shlex v0.0.3 h1:QzcD31o9L4EK0ga9AxUU1QrfvfYb9TCdgOYUhpIstpQ=
github.com/rsteube/carapace-shlex v0.0.3/go.mod h1:zPw1dOFwvLPKStUy9g2BYKanI6bsQMATzDMYQQybo3o=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/spf13/cobra v1.7.0 h1:hyqWnYt1ZQShIddO5kBpj3vu05/++x6tJ6dg8EC572I=
github.com/spf13/cobra v1.7.0/go.mod h1:uLxZILRyS/50WlhOIKD7W6V5bgeIt+4sICxh6uRMrb0=
Expand Down

0 comments on commit 1db08e2

Please sign in to comment.