Skip to content

Commit ca6a580

Browse files
committed
fix: prevent cursor rendering conflicts by simplifying cursor positioning logic
1 parent 6a19bb0 commit ca6a580

File tree

3 files changed

+12
-48
lines changed

3 files changed

+12
-48
lines changed

src/highlighters.cc

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
#include "regex.hh"
1717
#include "register_manager.hh"
1818
#include "string.hh"
19-
#include "terminal_ui.hh"
2019
#include "utf8.hh"
2120
#include "utf8_iterator.hh"
2221
#include "window.hh"
@@ -1291,20 +1290,11 @@ void highlight_selections(HighlightContext context, DisplayBuffer& display_buffe
12911290
highlight_range(display_buffer, begin, end, false,
12921291
apply_face(sel_faces[primary ? 0 : 1]));
12931292
}
1294-
// Check if we should skip drawing the primary cursor (for terminal cursor native mode)
1295-
const bool skip_primary_cursor = TerminalUI::has_instance() &&
1296-
TerminalUI::instance().is_cursor_native();
1297-
12981293
for (size_t i = 0; i < selections.size(); ++i)
12991294
{
13001295
auto& sel = selections[i];
13011296
const BufferCoord coord = sel.cursor();
13021297
const bool primary = (i == selections.main_index());
1303-
1304-
// Skip drawing primary cursor if terminal_cursor_native is enabled
1305-
if (primary && skip_primary_cursor)
1306-
continue;
1307-
13081298
const bool eol = buffer[coord.line].length() - 1 == coord.column;
13091299
highlight_range(display_buffer, coord, buffer.char_next(coord), false,
13101300
apply_face(sel_faces[2 + (eol ? 2 : 0) + (primary ? 0 : 1)]));

src/terminal_ui.cc

Lines changed: 12 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -539,29 +539,10 @@ void TerminalUI::redraw(bool force)
539539
format_with(writer, "\033[{};{}H", (int)c.line + 1, (int)c.column + 1);
540540
};
541541

542-
DisplayCoord target_pos;
543542
if (m_cursor.mode == CursorMode::Prompt)
544-
target_pos = {m_status_on_top ? 0 : m_dimensions.line, m_cursor.coord.column};
543+
set_cursor_pos({m_status_on_top ? 0 : m_dimensions.line, m_cursor.coord.column});
545544
else
546-
target_pos = m_cursor.coord + content_line_offset();
547-
548-
if (m_terminal_cursor_native)
549-
{
550-
// Always position cursor in native mode to ensure it's correct after screen updates
551-
set_cursor_pos(target_pos);
552-
553-
// Only send show cursor command when cursor changes or on force refresh
554-
if (m_cursor.mode != m_prev_cursor.mode || m_cursor.coord != m_prev_cursor.coord || force)
555-
{
556-
format_with(writer, "\033[?25h"); // ensure cursor is visible
557-
m_prev_cursor = m_cursor;
558-
}
559-
}
560-
else
561-
{
562-
// Always position cursor (original behavior)
563-
set_cursor_pos(target_pos);
564-
}
545+
set_cursor_pos(m_cursor.coord + content_line_offset());
565546
}
566547

567548
void TerminalUI::set_cursor(CursorMode mode, DisplayCoord coord)
@@ -1504,17 +1485,13 @@ void TerminalUI::set_resize_pending()
15041485

15051486
void TerminalUI::setup_terminal()
15061487
{
1507-
const char* cursor_cmd = instance().m_terminal_cursor_native ? "\033[?25h" : "\033[?25l";
1508-
15091488
write(STDOUT_FILENO,
15101489
"\033[?1049h" // enable alternative screen buffer
15111490
"\033[?1004h" // enable focus notify
15121491
"\033[>4;1m" // request CSI u style key reporting
15131492
"\033[>5u" // kitty progressive enhancement - report shifted key codes
15141493
"\033[22t" // save the current window title
1515-
);
1516-
write(STDOUT_FILENO, cursor_cmd); // show or hide cursor based on mode
1517-
write(STDOUT_FILENO,
1494+
"\033[?25l" // hide cursor
15181495
"\033=" // set application keypad mode, so the keypad keys send unique codes
15191496
"\033[?2004h" // force enable bracketed-paste events
15201497
);
@@ -1524,13 +1501,7 @@ void TerminalUI::restore_terminal()
15241501
{
15251502
write(STDOUT_FILENO,
15261503
"\033>"
1527-
);
1528-
1529-
// Only restore cursor visibility if it was hidden (non-native mode)
1530-
if (not instance().m_terminal_cursor_native)
1531-
write(STDOUT_FILENO, "\033[?25h");
1532-
1533-
write(STDOUT_FILENO,
1504+
"\033[?25h"
15341505
"\033[23t"
15351506
"\033[<u"
15361507
"\033[>4;0m"
@@ -1604,7 +1575,14 @@ void TerminalUI::set_ui_options(const Options& options)
16041575

16051576
m_padding_char = find("terminal_padding_char").map([](StringView s) { return s.column_length() < 1 ? ' ' : s[0_char]; }).value_or(Codepoint{'~'});
16061577
m_padding_fill = find("terminal_padding_fill").map(to_bool).value_or(false);
1607-
m_terminal_cursor_native = find("terminal_cursor_native").map(to_bool).value_or(false);
1578+
1579+
bool new_cursor_native = find("terminal_cursor_native").map(to_bool).value_or(false);
1580+
if (new_cursor_native != m_terminal_cursor_native)
1581+
{
1582+
m_terminal_cursor_native = new_cursor_native;
1583+
// Emit cursor visibility command when the option changes
1584+
write(STDOUT_FILENO, m_terminal_cursor_native ? "\033[?25h" : "\033[?25l");
1585+
}
16081586

16091587
m_info_max_width = find("terminal_info_max_width").map(str_to_int_ifp).value_or(0);
16101588
}

src/terminal_ui.hh

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,6 @@ public:
6161
static void restore_terminal();
6262

6363
void suspend();
64-
65-
bool is_cursor_native() const { return m_terminal_cursor_native; }
6664

6765
struct Rect
6866
{
@@ -136,8 +134,6 @@ private:
136134
CursorMode mode;
137135
DisplayCoord coord;
138136
} m_cursor;
139-
140-
struct Cursor m_prev_cursor;
141137

142138
FDWatcher m_stdin_watcher;
143139
OnKeyCallback m_on_key;

0 commit comments

Comments
 (0)