Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ActionMultiParts: fix c.Parts for empty dividier #867

Merged
merged 1 commit into from
Jul 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 28 additions & 6 deletions defaultActions.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,18 +219,40 @@ func ActionMultiParts(sep string, callback func(c Context) Action) Action {
// ActionMultiPartsN is like ActionMultiParts but limits the number of parts to `n`.
func ActionMultiPartsN(sep string, n int, callback func(c Context) Action) Action {
return ActionCallback(func(c Context) Action {
switch n {
case 0:
return ActionMessage("invalid value for n [ActionValuesDescribed]: %v", n)
case 1:
return callback(c).Invoke(c).ToA()
}

splitted := strings.SplitN(c.Value, sep, n)
prefix := ""
c.Parts = []string{}

switch {
case len(sep) == 0:
prefix = c.Value
c.Value = ""
case len(splitted) > 1:
c.Value = splitted[len(splitted)-1]
c.Parts = splitted[:len(splitted)-1]
prefix = strings.Join(c.Parts, sep) + sep
switch {
case n < 0:
prefix = c.Value
c.Value = ""
c.Parts = splitted
default:
prefix = c.Value
if n-1 < len(prefix) {
prefix = c.Value[:n-1]
c.Value = c.Value[n-1:]
} else {
c.Value = ""
}
c.Parts = strings.Split(prefix, "")
}
default:
if len(splitted) > 1 {
c.Value = splitted[len(splitted)-1]
c.Parts = splitted[:len(splitted)-1]
prefix = strings.Join(c.Parts, sep) + sep
}
}

nospace := '*'
Expand Down
11 changes: 11 additions & 0 deletions example/cmd/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ func init() {
actionCmd.Flags().String("multiparts", "", "ActionMultiParts()")
actionCmd.Flags().String("multiparts-nested", "", "ActionMultiParts(...ActionMultiParts...)")
actionCmd.Flags().String("multipartsn", "", "ActionMultiPartsN()")
actionCmd.Flags().String("multipartsn-empty", "", "ActionMultiPartsN()")
actionCmd.Flags().String("styles", "", "ActionStyles()")
actionCmd.Flags().String("styleconfig", "", "ActionStyleConfig()")
actionCmd.Flags().String("styled-values", "", "ActionStyledValues()")
Expand Down Expand Up @@ -146,6 +147,16 @@ func init() {
return carapace.ActionMessage("should never happen")
}
}),
"multipartsn-empty": carapace.ActionMultiPartsN("", 2, func(c carapace.Context) carapace.Action {
switch len(c.Parts) {
case 0:
return carapace.ActionValues("a", "b")
case 1:
return carapace.ActionValues("c", "d", "e").UniqueList("")
default:
return carapace.ActionMessage("should never happen")
}
}),
"styles": carapace.ActionStyles(),
"styleconfig": carapace.ActionStyleConfig(),
"styled-values": carapace.ActionStyledValues(
Expand Down
50 changes: 50 additions & 0 deletions example/cmd/multiparts.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"strings"

"github.com/rsteube/carapace"
"github.com/rsteube/carapace/pkg/style"
"github.com/spf13/cobra"
)

Expand All @@ -21,6 +22,10 @@ func init() {
multipartsCmd.Flags().String("dotdotdot", "", "multiparts with ... as divider")
multipartsCmd.Flags().String("equals", "", "multiparts with = as divider")
multipartsCmd.Flags().String("none", "", "multiparts without divider")
multipartsCmd.Flags().String("none-zero", "", "multiparts without divider limited to 0")
multipartsCmd.Flags().String("none-one", "", "multiparts without divider limited to 1")
multipartsCmd.Flags().String("none-two", "", "multiparts without divider limited to 2")
multipartsCmd.Flags().String("none-three", "", "multiparts without divider limited to 3")
multipartsCmd.Flags().String("slash", "", "multiparts with / as divider")

rootCmd.AddCommand(multipartsCmd)
Expand All @@ -35,6 +40,51 @@ func init() {
"none": carapace.ActionMultiParts("", func(c carapace.Context) carapace.Action {
return carapace.ActionValuesDescribed("a", "first", "b", "second", "c", "third", "d", "fourth").Invoke(c).Filter(c.Parts).ToA()
}),
"none-zero": carapace.ActionMultiPartsN("", 0, func(c carapace.Context) carapace.Action {
return carapace.ActionMessage("unreachable")
}),
"none-one": carapace.ActionMultiPartsN("", 1, func(c carapace.Context) carapace.Action {
return carapace.ActionValues("a", "b")
}),
"none-two": carapace.ActionMultiPartsN("", 2, func(c carapace.Context) carapace.Action {
switch len(c.Parts) {
case 0:
return carapace.ActionValuesDescribed(
"a", "zero",
"b", "zero",
).Style(style.Blue)
default:
return carapace.ActionValuesDescribed(
"a", "default",
"b", "default",
"c", "default",
).Style(style.Red).
UniqueList("")
}
}),
"none-three": carapace.ActionMultiPartsN("", 3, func(c carapace.Context) carapace.Action {
switch len(c.Parts) {
case 0:
return carapace.ActionValuesDescribed(
"a", "zero",
"b", "zero",
).Style(style.Blue)
case 1:
return carapace.ActionValuesDescribed(
"a", "one",
"b", "one",
"c", "one",
).Style(style.Red)
default:
return carapace.ActionValuesDescribed(
"a", "default",
"b", "default",
"c", "default",
"d", "default",
).Style(style.Green).
UniqueList("")
}
}),
"slash": actionMultipartsTest("/"),
})

Expand Down
96 changes: 96 additions & 0 deletions example/cmd/multiparts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/rsteube/carapace/pkg/style"
)

// TODO rename
func TestMultiparts(t *testing.T) {
sandbox.Package(t, "github.com/rsteube/carapace/example")(func(s *sandbox.Sandbox) {
s.Files(
Expand Down Expand Up @@ -75,5 +76,100 @@ func TestMultiparts(t *testing.T) {
StyleF(style.ForPath).
Prefix("VALUE=one,FILE=").
NoSpace(',', '/', '='))

s.Run("multiparts", "--none-zero", "").
Expect(carapace.ActionMessage("invalid value for n [ActionValuesDescribed]: 0").
Usage("multiparts without divider limited to 0"))

s.Run("multiparts", "--none-one", "").
Expect(carapace.ActionValues("a", "b").
Usage("multiparts without divider limited to 1"))

s.Run("multiparts", "--none-one", "a").
Expect(carapace.ActionValues("a").
Usage("multiparts without divider limited to 1"))

s.Run("multiparts", "--none-two", "").
Expect(carapace.ActionValuesDescribed(
"a", "zero",
"b", "zero",
).
NoSpace().
Style(style.Blue).
Usage("multiparts without divider limited to 2"))

s.Run("multiparts", "--none-two", "a").
Expect(carapace.ActionValuesDescribed(
"a", "default",
"b", "default",
"c", "default",
).
Prefix("a").
NoSpace().
Style(style.Red).
Usage("multiparts without divider limited to 2"))

s.Run("multiparts", "--none-two", "ab").
Expect(carapace.ActionValuesDescribed(
"a", "default",
"c", "default",
).
Prefix("ab").
NoSpace().
Style(style.Red).
Usage("multiparts without divider limited to 2"))

s.Run("multiparts", "--none-two", "abc").
Expect(carapace.ActionValuesDescribed(
"a", "default",
).
Prefix("abc").
NoSpace().
Style(style.Red).
Usage("multiparts without divider limited to 2"))

s.Run("multiparts", "--none-three", "a").
Expect(carapace.ActionValuesDescribed(
"a", "one",
"b", "one",
"c", "one",
).
Prefix("a").
NoSpace().
Style(style.Red).
Usage("multiparts without divider limited to 3"))

s.Run("multiparts", "--none-three", "ab").
Expect(carapace.ActionValuesDescribed(
"a", "default",
"b", "default",
"c", "default",
"d", "default",
).
Prefix("ab").
NoSpace().
Style(style.Green).
Usage("multiparts without divider limited to 3"))

s.Run("multiparts", "--none-three", "abc").
Expect(carapace.ActionValuesDescribed(
"a", "default",
"b", "default",
"d", "default",
).
Prefix("abc").
NoSpace().
Style(style.Green).
Usage("multiparts without divider limited to 3"))

s.Run("multiparts", "--none-three", "abcd").
Expect(carapace.ActionValuesDescribed(
"a", "default",
"b", "default",
).
Prefix("abcd").
NoSpace().
Style(style.Green).
Usage("multiparts without divider limited to 3"))
})
}
Loading