Repository: rsteube/carapace
Pull request #956 (2023-12-14 22:28:07)

xonsh: support style


Patch:
diff --git a/internal/shell/xonsh/action.go b/internal/shell/xonsh/action.go
index 81e0ad7..54c3464 100644
--- a/internal/shell/xonsh/action.go
+++ b/internal/shell/xonsh/action.go
@@ -18,6 +18,7 @@ type richCompletion struct {
 	Value       string
 	Display     string
 	Description string
+	Style       string
 }
 
 // ActionRawValues formats values for xonsh.
@@ -38,7 +39,12 @@ func ActionRawValues(currentWord string, meta common.Meta, values common.RawValu
 			val.Value = val.Value + " "
 		}
 
-		vals[index] = richCompletion{Value: val.Value, Display: val.Display, Description: val.TrimmedDescription()}
+		vals[index] = richCompletion{
+			Value:       val.Value,
+			Display:     val.Display,
+			Description: val.TrimmedDescription(),
+			Style:       convertStyle("bg-default fg-default " + val.Style),
+		}
 	}
 	m, _ := json.Marshal(vals)
 	return string(m)
diff --git a/internal/shell/xonsh/snippet.go b/internal/shell/xonsh/snippet.go
index caa9875..f9a9254 100644
--- a/internal/shell/xonsh/snippet.go
+++ b/internal/shell/xonsh/snippet.go
@@ -28,7 +28,7 @@ def _%v_completer(context):
 
         output, _ = Popen(['%v', '_carapace', 'xonsh', *[a.value for a in context.args], fix_prefix(context.prefix)], stdout=PIPE, stderr=PIPE).communicate()
         try:
-            result = {RichCompletion(c["Value"], display=c["Display"], description=c["Description"], prefix_len=len(context.raw_prefix), append_closing_quote=False) for c in loads(output)}
+            result = {RichCompletion(c["Value"], display=c["Display"], description=c["Description"], prefix_len=len(context.raw_prefix), append_closing_quote=False, style=c["Style"]) for c in loads(output)}
         except:
             result = {}
         if len(result) == 0:
