Skip to content

Commit

Permalink
Merge pull request #534 from rsteube/fix-tomultiparts-display
Browse files Browse the repository at this point in the history
ToMultiParts: fix display
  • Loading branch information
rsteube authored May 27, 2022
2 parents c0fb4fe + 10b4875 commit a855d57
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 17 deletions.
1 change: 0 additions & 1 deletion action_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,6 @@ func TestActionDirectories(t *testing.T) {

assertEqual(t,
ActionStyledValues(
"_test/", style.Of("fg-default", "bg-default", style.Blue, style.Bold),
"cmd/", style.Of("fg-default", "bg-default", style.Blue, style.Bold),
).noSpace(true).Invoke(Context{}).Prefix("example/"),
ActionDirectories().Invoke(Context{CallbackValue: "example/cm"}),
Expand Down
50 changes: 34 additions & 16 deletions invokedAction.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package carapace

import (
"fmt"
"regexp"
"strings"

"github.com/rsteube/carapace/internal/common"
Expand Down Expand Up @@ -88,37 +90,53 @@ func (a InvokedAction) ToA() Action {
return a.Action
}

// ToMultiPartsA create an ActionMultiParts from values with given divider
// ToMultiPartsA create an ActionMultiParts from values with given dividers
// a := carapace.ActionValues("A/B/C", "A/C", "B/C", "C").Invoke(c)
// b := a.ToMultiPartsA("/") // completes segments separately (first one is ["A/", "B/", "C"])
func (a InvokedAction) ToMultiPartsA(divider ...string) Action {
func (a InvokedAction) ToMultiPartsA(dividers ...string) Action {
return ActionCallback(func(c Context) Action {
for _, d := range divider {
a = a.toMultiPartsA(d).Invoke(c)
_split := func() func(s string) []string {
quotedDividiers := make([]string, 0)
for _, d := range dividers {
quotedDividiers = append(quotedDividiers, regexp.QuoteMeta(d))
}
f := fmt.Sprintf("([^(%v)]*(%v)?)", strings.Join(quotedDividiers, "|"), strings.Join(quotedDividiers, "|"))
r := regexp.MustCompile(f)
return func(s string) []string {
if matches := r.FindAllString(s, -1); matches != nil {
return matches
}
return []string{}
}
}()

splittedCV := _split(c.CallbackValue)
for _, d := range dividers {
if strings.HasSuffix(c.CallbackValue, d) {
splittedCV = append(splittedCV, "")
break
}

}
return a.ToA()
})
}

func (a InvokedAction) toMultiPartsA(divider string) Action {
return ActionMultiParts(divider, func(c Context) Action {
uniqueVals := make(map[string]common.RawValue)
for _, val := range a.rawValues {
if strings.HasPrefix(val.Value, strings.Join(c.Parts, divider)) {
if splitted := strings.Split(val.Value, divider); len(splitted) > len(c.Parts) {
if len(splitted) == len(c.Parts)+1 {
v := splitted[len(c.Parts)]
if strings.HasPrefix(val.Value, c.CallbackValue) {
if splitted := _split(val.Value); len(splitted) >= len(splittedCV) {
v := strings.Join(splitted[:len(splittedCV)], "")
d := splitted[len(splittedCV)-1]

if len(splitted) == len(splittedCV) {
uniqueVals[v] = common.RawValue{
Value: v,
Display: v,
Display: d,
Description: val.Description,
Style: val.Style,
}
} else {
v := splitted[len(c.Parts)] + divider
uniqueVals[v] = common.RawValue{
Value: v,
Display: v,
Display: d,
Description: "",
Style: "",
}
Expand Down
39 changes: 39 additions & 0 deletions invokedAction_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package carapace

import (
"strings"
"testing"

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

func TestToMultiParts(t *testing.T) {
_test := func(cv, expected string, delimiter ...string) {
a := ActionStyledValuesDescribed(
"A/a:1", "one", style.Green,
"A/a:2", "two", style.BgBlue,
"A/b:3", "three", style.Red,
"B/b:4", "four", style.Magenta,
"B/c:5:1", "fiftyone", style.Black,
"B/c:5:2/z", "fiftytwo", style.Yellow,
"B/c/", "withsuffix", style.Underlined,
)
a = a.Invoke(Context{}).ToMultiPartsA(delimiter...)
if actual := a.Invoke(Context{CallbackValue: cv}).value("export", cv); !strings.Contains(actual, expected) {
t.Errorf("expected '%v' in '%v' for '%v'", expected, actual, cv)
}
}

_test("A/a:1", `{"Value":"A/a:1","Display":"1","Description":"one","Style":"green"}`, "/", ":")
_test("A/a:1", `{"Value":"A/a:1","Display":"1","Description":"one","Style":"green"}`, ":", "/")
_test("A/a:1", `{"Value":"A/a:1","Display":"a:1","Description":"one","Style":"green"}`, "/")
_test("A", `{"Value":"A/","Display":"A/","Description":"","Style":""}`, "/", ":")
_test("A", `{"Value":"A/","Display":"A/","Description":"","Style":""}`, "/")
_test("", `{"Value":"A/","Display":"A/","Description":"","Style":""}`, "/")
_test("A/", `{"Value":"A/a:","Display":"a:","Description":"","Style":""}`, "/", ":")
_test("A/", `{"Value":"A/a:1","Display":"a:1","Description":"one","Style":"green"}`, "/")
_test("B/", `{"Value":"B/c/","Display":"c/","Description":"withsuffix","Style":"underlined"}`, "/")
_test("B/c:5", `{"Value":"B/c:5:2/","Display":"c:5:2/","Description":"","Style":""}`, "/")
_test("B/c:5", `{"Value":"B/c:5:","Display":"5:","Description":"","Style":""}`, "/", ":")
_test("B/c:5", `{"Value":"B/c:5:","Display":"5:","Description":"","Style":""}`, ":", "/")
}

0 comments on commit a855d57

Please sign in to comment.