Skip to content

Commit

Permalink
ask, choose-menu, choose-option: remove --main
Browse files Browse the repository at this point in the history
now replaced by `env NO_ALT_TTY=yes` or `env ALT_TTY=no`

First introduced in:

6f841ad

Closes:

#27
  • Loading branch information
balupton committed Nov 3, 2021
1 parent 2f5bff6 commit 2a5a131
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 26 deletions.
2 changes: 0 additions & 2 deletions commands/ask
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -130,7 +129,6 @@ function validate() {
choice="$(choose-option \
--question="$option_question" \
--timeout="$option_timeout" \
--main="$option_main" \
--label \
-- "${choices[@]}")" || ec="$?"

Expand Down
1 change: 0 additions & 1 deletion commands/choose-menu
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 0 additions & 6 deletions commands/choose-option
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ if is-help-separator "$@"; then
[--required] \\
[--multi] \\
[--confirm] \\
[--main] \\
-- <[value]...> <value> <value>
If you wish to show a question above the menu:
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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="$?"
Expand Down Expand Up @@ -262,7 +257,6 @@ function act() {
confirmed="$(choose-option \
--timeout=60 \
--question="$option_question" \
--main="$option_main" \
--required="$option_required" \
--label \
-- \
Expand Down
53 changes: 36 additions & 17 deletions sources/tty.bash
Original file line number Diff line number Diff line change
Expand Up @@ -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 "$y"
echo "$x"
}

tty_set_y_x () {
tty_set_y_x() {
local y x
if test "$#" -eq 0; then
mapfile -t yx < <(tty_get_y_x); y="${yx[0]}"; x="${yx[1]}"
mapfile -t yx < <(tty_get_y_x)
y="${yx[0]}"
x="${yx[1]}"
else
y="${1-}"; x="${2-}"
y="${1-}"
x="${2-}"
fi
echo -en "\e[${y};${x}H" > /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
}

0 comments on commit 2a5a131

Please sign in to comment.