From a503371c572cf1f2ef7411432542eb4baf4638fe Mon Sep 17 00:00:00 2001 From: Fabian Homborg Date: Fri, 12 Feb 2021 18:25:21 +0100 Subject: [PATCH] termsize: Default to 80x24 when the terminal says 0 again This was lost in 6bdbe732e40c2e325aa15fcf0f28ad0dedb3a551..c7160d7cb4970c2a03df34547f357721cb5e88db. Note that we only print a term-support flog message for now, the warning seems a bit much. Fixes #7709. --- src/termsize.cpp | 9 +++++++++ tests/pexpects/terminal.py | 15 +++++++++++++++ 2 files changed, 24 insertions(+) create mode 100644 tests/pexpects/terminal.py diff --git a/src/termsize.cpp b/src/termsize.cpp index cf3591adf..a4ce9255d 100644 --- a/src/termsize.cpp +++ b/src/termsize.cpp @@ -16,6 +16,15 @@ static maybe_t read_termsize_from_tty() { #ifdef HAVE_WINSIZE struct winsize winsize = {0, 0, 0, 0}; if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &winsize) >= 0) { + // 0 values are unusable, fall back to the default instead. + if (winsize.ws_col == 0) { + FLOGF(term_support, L"Terminal has 0 columns, falling back to default width"); + winsize.ws_col = termsize_t::DEFAULT_WIDTH; + } + if (winsize.ws_row == 0) { + FLOGF(term_support, L"Terminal has 0 rows, falling back to default height"); + winsize.ws_row = termsize_t::DEFAULT_HEIGHT; + } result = termsize_t{winsize.ws_col, winsize.ws_row}; } #endif diff --git a/tests/pexpects/terminal.py b/tests/pexpects/terminal.py new file mode 100644 index 000000000..cdcfeb65f --- /dev/null +++ b/tests/pexpects/terminal.py @@ -0,0 +1,15 @@ +#!/usr/bin/env python3 +from pexpect_helper import SpawnedProc + +# Set a 0 terminal size +sp = SpawnedProc(args=["-d", "term-support"], dimensions=(0,0)) +sendline, expect_prompt, expect_str = sp.sendline, sp.expect_prompt, sp.expect_str + +expect_prompt() +# Now confirm it defaulted to 80x24 +sendline("echo $COLUMNS $LINES") +expect_str("80 24") +expect_str("term-support: Terminal has 0 columns, falling back to default width") +expect_str("term-support: Terminal has 0 rows, falling back to default height") +expect_prompt() +