From 2a5a13109aa050fbb7dcae0984772027c5cecc39 Mon Sep 17 00:00:00 2001 From: Benjamin Lupton Date: Wed, 3 Nov 2021 08:46:47 +0800 Subject: [PATCH] ask, choose-menu, choose-option: remove `--main` now replaced by `env NO_ALT_TTY=yes` or `env ALT_TTY=no` First introduced in: https://github.com/bevry/dorothy/commit/6f841ad89c3e8b8e818fb379dcc746ed0a8a4ed2 Closes: https://github.com/bevry/dorothy/issues/27 --- commands/ask | 2 -- commands/choose-menu | 1 - commands/choose-option | 6 ----- sources/tty.bash | 53 ++++++++++++++++++++++++++++-------------- 4 files changed, 36 insertions(+), 26 deletions(-) diff --git a/commands/ask b/commands/ask index 094849f58..594c3e6f9 100755 --- a/commands/ask +++ b/commands/ask @@ -19,7 +19,6 @@ if is-help "$@"; then Provide [--required] to specify that the prompt should not continue until a value is provided. Provide [--timeout=...] to specify a custom timeout value in seconds. Provide [--flag=... -- ...args] to specify a flag to search the arguments for, to set a default value. - Provide [--main] to not create an alternative TTY shell when promting for questions. EOF exit 22 # Invalid argument fi @@ -130,7 +129,6 @@ function validate() { choice="$(choose-option \ --question="$option_question" \ --timeout="$option_timeout" \ - --main="$option_main" \ --label \ -- "${choices[@]}")" || ec="$?" diff --git a/commands/choose-menu b/commands/choose-menu index 3905bde25..9d78ffb5d 100755 --- a/commands/choose-menu +++ b/commands/choose-menu @@ -26,7 +26,6 @@ if is-help-separator "$@"; then Provide [--question=...] to specify the question that the prompt will be answering. Provide [--multi] to specify that multiple menu items should be able to be selected. Provide [--timeout=...] to specify a custom timeout value in seconds. - Provide [--main] to not create an alternative TTY shell when promting for questions. EOF exit 22 # Invalid argument fi diff --git a/commands/choose-option b/commands/choose-option index aaf2a8183..2c7b292f5 100755 --- a/commands/choose-option +++ b/commands/choose-option @@ -22,7 +22,6 @@ if is-help-separator "$@"; then [--required] \\ [--multi] \\ [--confirm] \\ - [--main] \\ -- <[value]...> If you wish to show a question above the menu: @@ -60,9 +59,6 @@ if is-help-separator "$@"; then If you wish to return the visual use: --label --return='\$visual' -- <[value, label]...> ^ the value is eval'd - - If you wish to not use an alternative TTY shell, use: - --main -- <[value, label]...> EOF exit 22 # Invalid argument fi @@ -221,7 +217,6 @@ function act() { results="$(choose-menu \ --question="$option_question" \ --multi="$option_multi" \ - --main="$option_main" \ --required="$option_required" \ --timeout="$option_timeout" \ -- "${filtered_visuals[@]}")" || ec="$?" @@ -262,7 +257,6 @@ function act() { confirmed="$(choose-option \ --timeout=60 \ --question="$option_question" \ - --main="$option_main" \ --required="$option_required" \ --label \ -- \ diff --git a/sources/tty.bash b/sources/tty.bash index 21b470564..f4b2df7d2 100644 --- a/sources/tty.bash +++ b/sources/tty.bash @@ -4,53 +4,72 @@ # These create a new TTY that can be cleared without affecting the prior TTY. # https://unix.stackexchange.com/a/668615/50703 -tty_start () { - tput smcup > /dev/tty - tput cup 0 0 > /dev/tty +tty_start() { + tput smcup >/dev/tty + tput cup 0 0 >/dev/tty } -tty_clear () { - tput clear > /dev/tty # also resets cursor to top +tty_clear() { + tput clear >/dev/tty # also resets cursor to top } -tty_finish () { +tty_finish() { # `tput rmcup` does not persist stderr, so for failure/stderr dumps, use `sleep 10` to ensure sterr is visible for long enough to be noticed before wiped. - tput rmcup > /dev/tty + tput rmcup >/dev/tty } -tty_auto () { +tty_auto() { tty_start trap tty_finish EXIT } +# if alt tty is disabled however, then disable it +if is-affirmative "${NO_ALT_TTY-}" || test "$(echo-exit-code is-affirmative "${ALT_TTY-}")" -eq 1; then + tty_start() { + return + } + tty_clear() { + return + } + tty_finish() { + return + } + tty_auto() { + return + } +fi + # ------------------------------------- # The below methods are only useful if the y is below $LINES # In other words, if the scroll buffer has not been and will not be reached. # As such, these are prone to failure, and you should use the earlier methods instead. # https://stackoverflow.com/a/69138082/130638 -tty_get_y_x () { +tty_get_y_x() { local y x - IFS='[;' read -srd R -p $'\e[6n' _ y x < /dev/tty + IFS='[;' read -srd R -p $'\e[6n' _ y x /dev/tty + echo -en "\e[${y};${x}H" >/dev/tty } -tty_erase_to_end () { - echo -en "\e[J" > /dev/tty +tty_erase_to_end() { + echo -en "\e[J" >/dev/tty } -tty_erase_from_y_x () { +tty_erase_from_y_x() { tty_set_y_x "${1-}" "${2-}" tty_erase_to_end }