diff options
author | lns <matzeton@googlemail.com> | 2019-12-26 02:07:14 +0100 |
---|---|---|
committer | lns <matzeton@googlemail.com> | 2019-12-26 02:07:14 +0100 |
commit | 8158ffb717d9f588a34a3adbd4adc95077af8622 (patch) | |
tree | 403e73e5862ba79fb8c19fafd10229a87ca3116d | |
parent | d85f361b1cb7f6b55b41a3224b3ffae6841e6385 (diff) |
react on terminal size changes
-rw-r--r-- | progressbar.c | 44 |
1 files changed, 41 insertions, 3 deletions
diff --git a/progressbar.c b/progressbar.c index 9683d09..281e567 100644 --- a/progressbar.c +++ b/progressbar.c @@ -5,6 +5,7 @@ #include <stdarg.h> #include <unistd.h> #include <errno.h> +#include <signal.h> #include <string.h> #include <stdbool.h> #include <fcntl.h> @@ -150,7 +151,9 @@ struct file_info { struct terminal { struct winsize dimensions; + int dimensions_changed; }; +static struct terminal term = {}; static int setup_file_info(struct file_info * const finfo, int proc_fd_fd, int proc_fdinfo_fd) { @@ -328,6 +331,20 @@ static enum unit_suffix choose_appropriate_unit(long int bytes, float * const re return NONE; } +static void fillup_remaining(struct terminal const * const term, + struct file_info * const finfo) +{ + size_t remaining_len = remaining_printable_chars(term, finfo); + + if (remaining_len) { + char spaces[remaining_len + 1]; + + memset(spaces, ' ', sizeof(spaces)); + spaces[remaining_len] = '\0'; + add_printable_buf(term, finfo, "%s", spaces); + } +} + static void prettify_with_units(long int bytes, char * const buf, size_t siz) { float unit_bytes = 0.0f; @@ -426,7 +443,7 @@ static void show_rate(struct terminal * const term, finfo->xfer_rate_history.xfer_rates[xfer_rate_index % XFER_RATES_LENGTH] = new_xfer_rate; /* do not show every updated rate; can be very annoying if values are changing e.g. between 10.0 and 9.0 */ - if (finfo->xfer_rate_history.next_index - 1 < XFER_RATES_LENGTH || + if (finfo->xfer_rate_history.next_index - 1 < XFER_RATES_LENGTH /* except right after startup */ || (finfo->xfer_rate_history.next_index - 1) % (XFER_RATES_LENGTH / 2) == 0) { finfo->xfer_rate_history.last_reported_rate = result; @@ -544,6 +561,18 @@ static int choose_filepath(struct filepath * const filepath) return 0; } +void signal_handler(int signo) +{ + switch (signo) { + case SIGWINCH: + get_terminal_dimensions(&term); + term.dimensions_changed = 1; + break; + default: + break; + } +} + int main(int argc, char ** argv) { struct filtered_dir_entries proc_pid_entries = {}; @@ -637,16 +666,25 @@ int main(int argc, char ** argv) cur->proc_fd_fd = -1; } - struct terminal term = {}; + get_terminal_dimensions(&term); + if (signal(SIGWINCH, signal_handler) == SIG_ERR) { + perror("signal"); + exit(EXIT_FAILURE); + } + loop_start(&finfo); while (!read_and_parse_fd_pos(&finfo)) { reset_terminal_output_buffer(&finfo); - get_terminal_dimensions(&term); + if (term.dimensions_changed) { + term.dimensions_changed = 0; + finfo.terminal_output.buf[0] = '\n'; + } loop_end(&finfo); show_positions(&term, &finfo); show_rate(&term, &finfo); show_progressbar(&term, &finfo); + fillup_remaining(&term, &finfo); loop_start(&finfo); int print_len = finfo.terminal_output.printable_chars + |