fix(ansi): allow hyperlink overrides

This commit is contained in:
Jan De Dobbeleer 2023-01-06 13:47:05 +01:00 committed by Jan De Dobbeleer
parent 85897ce9ec
commit 5eb6e99ea3
2 changed files with 16 additions and 12 deletions

View file

@ -66,7 +66,7 @@ const (
LINK = "link" LINK = "link"
TEXT = "text" TEXT = "text"
OTHER = "plain" OTHER = "other"
ANCHOR = "ANCHOR" ANCHOR = "ANCHOR"
BG = "BG" BG = "BG"
FG = "FG" FG = "FG"
@ -113,11 +113,11 @@ type Writer struct {
hasHyperlink bool hasHyperlink bool
hyperlinkBuilder strings.Builder hyperlinkBuilder strings.Builder
squareIndex, roundCount int squareIndex, roundCount int
state string hyperlinkState string
} }
func (w *Writer) Init(shellName string) { func (w *Writer) Init(shellName string) {
w.state = OTHER w.hyperlinkState = OTHER
w.shell = shellName w.shell = shellName
switch w.shell { switch w.shell {
case shell.BASH: case shell.BASH:
@ -327,6 +327,7 @@ func (w *Writer) Write(background, foreground, text string) {
// append remnant hyperlink // append remnant hyperlink
w.builder.WriteString(w.hyperlinkBuilder.String()) w.builder.WriteString(w.hyperlinkBuilder.String())
w.hyperlinkBuilder.Reset() w.hyperlinkBuilder.Reset()
w.hyperlinkState = OTHER
// reset colors // reset colors
w.writeEscapedAnsiString(colorStyle.End) w.writeEscapedAnsiString(colorStyle.End)
@ -349,11 +350,14 @@ func (w *Writer) writeEscapedAnsiString(text string) {
if w.Plain { if w.Plain {
return return
} }
if len(w.format) == 0 { if len(w.format) != 0 {
text = fmt.Sprintf(w.format, text)
}
if w.hyperlinkState == OTHER {
w.builder.WriteString(text) w.builder.WriteString(text)
return return
} }
w.builder.WriteString(fmt.Sprintf(w.format, text)) w.hyperlinkBuilder.WriteString(text)
} }
func (w *Writer) getAnsiFromColorString(colorString string, isBackground bool) Color { func (w *Writer) getAnsiFromColorString(colorString string, isBackground bool) Color {

View file

@ -21,13 +21,13 @@ func (w *Writer) write(i int, s rune) {
return return
} }
if s == '[' && w.state == OTHER { if s == '[' && w.hyperlinkState == OTHER {
w.state = TEXT w.hyperlinkState = TEXT
w.hyperlinkBuilder.WriteRune(s) w.hyperlinkBuilder.WriteRune(s)
return return
} }
if w.state == OTHER { if w.hyperlinkState == OTHER {
w.length += runewidth.RuneWidth(s) w.length += runewidth.RuneWidth(s)
w.builder.WriteRune(s) w.builder.WriteRune(s)
return return
@ -42,13 +42,13 @@ func (w *Writer) write(i int, s rune) {
case '(': case '(':
// split into link part // split into link part
if w.squareIndex == i-1 { if w.squareIndex == i-1 {
w.state = LINK w.hyperlinkState = LINK
} }
if w.state == LINK { if w.hyperlinkState == LINK {
w.roundCount++ w.roundCount++
} }
case ')': case ')':
if w.state != LINK { if w.hyperlinkState != LINK {
return return
} }
w.roundCount-- w.roundCount--
@ -58,7 +58,7 @@ func (w *Writer) write(i int, s rune) {
// end of link part // end of link part
w.builder.WriteString(w.replaceHyperlink(w.hyperlinkBuilder.String())) w.builder.WriteString(w.replaceHyperlink(w.hyperlinkBuilder.String()))
w.hyperlinkBuilder.Reset() w.hyperlinkBuilder.Reset()
w.state = OTHER w.hyperlinkState = OTHER
} }
} }