Skip to content

Commit

Permalink
choose, styles: fix terminal title and terminal resize not closing co…
Browse files Browse the repository at this point in the history
…rrectly

/ref #261 #254
  • Loading branch information
balupton committed Jan 30, 2025
1 parent d565f72 commit 6c44ccb
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 17 deletions.
30 changes: 15 additions & 15 deletions commands/choose
Original file line number Diff line number Diff line change
Expand Up @@ -987,7 +987,7 @@ function choose_() (
menu_body_rows=0 \
menu_body='' \
menu_mode='choose' \
menu_resized_to='' \
menu_resized_lines='' \
menu_skip_render='no' \
menu_status=0 \
menu_top_index_choose=0 \
Expand Down Expand Up @@ -1282,7 +1282,7 @@ function choose_() (
terminal_lines_prior="$terminal_lines"
terminal_columns_prior="$terminal_columns"
fi
if [[ -n $menu_resized_to && $menu_resized_to -gt $terminal_lines ]]; then
if [[ -n $menu_resized_lines && $menu_resized_lines -gt $terminal_lines ]]; then
terminal_too_short='yes'
else
terminal_too_short='no'
Expand Down Expand Up @@ -1724,10 +1724,10 @@ function choose_() (
fi
menu_top_index="$menu_index"
menu_bottom_index="$menu_index"
menu_resized_to=''
menu_resized_lines=''
# debug
function debug_menu {
debug "c=$menu_cursor" "mi=$menu_index" "ii=$item_index" "d=$menu_direction" "t=$menu_top_index" "b=$menu_bottom_index" "l=$menu_last" "u=$paging_used" "r=$menu_rows" "mr=$menu_resized_to" "tr=$terminal_lines" "tts=$terminal_too_short"
debug "c=$menu_cursor" "mi=$menu_index" "ii=$item_index" "d=$menu_direction" "t=$menu_top_index" "b=$menu_bottom_index" "l=$menu_last" "u=$paging_used" "r=$menu_rows" "mr=$menu_resized_lines" "tr=$terminal_lines" "tts=$terminal_too_short"
}
# e.g. go to second page, navigate half way, go page up
for (( ; menu_index >= 0 && menu_index <= menu_last; menu_index += menu_direction)); do
Expand Down Expand Up @@ -1808,7 +1808,7 @@ function choose_() (
if [[ $menu_rows -gt $terminal_lines ]]; then
if [[ -z $items_bundled ]]; then
# we need to resize the terminal
menu_resized_to="$menu_rows"
menu_resized_lines="$menu_rows"
else
# we already have content rendered, so exit with what we have
break
Expand All @@ -1824,7 +1824,7 @@ function choose_() (
items_bundled="$item_line$items_bundled"
menu_top_index="$menu_index"
fi
if [[ -n $menu_resized_to ]]; then
if [[ -n $menu_resized_lines ]]; then
break
fi
done
Expand Down Expand Up @@ -1901,8 +1901,8 @@ function choose_() (
fi

# resize the terminal if it is too short?
if [[ -n $menu_resized_to ]]; then
terminal_title+="${style__terminal_resize}${menu_resized_to}${style__terminal_resize_end}"
if [[ -n $menu_resized_lines ]]; then
terminal_title+="${style__terminal_resize}${menu_resized_lines};${style__end__terminal_resize}"
fi

# calculate page top, middle, bottom
Expand Down Expand Up @@ -2075,17 +2075,17 @@ function choose_() (
if [[ $title_supported == 'no' ]]; then
terminal_title=''
elif [[ $paging_used == 'yes' ]]; then
terminal_title+="${style__terminal_title}👉 ${verb} ${verb_total} of ${items_count} items 💁‍♀️ Viewing ${page_items_count} of ${items_count} items [$((menu_top_index + 1))$((menu_cursor + 1))$((menu_bottom_index + 1))] 👈${style__terminal_title_end}"
terminal_title+="${style__terminal_title}👉 ${verb} ${verb_total} of ${items_count} items 💁‍♀️ Viewing ${page_items_count} of ${items_count} items [$((menu_top_index + 1))$((menu_cursor + 1))$((menu_bottom_index + 1))] 👈${style__end__terminal_title}"
else
terminal_title+="${style__terminal_title}👉 ${verb} ${verb_total} of ${items_count} items 👈${style__terminal_title_end}"
terminal_title+="${style__terminal_title}👉 ${verb} ${verb_total} of ${items_count} items 👈${style__end__terminal_title}"
fi
printf '%s' "${style__clear_screen}${style__hide_cursor}${terminal_title}${menu_title}${menu_body}${bar_top_content}${bar_middle_content}${items_bundled}${bar_bottom_content}${menu_legend_content}" >"$terminal_device_file"
if [[ -n $menu_resized_to ]]; then
if [[ -n $menu_resized_lines ]]; then
refresh_terminal_size
if [[ $terminal_too_short == 'yes' ]]; then
terminal_title="${style__terminal_title}‼️ TERMINAL TOO SHORT ‼️${style__terminal_title_end}"
local additional_rows="$((menu_resized_to - terminal_lines))"
printf '%s' "${style__clear_screen}${style__hide_cursor}${terminal_title}${style__error}You have been prompted to make a menu selection, however the terminal does not have enough vertical height to make that selection.${style__end__error}"$'\n'"${style__notice}Increase the terminal height or reduce font size to proceed, then press any key.${style__end__notice}"$'\n'"Terminal rows = ${terminal_lines}, Needed rows = ${menu_resized_to}, Additional rows = ${additional_rows}"$'\n' >"$terminal_device_file"
terminal_title="${style__terminal_title}‼️ TERMINAL TOO SHORT ‼️${style__end__terminal_title}"
local additional_rows="$((menu_resized_lines - terminal_lines))"
printf '%s' "${style__clear_screen}${style__hide_cursor}${terminal_title}${style__error}You have been prompted to make a menu selection, however the terminal does not have enough vertical height to make that selection.${style__end__error}"$'\n'"${style__notice}Increase the terminal height or reduce font size to proceed, then press any key.${style__end__notice}"$'\n'"Terminal rows = ${terminal_lines}, Needed rows = ${menu_resized_lines}, Additional rows = ${additional_rows}"$'\n' >"$terminal_device_file"
fi
fi
}
Expand Down Expand Up @@ -2144,7 +2144,7 @@ function choose_() (
printf '' >debug.txt
fi
message="$(printf '%q ' "$@" "c=${debug_counter}")"
printf '%s' "${style__terminal_title}${message}${style__terminal_title_end}" >"$terminal_device_file"
printf '%s' "${style__terminal_title}${message}${style__end__terminal_title}" >"$terminal_device_file"
printf '%s\n' "$message" >>debug.txt
debug_counter="$((debug_counter + 1))"
}
Expand Down
11 changes: 9 additions & 2 deletions config/styles.bash
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,17 @@ style__backspace=$'\b'
style__carriage_return=$'\r'
style__escape=$'\e'
style__home=$'\e[H'

# terminal
style__terminal_title=$'\e]0;'
style__terminal_title_end=$'\a'
style__end__terminal_title=$'\a'

# echo-style --terminal-resize='100;80' # height and width
# echo-style --terminal-resize='100;' # width only
# echo-style --terminal-resize=';80' # width only
style__terminal_resize=$'\e[8;'
style__terminal_resize_end=';t'
style__end__terminal_resize='t'


# modes
style__color_end__intensity=$'\e[22m' #
Expand Down

0 comments on commit 6c44ccb

Please sign in to comment.