summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorTavian Barnes <tavianator@tavianator.com>2024-05-13 16:38:04 -0400
committerTavian Barnes <tavianator@tavianator.com>2024-05-15 10:10:20 -0400
commit5865ad257a2cdc0145042817b58d56c2ecf78c22 (patch)
treebb5079ed87f151e3a55f1bd4674c0f22c15eb121 /src
parent13da3ecfd88d61c547acb9f264af3161368137d8 (diff)
downloadbfs-5865ad257a2cdc0145042817b58d56c2ecf78c22.tar.xz
bar: Make SIGWINCH move the cursor out of the bar
Diffstat (limited to 'src')
-rw-r--r--src/bar.c39
1 files changed, 16 insertions, 23 deletions
diff --git a/src/bar.c b/src/bar.c
index 5e19740..6eba78f 100644
--- a/src/bar.c
+++ b/src/bar.c
@@ -136,22 +136,24 @@ static char *ass_itoa(char *str, unsigned int n) {
/** Update the size of the scrollable region. */
static int bfs_bar_resize(struct bfs_bar *bar) {
- char esc_seq[12 + ITOA_DIGITS];
-
- char *cur = stpcpy(esc_seq,
- "\0337" // DECSC: Save cursor
- "\033[;" // DECSTBM: Set scrollable region
- );
+ static const char PREFIX[] =
+ "\033D" // IND: Line feed, possibly scrolling
+ "\033[1A" // CUU: Move cursor up 1 row
+ "\0337" // DECSC: Save cursor
+ "\033[;"; // DECSTBM: Set scrollable region
+ static const char SUFFIX[] =
+ "r" // (end of DECSTBM)
+ "\0338" // DECRC: Restore the cursor
+ "\033[J"; // ED: Erase display from cursor to end
+
+ char esc_seq[sizeof(PREFIX) + ITOA_DIGITS + sizeof(SUFFIX)];
// DECSTBM takes the height as the second argument
- unsigned int height = load(&bar->height, relaxed);
- cur = ass_itoa(cur, height - 1);
+ unsigned int height = load(&bar->height, relaxed) - 1;
- cur = stpcpy(cur,
- "r" // DECSTBM
- "\0338" // DECRC: Restore the cursor
- "\033[J" // ED: Erase display from cursor to end
- );
+ char *cur = stpcpy(esc_seq, PREFIX);
+ cur = ass_itoa(cur, height);
+ cur = stpcpy(cur, SUFFIX);
return bfs_bar_write(bar, esc_seq, cur - esc_seq);
}
@@ -251,16 +253,7 @@ struct bfs_bar *bfs_bar_show(void) {
sigaction(SIGWINCH, &sa, NULL);
#endif
- unsigned int height = load(&the_bar.height, relaxed);
- bfs_bar_printf(&the_bar,
- "\n" // Make space for the bar
- "\0337" // DECSC: Save cursor
- "\033[;%ur" // DECSTBM: Set scrollable region
- "\0338" // DECRC: Restore cursor
- "\033[1A", // CUU: Move cursor up 1 row
- height - 1
- );
-
+ bfs_bar_resize(&the_bar);
return &the_bar;
put: