Skip to content

Commit

Permalink
Other fixes to display, better support for color
Browse files Browse the repository at this point in the history
  • Loading branch information
maxlandon committed Aug 15, 2023
1 parent d03da80 commit 5d04c38
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 14 deletions.
37 changes: 26 additions & 11 deletions internal/color/color.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,32 @@ func Fmt(color string) string {
return SGRStart + color + SGREnd
}

// Trim accepts a string including arbitrary escaped sequences at arbitrary
// index positions, and returns the first 'n' printable characters in this
// string, including all escape codes found between and immediately around
// those characters (including surrounding 1st and 80th ones).
func Trim(input string, maxPrintableLength int) string {
if len(input) < maxPrintableLength {
return input
}

// Find all escape sequences in the input
escapeIndices := re.FindAllStringIndex(input, -1)

// Iterate over escape sequences to find the
// last escape index within maxPrintableLength
for _, indices := range escapeIndices {
if indices[0] <= maxPrintableLength {
maxPrintableLength += indices[1] - indices[0]
} else {
break
}
}

// Determine the end index for limiting printable content
return input[:maxPrintableLength]
}

// UnquoteRC removes the `\e` escape used in readline .inputrc
// configuration values and replaces it with the printable escape.
func UnquoteRC(color string) string {
Expand Down Expand Up @@ -173,14 +199,3 @@ var re = regexp.MustCompile(ansi)
func Strip(str string) string {
return re.ReplaceAllString(str, "")
}

// wrong: reapplies fg/bg escapes regardless of the string passed.
// Users should be in charge of applying any effect as they wish.
// func SGR(color string, fg bool) string {
// if fg {
// return SGRStart + FgColorStart + color + SGREnd
// // return SGRStart + color + SGREnd
// }
//
// return SGRStart + BgColorStart + color + SGREnd
// }
13 changes: 10 additions & 3 deletions internal/completion/group.go
Original file line number Diff line number Diff line change
Expand Up @@ -365,8 +365,9 @@ func (g *group) trimDisplay(comp Candidate, pad, col int) (candidate, padded str
}

if comp.displayLen > maxDisplayWidth {
val = val[:maxDisplayWidth-3] + "..." // 3 dots = -3
val = g.listSep() + sanitizer.Replace(val)
val = color.Trim(val, maxDisplayWidth-4)
val += "..." // 3 dots + 1 safety space = -3
val = sanitizer.Replace(val)

return val, " "
}
Expand All @@ -380,12 +381,18 @@ func (g *group) trimDesc(val Candidate, pad int) (desc, padded string) {
return desc, padSpace(pad)
}

// We don't compare against the terminal width:
// the correct padding should have been computed
// based on the space taken by all candidates
// described by our current string.
if pad > g.maxDescAllowed {
pad = g.maxDescAllowed - val.descLen
}

// Trim the description accounting for escapes.
if val.descLen > g.maxDescAllowed && g.maxDescAllowed > 0 {
desc = desc[:g.maxDescAllowed-3] + "..."
desc = color.Trim(desc, g.maxDescAllowed-3)
desc += "..." // 3 dots = -3
desc = g.listSep() + sanitizer.Replace(desc)

return desc, ""
Expand Down

0 comments on commit 5d04c38

Please sign in to comment.