@@ -12,6 +12,7 @@ import (
1212 "github.com/gookit/cliui/interact/backend"
1313 "github.com/gookit/cliui/interact/backend/plain"
1414 "github.com/gookit/color"
15+ "github.com/gookit/goutil/strutil"
1516 "golang.org/x/term"
1617)
1718
@@ -97,6 +98,7 @@ func (s *Session) Render(view backend.View) error {
9798 }
9899 }
99100
101+ currentRows := viewRows (view )
100102 renderedLines := len (view .Lines )
101103 if s .rendered > renderedLines {
102104 renderedLines = s .rendered
@@ -113,7 +115,7 @@ func (s *Session) Render(view backend.View) error {
113115 }
114116
115117 if view .CursorRow >= 0 && view .CursorRow < len (view .Lines ) {
116- moveUp := renderedLines - 1 - view . CursorRow
118+ moveUp := currentRows - 1 - cursorScreenRow ( view )
117119 if moveUp > 0 {
118120 fmt .Fprintf (s .out , "\x1B [%dA" , moveUp )
119121 }
@@ -123,10 +125,36 @@ func (s *Session) Render(view backend.View) error {
123125 }
124126 }
125127
126- s .rendered = len ( view . Lines )
128+ s .rendered = currentRows
127129 return nil
128130}
129131
132+ func viewRows (view backend.View ) int {
133+ if view .Width <= 0 {
134+ return len (view .Lines )
135+ }
136+
137+ rows := 0
138+ for _ , line := range view .Lines {
139+ width := strutil .TextWidth (color .ClearTag (line ))
140+ rows += 1 + (width - 1 )/ view .Width
141+ }
142+ return rows
143+ }
144+
145+ func cursorScreenRow (view backend.View ) int {
146+ if view .Width <= 0 {
147+ return view .CursorRow
148+ }
149+
150+ row := 0
151+ for _ , line := range view .Lines [:view .CursorRow ] {
152+ width := strutil .TextWidth (color .ClearTag (line ))
153+ row += 1 + (width - 1 )/ view .Width
154+ }
155+ return row + view .CursorColumn / view .Width
156+ }
157+
130158// ReadEvent reads and normalizes one terminal event.
131159func (s * Session ) ReadEvent (ctx context.Context ) (backend.Event , error ) {
132160 select {
0 commit comments