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

View file

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