3
3
// These code is from the https://github.com/charmbracelet/lipgloss/pull/102
4
4
// Thanks a lot!!!!!
5
5
6
+ // Edit - cutLeft has been replaced with charmansi.TruncateLeft. See https://github.com/charmbracelet/lipgloss/pull/102#issuecomment-2900110821
7
+
6
8
// =======================================================================================================================
7
9
package stringfunction
8
10
9
11
import (
10
- "bytes"
11
12
"strings"
12
13
13
- charmansi "github.com/charmbracelet/x/exp/term/ansi"
14
- "github.com/mattn/go-runewidth"
14
+ charmansi "github.com/charmbracelet/x/ansi"
15
15
ansi "github.com/muesli/reflow/ansi"
16
16
"github.com/muesli/reflow/truncate"
17
17
"github.com/muesli/termenv"
@@ -100,7 +100,7 @@ func PlaceOverlay(x, y int, fg, bg string, opts ...WhitespaceOption) string {
100
100
b .WriteString (fgLine )
101
101
pos += ansi .PrintableRuneWidth (fgLine )
102
102
103
- right := cutLeft (bgLine , pos )
103
+ right := charmansi . TruncateLeft (bgLine , pos , "" )
104
104
bgWidth = ansi .PrintableRuneWidth (bgLine )
105
105
rightWidth := ansi .PrintableRuneWidth (right )
106
106
if rightWidth <= bgWidth - pos {
@@ -113,47 +113,6 @@ func PlaceOverlay(x, y int, fg, bg string, opts ...WhitespaceOption) string {
113
113
return b .String ()
114
114
}
115
115
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
-
157
116
func clamp (v , lower , upper int ) int {
158
117
return min (max (v , lower ), upper )
159
118
}
0 commit comments