diff --git a/internal/shell/xonsh/style.go b/internal/shell/xonsh/style.go
new file mode 100644
index 0000000..13392d7
--- /dev/null
+++ b/internal/shell/xonsh/style.go
@@ -0,0 +1,307 @@
+package xonsh
+
+import "strings"
+
+// TODO support colorXXX (256 colors)
+func convertStyle(s string) string {
+	s = strings.NewReplacer(
+		"black", "ansiblack",
+		"red", "ansired",
+		"green", "ansigreen",
+		"yellow", "ansiyellow",
+		"blue", "ansiblue",
+		"magenta", "ansimagenta",
+		"cyan", "ansicyan",
+		"white", "ansiwhite",
+	).Replace(s)
+
+	s = strings.NewReplacer(
+		"bright-ansiblack", "ansibrightblack",
+		"bright-ansired", "ansibrightred",
+		"bright-ansigreen", "ansibrightgreen",
+		"bright-ansiyellow", "ansibrightyellow",
+		"bright-ansiblue", "ansibrightblue",
+		"bright-ansimagenta", "ansibrightmagenta",
+		"bright-ansicyan", "ansibrightcyan",
+		"bright-ansiwhite", "ansiwhite",
+	).Replace(s)
+
+	s = strings.NewReplacer(
+		"bg-", "bg:",
+		"fg-", "fg:",
+	).Replace(s)
+
+	s = strings.NewReplacer(
+		"toggle-", "", // TODO unsupported
+		"no-", "no",
+	).Replace(s)
+
+	s = strings.NewReplacer(
+		"dim", "", // TODO unsupported
+		"underlined", "underline",
+		"inverse", "reverse",
+	).Replace(s)
+
+	s = color256replacer.Replace(s)
+
+	return s
+}
+
+var color256replacer = strings.NewReplacer(
+	"color255", "#eeeeee",
+	"color254", "#e4e4e4",
+	"color253", "#dadada",
+	"color252", "#d0d0d0",
+	"color251", "#c6c6c6",
+	"color250", "#bcbcbc",
+	"color249", "#b2b2b2",
+	"color248", "#a8a8a8",
+	"color247", "#9e9e9e",
+	"color246", "#949494",
+	"color245", "#8a8a8a",
+	"color244", "#808080",
+	"color243", "#767676",
+	"color242", "#6c6c6c",
+	"color241", "#626262",
+	"color240", "#585858",
+	"color239", "#4e4e4e",
+	"color238", "#444444",
+	"color237", "#3a3a3a",
+	"color236", "#303030",
+	"color235", "#262626",
+	"color234", "#1c1c1c",
+	"color233", "#121212",
+	"color232", "#080808",
+	"color231", "#ffffff",
+	"color230", "#ffffd7",
+	"color229", "#ffffaf",
+	"color228", "#ffff87",
+	"color227", "#ffff5f",
+	"color226", "#ffff00",
+	"color225", "#ffd7ff",
+	"color224", "#ffd7d7",
+	"color223", "#ffd7af",
+	"color222", "#ffd787",
+	"color221", "#ffd75f",
+	"color220", "#ffd700",
+	"color219", "#ffafff",
+	"color218", "#ffafd7",
+	"color217", "#ffafaf",
+	"color216", "#ffaf87",
+	"color215", "#ffaf5f",
+	"color214", "#ffaf00",
+	"color213", "#ff87ff",
+	"color212", "#ff87d7",
+	"color211", "#ff87af",
+	"color210", "#ff8787",
+	"color209", "#ff875f",
+	"color208", "#ff8700",
+	"color207", "#ff5fff",
+	"color206", "#ff5fd7",
+	"color205", "#ff5faf",
+	"color204", "#ff5f87",
+	"color203", "#ff5f5f",
+	"color202", "#ff5f00",
+	"color201", "#ff00ff",
+	"color200", "#ff00d7",
+	"color199", "#ff00af",
+	"color198", "#ff0087",
+	"color197", "#ff005f",
+	"color196", "#ff0000",
+	"color195", "#d7ffff",
+	"color194", "#d7ffd7",
+	"color193", "#d7ffaf",
+	"color192", "#d7ff87",
+	"color191", "#d7ff5f",
+	"color190", "#d7ff00",
+	"color189", "#d7d7ff",
+	"color188", "#d7d7d7",
+	"color187", "#d7d7af",
+	"color186", "#d7d787",
+	"color185", "#d7d75f",
+	"color184", "#d7d700",
+	"color183", "#d7afff",
+	"color182", "#d7afd7",
+	"color181", "#d7afaf",
+	"color180", "#d7af87",
+	"color179", "#d7af5f",
+	"color178", "#d7af00",
+	"color177", "#d787ff",
+	"color176", "#d787d7",
+	"color175", "#d787af",
+	"color174", "#d78787",
+	"color173", "#d7875f",
+	"color172", "#d78700",
+	"color171", "#d75fff",
+	"color170", "#d75fd7",
+	"color169", "#d75faf",
+	"color168", "#d75f87",
+	"color167", "#d75f5f",
+	"color166", "#d75f00",
+	"color165", "#d700ff",
+	"color164", "#d700d7",
+	"color163", "#d700af",
+	"color162", "#d70087",
+	"color161", "#d7005f",
+	"color160", "#d70000",
+	"color159", "#afffff",
+	"color158", "#afffd7",
+	"color157", "#afffaf",
+	"color156", "#afff87",
+	"color155", "#afff5f",
+	"color154", "#afff00",
+	"color153", "#afd7ff",
+	"color152", "#afd7d7",
+	"color151", "#afd7af",
+	"color150", "#afd787",
+	"color149", "#afd75f",
+	"color148", "#afd700",
+	"color147", "#afafff",
+	"color146", "#afafd7",
+	"color145", "#afafaf",
+	"color144", "#afaf87",
+	"color143", "#afaf5f",
+	"color142", "#afaf00",
+	"color141", "#af87ff",
+	"color140", "#af87d7",
+	"color139", "#af87af",
+	"color138", "#af8787",
+	"color137", "#af875f",
+	"color136", "#af8700",
+	"color135", "#af5fff",
+	"color134", "#af5fd7",
+	"color133", "#af5faf",
+	"color132", "#af5f87",
+	"color131", "#af5f5f",
+	"color130", "#af5f00",
+	"color129", "#af00ff",
+	"color128", "#af00d7",
+	"color127", "#af00af",
+	"color126", "#af0087",
+	"color125", "#af005f",
+	"color124", "#af0000",
+	"color123", "#87ffff",
+	"color122", "#87ffd7",
+	"color121", "#87ffaf",
+	"color120", "#87ff87",
+	"color119", "#87ff5f",
+	"color118", "#87ff00",
+	"color117", "#87d7ff",
+	"color116", "#87d7d7",
+	"color115", "#87d7af",
+	"color114", "#87d787",
+	"color113", "#87d75f",
+	"color112", "#87d700",
+	"color111", "#87afff",
+	"color110", "#87afd7",
+	"color109", "#87afaf",
+	"color108", "#87af87",
+	"color107", "#87af5f",
+	"color106", "#87af00",
+	"color105", "#8787ff",
+	"color104", "#8787d7",
+	"color103", "#8787af",
+	"color102", "#878787",
+	"color101", "#87875f",
+	"color100", "#878700",
+	"color99", "#875fff",
+	"color98", "#875fd7",
+	"color97", "#875faf",
+	"color96", "#875f87",
+	"color95", "#875f5f",
+	"color94", "#875f00",
+	"color93", "#8700ff",
+	"color92", "#8700d7",
+	"color91", "#8700af",
+	"color90", "#870087",
+	"color89", "#87005f",
+	"color88", "#870000",
+	"color87", "#5fffff",
+	"color86", "#5fffd7",
+	"color85", "#5fffaf",
+	"color84", "#5fff87",
+	"color83", "#5fff5f",
+	"color82", "#5fff00",
+	"color81", "#5fd7ff",
+	"color80", "#5fd7d7",
+	"color79", "#5fd7af",
+	"color78", "#5fd787",
+	"color77", "#5fd75f",
+	"color76", "#5fd700",
+	"color75", "#5fafff",
+	"color74", "#5fafd7",
+	"color73", "#5fafaf",
+	"color72", "#5faf87",
+	"color71", "#5faf5f",
+	"color70", "#5faf00",
+	"color69", "#5f87ff",
+	"color68", "#5f87d7",
+	"color67", "#5f87af",
+	"color66", "#5f8787",
+	"color65", "#5f875f",
+	"color64", "#5f8700",
+	"color63", "#5f5fff",
+	"color62", "#5f5fd7",
+	"color61", "#5f5faf",
+	"color60", "#5f5f87",
+	"color59", "#5f5f5f",
+	"color58", "#5f5f00",
+	"color57", "#5f00ff",
+	"color56", "#5f00d7",
+	"color55", "#5f00af",
+	"color54", "#5f0087",
+	"color53", "#5f005f",
+	"color52", "#5f0000",
+	"color51", "#00ffff",
+	"color50", "#00ffd7",
+	"color49", "#00ffaf",
+	"color48", "#00ff87",
+	"color47", "#00ff5f",
+	"color46", "#00ff00",
+	"color45", "#00d7ff",
+	"color44", "#00d7d7",
+	"color43", "#00d7af",
+	"color42", "#00d787",
+	"color41", "#00d75f",
+	"color40", "#00d700",
+	"color39", "#00afff",
+	"color38", "#00afd7",
+	"color37", "#00afaf",
+	"color36", "#00af87",
+	"color35", "#00af5f",
+	"color34", "#00af00",
+	"color33", "#0087ff",
+	"color32", "#0087d7",
+	"color31", "#0087af",
+	"color30", "#008787",
+	"color29", "#00875f",
+	"color28", "#008700",
+	"color27", "#005fff",
+	"color26", "#005fd7",
+	"color25", "#005faf",
+	"color24", "#005f87",
+	"color23", "#005f5f",
+	"color22", "#005f00",
+	"color21", "#0000ff",
+	"color20", "#0000d7",
+	"color19", "#0000af",
+	"color18", "#000087",
+	"color17", "#00005f",
+	"color16", "#000000",
+	"color15", "#ffffff",
+	"color14", "#00ffff",
+	"color13", "#ff00ff",
+	"color12", "#0000ff",
+	"color11", "#ffff00",
+	"color10", "#00ff00",
+	"color9", "#ff0000",
+	"color8", "#808080",
+	"color7", "#c0c0c0",
+	"color6", "#008080",
+	"color5", "#800080",
+	"color4", "#000080",
+	"color3", "#808000",
+	"color2", "#008000",
+	"color1", "#800000",
+	"color0", "#000000",
+)


Test patch:
diff --git a/example/cmd/_test/xonsh.py b/example/cmd/_test/xonsh.py
index 60d9e1c..959961b 100644
--- a/example/cmd/_test/xonsh.py
+++ b/example/cmd/_test/xonsh.py
@@ -14,7 +14,7 @@ def _example_completer(context):
 
         output, _ = Popen(['example', '_carapace', 'xonsh', *[a.value for a in context.args], fix_prefix(context.prefix)], stdout=PIPE, stderr=PIPE).communicate()
         try:
-            result = {RichCompletion(c["Value"], display=c["Display"], description=c["Description"], prefix_len=len(context.raw_prefix), append_closing_quote=False) for c in loads(output)}
+            result = {RichCompletion(c["Value"], display=c["Display"], description=c["Description"], prefix_len=len(context.raw_prefix), append_closing_quote=False, style=c["Style"]) for c in loads(output)}
         except:
             result = {}
         if len(result) == 0: