Skip to content

Commit 8863d8a

Browse files
authored
Merge pull request #833 from yorukot/develop
fix: Use ansi.TruncateLeft and remove buggy cutLeft
2 parents 49dd522 + 8884da2 commit 8863d8a

File tree

1 file changed

+4
-45
lines changed

1 file changed

+4
-45
lines changed

src/pkg/string_function/overplace.go

Lines changed: 4 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@
33
// These code is from the https://github.com/charmbracelet/lipgloss/pull/102
44
// Thanks a lot!!!!!
55

6+
// Edit - cutLeft has been replaced with charmansi.TruncateLeft. See https://github.com/charmbracelet/lipgloss/pull/102#issuecomment-2900110821
7+
68
// =======================================================================================================================
79
package stringfunction
810

911
import (
10-
"bytes"
1112
"strings"
1213

13-
charmansi "github.com/charmbracelet/x/exp/term/ansi"
14-
"github.com/mattn/go-runewidth"
14+
charmansi "github.com/charmbracelet/x/ansi"
1515
ansi "github.com/muesli/reflow/ansi"
1616
"github.com/muesli/reflow/truncate"
1717
"github.com/muesli/termenv"
@@ -100,7 +100,7 @@ func PlaceOverlay(x, y int, fg, bg string, opts ...WhitespaceOption) string {
100100
b.WriteString(fgLine)
101101
pos += ansi.PrintableRuneWidth(fgLine)
102102

103-
right := cutLeft(bgLine, pos)
103+
right := charmansi.TruncateLeft(bgLine, pos, "")
104104
bgWidth = ansi.PrintableRuneWidth(bgLine)
105105
rightWidth := ansi.PrintableRuneWidth(right)
106106
if rightWidth <= bgWidth-pos {
@@ -113,47 +113,6 @@ func PlaceOverlay(x, y int, fg, bg string, opts ...WhitespaceOption) string {
113113
return b.String()
114114
}
115115

116-
// cutLeft cuts printable characters from the left.
117-
// This function is heavily based on muesli's ansi and truncate packages.
118-
func cutLeft(s string, cutWidth int) string {
119-
var (
120-
pos int
121-
isAnsi bool
122-
ab bytes.Buffer
123-
b bytes.Buffer
124-
)
125-
for _, c := range s {
126-
var w int
127-
if c == ansi.Marker || isAnsi {
128-
isAnsi = true
129-
ab.WriteRune(c)
130-
if ansi.IsTerminator(c) {
131-
isAnsi = false
132-
if bytes.HasSuffix(ab.Bytes(), []byte("[0m")) {
133-
ab.Reset()
134-
}
135-
}
136-
} else {
137-
w = runewidth.RuneWidth(c)
138-
}
139-
140-
if pos >= cutWidth {
141-
if b.Len() == 0 {
142-
if ab.Len() > 0 {
143-
b.Write(ab.Bytes())
144-
}
145-
if pos-cutWidth > 1 {
146-
b.WriteByte(' ')
147-
continue
148-
}
149-
}
150-
b.WriteRune(c)
151-
}
152-
pos += w
153-
}
154-
return b.String()
155-
}
156-
157116
func clamp(v, lower, upper int) int {
158117
return min(max(v, lower), upper)
159118
}

0 commit comments

Comments
 (0)