From ae75ca56e713228a839214843d344e71c11fe289 Mon Sep 17 00:00:00 2001 From: RocketQuack <202538874+Rocket-Quack@users.noreply.github.com> Date: Fri, 24 Apr 2026 12:45:24 +0200 Subject: [PATCH] fix(easy-docker): handle missing terminal capabilities quietly --- scripts/easy-docker/lib/app/screen.sh | 40 ++++++++++++++---- tests/easy-docker/25_screen_terminal.bats | 49 +++++++++++++++++++++++ tests/easy-docker/test_helper.bash | 20 +++++++++ 3 files changed, 102 insertions(+), 7 deletions(-) create mode 100755 tests/easy-docker/25_screen_terminal.bats diff --git a/scripts/easy-docker/lib/app/screen.sh b/scripts/easy-docker/lib/app/screen.sh index ce293994..8a6d1344 100755 --- a/scripts/easy-docker/lib/app/screen.sh +++ b/scripts/easy-docker/lib/app/screen.sh @@ -1,19 +1,45 @@ #!/usr/bin/env bash ALT_SCREEN_ACTIVE=0 +CURSOR_HIDDEN=0 + +stdout_is_terminal() { + [ -t 1 ] +} + +run_tput_quietly() { + tput "$@" 2>/dev/null +} enter_alt_screen() { - if [ -t 1 ] && command_exists tput; then - tput smcup || true - tput civis || true + if ! stdout_is_terminal || ! command_exists tput; then + return 0 + fi + + if run_tput_quietly smcup; then ALT_SCREEN_ACTIVE=1 fi + + if run_tput_quietly civis; then + CURSOR_HIDDEN=1 + fi + + return 0 } leave_alt_screen() { - if [ "${ALT_SCREEN_ACTIVE}" = "1" ] && command_exists tput; then - tput cnorm || true - tput rmcup || true - ALT_SCREEN_ACTIVE=0 + if command_exists tput; then + if [ "${CURSOR_HIDDEN}" = "1" ]; then + run_tput_quietly cnorm || true + fi + + if [ "${ALT_SCREEN_ACTIVE}" = "1" ]; then + run_tput_quietly rmcup || true + fi fi + + CURSOR_HIDDEN=0 + ALT_SCREEN_ACTIVE=0 + + return 0 } diff --git a/tests/easy-docker/25_screen_terminal.bats b/tests/easy-docker/25_screen_terminal.bats new file mode 100755 index 00000000..57ce2775 --- /dev/null +++ b/tests/easy-docker/25_screen_terminal.bats @@ -0,0 +1,49 @@ +#!/usr/bin/env bats + +load 'test_helper.bash' + +setup() { + easy_docker_test_begin + easy_docker_test_source_screen_modules_with_tty_stdout +} + +teardown() { + easy_docker_test_end +} + +@test "enter_alt_screen suppresses tput stderr when terminfo is unavailable" { + easy_docker_test_write_bin_command tput \ + 'echo '"'"'tput: unknown terminal "xterm-256color"'"'"' >&2' \ + 'exit 1' + easy_docker_test_prepend_bin_dir + + run enter_alt_screen + [ "${status}" -eq 0 ] + [ -z "${output}" ] +} + +@test "enter_alt_screen and leave_alt_screen track successful terminal state" { + local log_file="" + local expected_log="" + + log_file="${EASY_DOCKER_TEST_TMPDIR}/tput.log" + + easy_docker_test_write_bin_command tput \ + "printf '%s\\n' \"\${1:-}\" >>\"${log_file}\"" \ + 'exit 0' + easy_docker_test_prepend_bin_dir + + enter_alt_screen + [ "${ALT_SCREEN_ACTIVE}" = "1" ] + [ "${CURSOR_HIDDEN}" = "1" ] + + leave_alt_screen + [ "${ALT_SCREEN_ACTIVE}" = "0" ] + [ "${CURSOR_HIDDEN}" = "0" ] + + expected_log=$'smcup\ncivis\ncnorm\nrmcup' + + run cat "${log_file}" + [ "${status}" -eq 0 ] + [ "${output}" = "${expected_log}" ] +} diff --git a/tests/easy-docker/test_helper.bash b/tests/easy-docker/test_helper.bash index 66946327..5d5cb6f5 100755 --- a/tests/easy-docker/test_helper.bash +++ b/tests/easy-docker/test_helper.bash @@ -136,6 +136,26 @@ easy_docker_test_source_gum_modules() { source "${repo_root}/scripts/easy-docker/lib/install/gum/ensure.sh" } +easy_docker_test_source_screen_modules() { + local repo_root="" + + repo_root="$(easy_docker_test_repo_root)" + + easy_docker_test_source_common_modules + + # shellcheck source=scripts/easy-docker/lib/app/screen.sh + source "${repo_root}/scripts/easy-docker/lib/app/screen.sh" +} + +easy_docker_test_source_screen_modules_with_tty_stdout() { + easy_docker_test_source_screen_modules + + # shellcheck disable=SC2317 + stdout_is_terminal() { + return 0 + } +} + easy_docker_test_create_repo_sandbox() { local sandbox_name="${1}" local sandbox_root=""