Skip to content

Commit

Permalink
nushell style
Browse files Browse the repository at this point in the history
  • Loading branch information
rsteube committed Aug 11, 2022
1 parent 4d2ec94 commit 43ff779
Showing 1 changed file with 62 additions and 3 deletions.
65 changes: 62 additions & 3 deletions internal/shell/nushell/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ import (
"strings"

"github.com/rsteube/carapace/internal/common"
"github.com/rsteube/carapace/third_party/github.com/elves/elvish/pkg/ui"
)

type record struct {
Value string `json:"value"`
Description string `json:"description,omitempty"`
Value string `json:"value"`
Description string `json:"description,omitempty"`
nushellStyle `json:",inline"`
}

var sanitizer = strings.NewReplacer(
Expand Down Expand Up @@ -43,8 +45,65 @@ func ActionRawValues(currentWord string, nospace bool, values common.RawValues)
val.Value = val.Value + " "
}

vals[index] = record{Value: val.Value, Description: val.TrimmedDescription()}
vals[index] = record{Value: val.Value, Description: val.TrimmedDescription(), nushellStyle: toNushellstyle(val.Style)}
}
m, _ := json.Marshal(vals)
return string(m)
}

type nushellStyle struct {
Foreground string `json:"foreground,omitempty"`
Background string `json:"background,omitempty"`
Attributes []string `json:"attributes,omitempty"`
}

func toNushellstyle(_style string) nushellStyle {
s := parseStyle(_style)

attributes := make([]string, 0)
if s.Blink {
attributes = append(attributes, "blink")
}
if s.Bold {
attributes = append(attributes, "bold")
}
if s.Dim {
attributes = append(attributes, "dimmed")
}
if s.Italic {
attributes = append(attributes, "italic")
}
if s.Inverse {
attributes = append(attributes, "reverse")
}
if s.Underlined {
attributes = append(attributes, "underline")
}

fg := ""
if s.Foreground != nil {
fg = s.Foreground.String()
}
bg := ""
if s.Background != nil {
bg = s.Background.String()
}

// TODO patch color names
return nushellStyle{
Foreground: fg,
Background: bg,
Attributes: attributes,
}
}

// TODO copied from style package
func parseStyle(s string) ui.Style {
stylings := make([]ui.Styling, 0)
for _, word := range strings.Split(s, " ") {
if styling := ui.ParseStyling(word); styling != nil {
stylings = append(stylings, styling)
}
}
return ui.ApplyStyling(ui.Style{}, stylings...)
}

0 comments on commit 43ff779

Please sign in to comment.