Skip to content

Commit

Permalink
Do not colorize wrapping continuation characters.
Browse files Browse the repository at this point in the history
  • Loading branch information
mitchell-as committed Nov 4, 2024
1 parent 48614a3 commit 55a07c0
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 8 deletions.
27 changes: 26 additions & 1 deletion internal/colorize/wrap.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package colorize

import (
"fmt"
"regexp"
"strings"

"github.com/ActiveState/cli/internal/logging"
)

type WrappedLines []WrappedLine
Expand Down Expand Up @@ -37,6 +40,7 @@ func Wrap(text string, maxLen int, includeLineEnds bool, continuation string) Wr

entries := make([]WrappedLine, 0)
colorCodes := colorRx.FindAllStringSubmatchIndex(text, -1)
colorNames := colorRx.FindAllStringSubmatch(text, -1)

isLineEnd := false
entry := WrappedLine{}
Expand Down Expand Up @@ -73,7 +77,15 @@ func Wrap(text string, maxLen int, includeLineEnds bool, continuation string) Wr
}
// Extract the word from the current line if it doesn't start the line.
if i > 0 && i < len(entry.Line)-1 && !isLinkRegexp.MatchString(entry.Line[i:]) {
wrapped = indent + continuation + entry.Line[i:]
tag := colorTag(pos, colorCodes, colorNames)
if continuation != "" && tag != "" {
// Do not colorize the continuation.
wrapped = fmt.Sprintf("%s[/RESET]%s%s%s", indent, continuation, tag, entry.Line[i:])
} else {
wrapped = indent + continuation + entry.Line[i:]
}
logging.Debug("continuation: '%s'", continuation)
logging.Debug("wrapped: '%s'", wrapped)
entry.Line = entry.Line[:i]
entry.Length -= wrappedLength
isLineEnd = true // emulate for wrapping purposes
Expand Down Expand Up @@ -103,6 +115,19 @@ func inRange(pos int, ranges [][]int) bool {
return false
}

// colorTag returns the currently active color tag (if any) at the given position.
func colorTag(pos int, ranges [][]int, names [][]string) string {
for i, intRange := range ranges {
if pos < intRange[0] {
continue // before [COLOR]
}
if i < len(ranges)-1 || pos < ranges[i+1][0] {
return names[i][0] // missing [/RESET] or between [COLOR] and [/RESET]
}
}
return ""
}

func isSpace(b byte) bool { return b == ' ' || b == '\t' }

func isUTF8TrailingByte(b byte) bool {
Expand Down
14 changes: 7 additions & 7 deletions internal/output/renderers/bulletlist.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package renderers

import (
"fmt"
"strings"

"github.com/ActiveState/cli/internal/colorize"
Expand All @@ -22,15 +21,15 @@ type bulletList struct {
// ├─ two
// │ wrapped
// └─ three
var BulletTree = []string{output.TreeMid, output.TreeMid, output.TreeLink, output.TreeEnd}
var BulletTree = []string{output.TreeMid, output.TreeMid, output.TreeLink + " ", output.TreeEnd}

// HeadedBulletTree outputs a list like:
//
// one
// ├─ two
// │ wrapped
// └─ three
var HeadedBulletTree = []string{"", output.TreeMid, output.TreeLink, output.TreeEnd}
var HeadedBulletTree = []string{"", output.TreeMid, output.TreeLink + " ", output.TreeEnd}

// NewBulletList returns a printable list of items prefixed with the given set of bullets.
// The set of bullets should contain four items: the bullet for the first item (e.g. ""); the
Expand Down Expand Up @@ -73,14 +72,15 @@ func (b *bulletList) MarshalOutput(format output.Format) interface{} {
}
item = b.prefix + bullet + item
} else {
bullet = b.bullets[1]
bullet = b.bullets[1] + " "
continuation := indent + b.bullets[2] + " "
if i == len(b.items)-1 {
bullet = b.bullets[3] // this is the last item
bullet = b.bullets[3] + " " // this is the last item
continuation = " "
}
wrapped := colorize.Wrap(item, termutils.GetWidth()-len(indent), true, continuation).String()
item = fmt.Sprintf("%s%s %s", indent, bullet, wrapped)
maxWidth := termutils.GetWidth() - len(indent) - len(bullet)
wrapped := colorize.Wrap(item, maxWidth, true, continuation).String()
item = indent + bullet + wrapped
}
out[i] = item
}
Expand Down

0 comments on commit 55a07c0

Please sign in to comment.