Created
November 18, 2025 04:35
-
-
Save CreateToSurvive/3a1bc34685322c7424f7b3b9ba91691f to your computer and use it in GitHub Desktop.
Nov 18, 2024 | tui-lib/options.sh | + Пример использования библиотеке в отдельном файле sm.sh
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| w_init() | |
| { | |
| printf "\e[?25l\e[2J" | |
| trap "w_reset""" SIGINT EXIT | |
| } | |
| w_reset() | |
| { | |
| printf "\e[?25h\e[0m" | |
| clear | |
| exit 0 | |
| } | |
| w_init "" | |
| create_menus() | |
| { | |
| local groupName="$1" | |
| local optionName="$2" | |
| local actionName="$3" | |
| if (( ${#namesOfGroups[@]} == 0 )); then | |
| defaultGroup="$groupName" | |
| namesOfGroups+=("$groupName") | |
| fi | |
| for i in "${namesOfGroups[@]}"; do | |
| if [[ "$i" == "$groupName" ]]; then | |
| found=1 | |
| break | |
| fi | |
| done | |
| if (( ! $found )); then | |
| namesOfGroups+=("$groupName") | |
| fi | |
| eval "${groupName}Options+=(\"${optionName}\")" | |
| eval "${groupName}Actions+=(\"${actionName}\")" | |
| found=0 | |
| } | |
| switch_menu() | |
| { | |
| currentGroup="$1" | |
| output="pressed" | |
| eval "COUNT=\${#${currentGroup}Options[@]}" | |
| CHOICE=0 | |
| } | |
| switch_to_default_group() | |
| { | |
| currentGroup="$defaultGroup" | |
| output="home" | |
| eval "COUNT=\${#${currentGroup}Options[@]}" | |
| CHOICE=0 | |
| } | |
| execute_action() { eval "\${${currentGroup}Actions[\$(( CHOICE ))]}"; } | |
| update_status_bar_info() { eval "printf \"\e[\${${currentGroup}StatusBarLinePos};\${${currentGroup}StatusBarColPos}H# %s\" \"$output\""; } | |
| input_handler() | |
| { | |
| read -rsn1 key | |
| case $key in | |
| k) (( CHOICE-- ));; | |
| j) (( CHOICE++ ));; | |
| "") execute_action "";; | |
| 1) CHOICE=0 && execute_action "" ;; | |
| 2) CHOICE=1 && execute_action "" ;; | |
| 3) CHOICE=2 && execute_action "" ;; | |
| 4) CHOICE=3 && execute_action "" ;; | |
| 5) CHOICE=4 && execute_action "" ;; | |
| q) exit 0 ;; | |
| esac | |
| (( CHOICE == $(( COUNT )) )) && CHOICE=0 | |
| (( CHOICE == -1 )) && CHOICE=$((COUNT - 1)) | |
| UNPAUSE=1 | |
| } | |
| update_options() | |
| { | |
| printf "\e[2J" | |
| eval "printf \"%b\" \"\${formed${currentGroup}Options[@]}\"" | |
| } | |
| form_options() | |
| { | |
| for i in "${namesOfGroups[@]}"; do | |
| local groupName="$i" | |
| if [[ "$groupName" != "$defaultGroup" ]]; then | |
| eval "${groupName}Options+=(\"<- back\")" | |
| eval "${groupName}Actions+=(switch_to_default_group)" | |
| else | |
| eval "${groupName}Options+=(\"exit\")" | |
| eval "${groupName}Actions+=(exit 0)" | |
| fi | |
| eval "options=(\"\${${groupName}Options[@]}\")" | |
| find_centers() | |
| { | |
| for i in ${options[i]}; do | |
| if (( ${#options[i]} > max )); then | |
| indx=$i | |
| max=$(( ${#options[i]} / 2 )) | |
| fi | |
| done | |
| cols=$(tput cols) | |
| lines=$(tput lines) | |
| cntOfLines=0 | |
| linesB=$(( lines / 2 )) | |
| colL=$(( ${#options[indx]} / 2 )) | |
| colB="$(( cols / 2 ))" | |
| value=$(( colB - colL )) | |
| if (( ${#options[@]} % 2 == 0 )); then | |
| cntOfLines=$(( linesB - $(( ${#options[@]} / 2 )) + 1 )) | |
| else | |
| cntOfLines=$(( linesB - $(( $(( ${#options[@]} - 1 )) / 2 )) + 1 )) | |
| fi | |
| } | |
| find_centers "" | |
| # j=${#options[@]} | |
| for ((i=0; i <= ${#options[@]}; i++ )); do | |
| center=$(( ${#options[i]} / 2 )) | |
| # cenCopy=$center | |
| cntOfSpaces=$(( value + max - center )) | |
| eval "formed${groupName}Options+=(\"\e[$(( cntOfLines + i ));${cntOfSpaces}H${options[$i]}\e[0m\n\")" | |
| # (( j-- )) | |
| done | |
| eval "${groupName}StatusBarLinePos=$(( cntOfLines + ${#options[@]} + 1 ))" | |
| eval "${groupName}StatusBarColPos=$(( value ))" | |
| # eval "formed${groupName}Options+=(\"\e[$(( cntOfLines + ${#options[@]} + 1 ));${cntOfSpaces}H# \")" | |
| unset max | |
| done | |
| UNPAUSE=1 | |
| currentGroup="$defaultGroup" | |
| eval "COUNT=\${#${currentGroup}Options[@]}" | |
| CHOICE=0 | |
| for ((;;)) | |
| { | |
| (( UNPAUSE )) && update_options && update_status_bar_info | |
| UNPAUSE=0 | |
| eval "printf \"\e[7m%b\" \"\${formed${currentGroup}Options[$(( CHOICE ))]}\"" | |
| input_handler "" | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Стандартное меню - первое объявленное меню с помощью create_option | |
| # Переключаться на меню можно с помощью switch_menu "<menuName>" | |
| # create_menu "<menuName>" <optionName>" "<action>" | |
| # После объявления и использования всех функций необходимо написать form_options "" в конце файла | |
| source "$HOME/scripts/tui-lib/options.sh" | |
| m1="Main" | |
| m2="Frequency" | |
| create_menus "$m1" "switch CPU frequency" "switch_menu "$m2"" | |
| create_menus "$m2" "2,0 GHz" "manage_CPU_freq_limit "1"" | |
| create_menus "$m2" "2,7 GHz" "manage_CPU_freq_limit "2"" | |
| manage_CPU_freq_limit() | |
| { | |
| case "$1" in | |
| "1") | |
| sudo cpupower frequency-set -u 2.00GHz >> /dev/null | |
| output="2.00GHz CAP" | |
| ;; | |
| "2") | |
| sudo cpupower frequency-set -u 2.70GHz >> /dev/null | |
| output="2.70GHz CAP" | |
| ;; | |
| *) | |
| sudo cpupower frequency-set -u 10.00GHz >> /dev/null | |
| output="frequency reset" | |
| ;; | |
| esac | |
| } | |
| form_options "" |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Если скрипт использует команды, которые требуют исполнения от администратора, то я делаю alias на такой случай:
sm="sudo -E ~/sm.sh"