Skip to content

Commit

Permalink
ActionMultiParts: fix c.Parts for empty dividier
Browse files Browse the repository at this point in the history
  • Loading branch information
rsteube committed Jul 31, 2023
1 parent 8245a20 commit 51cb0e0
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 6 deletions.
37 changes: 31 additions & 6 deletions defaultActions.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,20 +219,45 @@ 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 {
if n == 0 { // TODO more validation checks
return ActionMessage("invalid value for n: %v", n)
}

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
}
}

LOG.Println("-----")
LOG.Printf("splitted: %#v", splitted)
LOG.Printf("c.Parts: %#v", c.Parts)
LOG.Printf("c.Value: %#v", c.Value)
LOG.Printf("prefix: %#v", prefix)

nospace := '*'
if runes := []rune(sep); len(runes) > 0 {
nospace = runes[len(runes)-1]
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
9 changes: 9 additions & 0 deletions example/cmd/multiparts.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ 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-two", "", "multiparts without divider limited to 2")
multipartsCmd.Flags().String("slash", "", "multiparts with / as divider")

rootCmd.AddCommand(multipartsCmd)
Expand All @@ -35,6 +36,14 @@ 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-two": carapace.ActionMultiPartsN("", 2, func(c carapace.Context) carapace.Action {
switch len(c.Parts) {
case 0:
return carapace.ActionValues("a", "b")
default:
return carapace.ActionValues("a", "b", "c", "d").UniqueList("")
}
}),
"slash": actionMultipartsTest("/"),
})

Expand Down
29 changes: 29 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,33 @@ func TestMultiparts(t *testing.T) {
StyleF(style.ForPath).
Prefix("VALUE=one,FILE=").
NoSpace(',', '/', '='))

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

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

s.Run("multiparts", "--none-two", "a").
Expect(carapace.ActionValues("a", "b", "c", "d").
Prefix("a").
NoSpace().
Usage("multiparts without divider limited to 2"))

s.Run("multiparts", "--none-two", "ab").
Expect(carapace.ActionValues("a", "c", "d").
Prefix("ab").
NoSpace().
Usage("multiparts without divider limited to 2"))

s.Run("multiparts", "--none-two", "abc").
Expect(carapace.ActionValues("a", "d").
Prefix("abc").
NoSpace().
Usage("multiparts without divider limited to 2"))
})
}

0 comments on commit 51cb0e0

Please sign in to comment.