Skip to content

Commit 6a6d23e

Browse files
committed
slash: use CUB to move cursor back
1 parent 2a05d97 commit 6a6d23e

1 file changed

Lines changed: 27 additions & 15 deletions

File tree

src/slash.c

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -911,6 +911,18 @@ static bool slash_history_previous(struct slash *slash)
911911
}
912912

913913
/* Line editing */
914+
static int slash_cursor_back(struct slash *slash, size_t n)
915+
{
916+
/* If we need to move more than 3 colums, CUB uses fewer bytes */
917+
if (n > 3) {
918+
slash_printf(slash, ESCAPE("%zuD"), n);
919+
} else {
920+
while (n--)
921+
slash_putchar(slash, '\b');
922+
}
923+
924+
return 0;
925+
}
914926
int slash_refresh(struct slash *slash)
915927
{
916928
const char *esc = ESCAPE("K");
@@ -927,21 +939,20 @@ int slash_refresh(struct slash *slash)
927939
}
928940

929941
if (slash->refresh_buffer) {
930-
while (slash->cursor_screen > 0) {
931-
slash_putchar(slash, '\b');
932-
slash->cursor_screen--;
933-
}
942+
slash_cursor_back(slash, slash->cursor_screen);
943+
slash->cursor_screen = 0;
934944
slash->refresh_buffer = false;
935945
}
936946

937-
while (slash->cursor_screen != slash->cursor) {
938-
if (slash->cursor_screen < slash->cursor) {
939-
slash_putchar(slash, slash->buffer[slash->cursor_screen]);
940-
slash->cursor_screen++;
941-
} else if (slash->cursor_screen > slash->cursor) {
942-
slash_putchar(slash, '\b');
943-
slash->cursor_screen--;
944-
}
947+
while (slash->cursor_screen < slash->cursor) {
948+
slash_putchar(slash, slash->buffer[slash->cursor_screen]);
949+
slash->cursor_screen++;
950+
}
951+
952+
if (slash->cursor_screen > slash->cursor) {
953+
slash_cursor_back(slash,
954+
slash->cursor_screen - slash->cursor);
955+
slash->cursor_screen = slash->cursor;
945956
}
946957

947958
if (slash->length_screen != slash->length) {
@@ -956,9 +967,10 @@ int slash_refresh(struct slash *slash)
956967
return -1;
957968
}
958969

959-
while (slash->cursor_screen > slash->cursor) {
960-
slash_putchar(slash, '\b');
961-
slash->cursor_screen--;
970+
if (slash->cursor_screen > slash->cursor) {
971+
slash_cursor_back(slash,
972+
slash->cursor_screen - slash->cursor);
973+
slash->cursor_screen = slash->cursor;
962974
}
963975

964976
slash->length_screen = slash->length;

0 commit comments

Comments
 (0)