Skip to content

Commit e357139

Browse files
committed
fix: ensure we flush pending data and hide cursor when synchronized updates are enabled
1 parent 99b1399 commit e357139

1 file changed

Lines changed: 25 additions & 10 deletions

File tree

terminal_screen.go

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -182,17 +182,32 @@ func (s *TerminalScreen) Flush() error {
182182
var buf bytes.Buffer
183183
buf.Grow(s.buf.Len())
184184

185-
if s.syncUpdates && s.buf.Len() > 0 {
186-
buf.Grow(len(ansi.SetModeSynchronizedOutput) + len(ansi.ResetModeSynchronizedOutput))
187-
188-
// If synchronized updates are enabled, we need to wrap the output in
189-
// the appropriate control sequences to ensure that the terminal treats
190-
// it as a single atomic update. This is necessary to prevent flickering
191-
// and other visual artifacts that can occur when multiple updates are sent
192-
// separately.
193-
buf.WriteString(ansi.SetModeSynchronizedOutput)
185+
if s.buf.Len() > 0 {
186+
if s.syncUpdates {
187+
buf.Grow(len(ansi.SetModeSynchronizedOutput) + len(ansi.ResetModeSynchronizedOutput))
188+
189+
// If synchronized updates are enabled, we need to wrap the output in
190+
// the appropriate control sequences to ensure that the terminal treats
191+
// it as a single atomic update. This is necessary to prevent flickering
192+
// and other visual artifacts that can occur when multiple updates are sent
193+
// separately.
194+
buf.WriteString(ansi.SetModeSynchronizedOutput)
195+
} else if s.cursor != nil && !s.cursor.Hidden {
196+
buf.Grow(len(ansi.HideCursor) + len(ansi.ShowCursor))
197+
198+
// If synchronized updates are not enabled, we need to ensure that
199+
// the cursor is hidden before writing any output to prevent
200+
// unwanted cursor visual artifacts.
201+
buf.WriteString(ansi.HideCursor)
202+
}
203+
194204
buf.Write(s.buf.Bytes())
195-
buf.WriteString(ansi.ResetModeSynchronizedOutput)
205+
206+
if s.syncUpdates {
207+
buf.WriteString(ansi.ResetModeSynchronizedOutput)
208+
} else if s.cursor != nil && !s.cursor.Hidden {
209+
buf.WriteString(ansi.ShowCursor)
210+
}
196211
}
197212

198213
_, err := s.w.Write(buf.Bytes())

0 commit comments

Comments
 (0)