mirror of
https://github.com/JanDeDobbeleer/oh-my-posh.git
synced 2024-11-09 20:44:03 -08:00
parent
f6d0b9e076
commit
df8a0fdf6d
|
@ -149,26 +149,31 @@ func (a *Ansi) generateHyperlink(text string) string {
|
|||
}
|
||||
|
||||
func (a *Ansi) formatText(text string) string {
|
||||
results := regex.FindAllNamedRegexMatch("(?P<context><(?P<format>[buisrdf])>(?P<text>[^<]+)</[buisrdf]>)", text)
|
||||
for _, result := range results {
|
||||
var formatted string
|
||||
switch result["format"] {
|
||||
case "b":
|
||||
formatted = fmt.Sprintf(a.bold, result["text"])
|
||||
case "u":
|
||||
formatted = fmt.Sprintf(a.underline, result["text"])
|
||||
case "i":
|
||||
formatted = fmt.Sprintf(a.italic, result["text"])
|
||||
case "s":
|
||||
formatted = fmt.Sprintf(a.strikethrough, result["text"])
|
||||
case "d":
|
||||
formatted = fmt.Sprintf(a.dimmed, result["text"])
|
||||
case "f":
|
||||
formatted = fmt.Sprintf(a.blink, result["text"])
|
||||
case "r":
|
||||
formatted = fmt.Sprintf(a.reverse, result["text"])
|
||||
replaceFormats := func(results []map[string]string) {
|
||||
for _, result := range results {
|
||||
var formatted string
|
||||
switch result["format"] {
|
||||
case "b":
|
||||
formatted = fmt.Sprintf(a.bold, result["text"])
|
||||
case "u":
|
||||
formatted = fmt.Sprintf(a.underline, result["text"])
|
||||
case "i":
|
||||
formatted = fmt.Sprintf(a.italic, result["text"])
|
||||
case "s":
|
||||
formatted = fmt.Sprintf(a.strikethrough, result["text"])
|
||||
case "d":
|
||||
formatted = fmt.Sprintf(a.dimmed, result["text"])
|
||||
case "f":
|
||||
formatted = fmt.Sprintf(a.blink, result["text"])
|
||||
case "r":
|
||||
formatted = fmt.Sprintf(a.reverse, result["text"])
|
||||
}
|
||||
text = strings.Replace(text, result["context"], formatted, 1)
|
||||
}
|
||||
text = strings.Replace(text, result["context"], formatted, 1)
|
||||
}
|
||||
rgx := "(?P<context><(?P<format>[buisrdf])>(?P<text>[^<]+)</[buisrdf]>)"
|
||||
for results := regex.FindAllNamedRegexMatch(rgx, text); len(results) != 0; results = regex.FindAllNamedRegexMatch(rgx, text) {
|
||||
replaceFormats(results)
|
||||
}
|
||||
return text
|
||||
}
|
||||
|
|
|
@ -74,6 +74,7 @@ func TestFormatText(t *testing.T) {
|
|||
{Case: "dimmed", Text: "This <d>is</d> white", Expected: "This \x1b[2mis\x1b[22m white"},
|
||||
{Case: "flash", Text: "This <f>is</f> white", Expected: "This \x1b[5mis\x1b[25m white"},
|
||||
{Case: "reversed", Text: "This <r>is</r> white", Expected: "This \x1b[7mis\x1b[27m white"},
|
||||
{Case: "double", Text: "This <i><f>is</f></i> white", Expected: "This \x1b[3m\x1b[5mis\x1b[25m\x1b[23m white"},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
a := Ansi{}
|
||||
|
|
Loading…
Reference in a new issue