terminal-util: add helper that adjust terminal width/height from data acquired via ANSI sequences
This commit is contained in:
@@ -2072,3 +2072,36 @@ finish:
|
||||
RET_GATHER(r, RET_NERRNO(tcsetattr(input_fd, TCSADRAIN, &old_termios)));
|
||||
return r;
|
||||
}
|
||||
|
||||
int terminal_fix_size(int input_fd, int output_fd) {
|
||||
unsigned rows, columns;
|
||||
int r;
|
||||
|
||||
/* Tries to update the current terminal dimensions to the ones reported via ANSI sequences */
|
||||
|
||||
r = terminal_verify_same(input_fd, output_fd);
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
struct winsize ws = {};
|
||||
if (ioctl(output_fd, TIOCGWINSZ, &ws) < 0)
|
||||
return log_debug_errno(errno, "Failed to query terminal dimensions, ignoring: %m");
|
||||
|
||||
r = terminal_get_size_by_dsr(input_fd, output_fd, &rows, &columns);
|
||||
if (r < 0)
|
||||
return log_debug_errno(r, "Failed to acquire terminal dimensions via ANSI sequences, not adjusting terminal dimensions: %m");
|
||||
|
||||
if (ws.ws_row == rows && ws.ws_col == columns) {
|
||||
log_debug("Terminal dimensions reported via ANSI sequences match currently set terminal dimensions, not changing.");
|
||||
return 0;
|
||||
}
|
||||
|
||||
ws.ws_col = columns;
|
||||
ws.ws_row = rows;
|
||||
|
||||
if (ioctl(output_fd, TIOCSWINSZ, &ws) < 0)
|
||||
return log_debug_errno(errno, "Failed to update terminal dimensions, ignoring: %m");
|
||||
|
||||
log_debug("Fixed terminal dimensions to %ux%u based on ANSI sequence information.", columns, rows);
|
||||
return 1;
|
||||
}
|
||||
|
||||
@@ -296,3 +296,5 @@ void termios_disable_echo(struct termios *termios);
|
||||
|
||||
int get_default_background_color(double *ret_red, double *ret_green, double *ret_blue);
|
||||
int terminal_get_size_by_dsr(int input_fd, int output_fd, unsigned *ret_rows, unsigned *ret_columns);
|
||||
|
||||
int terminal_fix_size(int input_fd, int output_fd);
|
||||
|
||||
@@ -197,6 +197,18 @@ TEST(terminal_get_size_by_dsr) {
|
||||
}
|
||||
}
|
||||
|
||||
TEST(terminal_fix_size) {
|
||||
int r;
|
||||
|
||||
r = terminal_fix_size(STDIN_FILENO, STDOUT_FILENO);
|
||||
if (r < 0)
|
||||
log_warning_errno(r, "Failed to fix terminal size: %m");
|
||||
else if (r == 0)
|
||||
log_notice("Not fixing terminal size, nothing to do.");
|
||||
else
|
||||
log_notice("Fixed terminal size.");
|
||||
}
|
||||
|
||||
static void test_get_color_mode_with_env(const char *key, const char *val, ColorMode expected) {
|
||||
ASSERT_OK(setenv(key, val, true));
|
||||
reset_terminal_feature_caches();
|
||||
|
||||
Reference in New Issue
Block a user