feat: support overline

This commit is contained in:
Jan De Dobbeleer 2022-05-20 21:53:59 +02:00 committed by Jan De Dobbeleer
parent b3b4f473b5
commit 37dd466adf
3 changed files with 9 additions and 1 deletions

View file

@ -33,6 +33,7 @@ type Ansi struct {
bold string
italic string
underline string
overline string
strikethrough string
blink string
reverse string
@ -65,6 +66,7 @@ func (a *Ansi) Init(shellName string) {
a.bold = "%%{\x1b[1m%%}%s%%{\x1b[22m%%}"
a.italic = "%%{\x1b[3m%%}%s%%{\x1b[23m%%}"
a.underline = "%%{\x1b[4m%%}%s%%{\x1b[24m%%}"
a.overline = "%%{\x1b[53m%%}%s%%{\x1b[55m%%}"
a.blink = "%%{\x1b[5m%%}%s%%{\x1b[25m%%}"
a.reverse = "%%{\x1b[7m%%}%s%%{\x1b[27m%%}"
a.dimmed = "%%{\x1b[2m%%}%s%%{\x1b[22m%%}"
@ -91,6 +93,7 @@ func (a *Ansi) Init(shellName string) {
a.bold = "\\[\x1b[1m\\]%s\\[\x1b[22m\\]"
a.italic = "\\[\x1b[3m\\]%s\\[\x1b[23m\\]"
a.underline = "\\[\x1b[4m\\]%s\\[\x1b[24m\\]"
a.overline = "\\[\x1b[53m\\]%s\\[\x1b[55m\\]"
a.blink = "\\[\x1b[5m\\]%s\\[\x1b[25m\\]"
a.reverse = "\\[\x1b[7m\\]%s\\[\x1b[27m\\]"
a.dimmed = "\\[\x1b[2m\\]%s\\[\x1b[22m\\]"
@ -117,6 +120,7 @@ func (a *Ansi) Init(shellName string) {
a.bold = "\x1b[1m%s\x1b[22m"
a.italic = "\x1b[3m%s\x1b[23m"
a.underline = "\x1b[4m%s\x1b[24m"
a.overline = "\x1b[53m%s\x1b[55m"
a.blink = "\x1b[5m%s\x1b[25m"
a.reverse = "\x1b[7m%s\x1b[27m"
a.dimmed = "\x1b[2m%s\x1b[22m"
@ -195,6 +199,8 @@ func (a *Ansi) formatText(text string) string {
formatted = fmt.Sprintf(a.bold, result["text"])
case "u":
formatted = fmt.Sprintf(a.underline, result["text"])
case "o":
formatted = fmt.Sprintf(a.overline, result["text"])
case "i":
formatted = fmt.Sprintf(a.italic, result["text"])
case "s":
@ -209,7 +215,7 @@ func (a *Ansi) formatText(text string) string {
text = strings.Replace(text, result["context"], formatted, 1)
}
}
rgx := "(?P<context><(?P<format>[buisrdf])>(?P<text>[^<]+)</[buisrdf]>)"
rgx := "(?P<context><(?P<format>[buisrdfo])>(?P<text>[^<]+)</[buisrdfo]>)"
for results := regex.FindAllNamedRegexMatch(rgx, text); len(results) != 0; results = regex.FindAllNamedRegexMatch(rgx, text) {
replaceFormats(results)
}

View file

@ -76,6 +76,7 @@ func TestFormatText(t *testing.T) {
{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"},
{Case: "overline", Text: "This <o>is</o> white", Expected: "This \x1b[53mis\x1b[55m white"},
}
for _, tc := range cases {
a := Ansi{}

View file

@ -94,6 +94,7 @@ You can make use of the following syntax to decorate text:
- `<b>bold</b>`: renders `bold` as bold text
- `<u>underline</u>`: renders `underline` as underlined text
- `<o>overline</o>`: renders `overline` as overlined text
- `<i>italic</i>`: renders `italic` as italic text
- `<s>strikethrough</s>`: renders `strikethrough` as strikethrough text
- `<d>dimmed</d>`: renders `dimmed` as dimmed text