diff --git a/docs/iptv.sh b/docs/iptv.sh index 6fa0697..b85ec62 100755 --- a/docs/iptv.sh +++ b/docs/iptv.sh @@ -693,7 +693,7 @@ inquirer() local var=("$1"[@]) if [[ -z ${!var:-} ]] then - return 0 + return fi local join_list=("${!var}") first=true item for item in "${join_list[@]}" @@ -709,7 +709,7 @@ inquirer() } inquirer:on_default() { - return 0 + return } inquirer:on_keypress() { @@ -749,7 +749,7 @@ inquirer() # The space is the first printable character listed on http://www.asciitable.com/, ~ is the last # [^ -~] *[$'\x80'-$'\xFF']*) - if [[ ${BASH_VERSINFO[0]} -lt 4 ]] && [ "$option" == "text_input" ] + if [[ ${BASH_VERSINFO[0]} -lt 4 ]] then char_read="${char_read:-}$key" LC_ALL= LANG=C @@ -819,6 +819,46 @@ inquirer() } inquirer:on_checkbox_input_up() { + if [ "$checkbox_input_search" = true ] + then + tput cub "$(tput cols)" + tput el + tput cuu1 + tput el + tput cuu1 + tput el + + stty -echo + tput civis + + local i + + for((i=0;i 选择, 确认)\"`${normal}\n\n\n" + fi + inquirer:on_checkbox_input_up + return + fi + + if [ "$checkbox_input_page" = true ] + then + checkbox_input_page=false + if [ -n "${checkbox_input_page_num:-}" ] + then + if [ "$checkbox_input_page_num" -gt "$checkbox_pages_count" ] || [ "$checkbox_input_page_num" -eq $((checkbox_pages_index+1)) ] + then + checkbox_input_page_num="" + return + fi + + checkbox_pages_index=$((checkbox_input_page_num-1)) + + checkbox_pages_tip="${dim}$checkbox_pages_arrows $((checkbox_pages_index+1))/$checkbox_pages_count `gettext \"页\"`${normal}" + + if [ "$checkbox_input_page_num" -eq "$checkbox_pages_count" ] + then + checkbox_page_list_count=$((checkbox_list_count-checkbox_pages_index*checkbox_list_perpage)) + else + checkbox_page_list_count=$checkbox_list_perpage + fi + + inquirer:page_instructions + tput cub "$(tput cols)" + tput cud $((checkbox_list_perpage-current_index+1)) + + for((i=0;i<=checkbox_list_perpage;i++)); + do + tput el + tput cuu1 + done + + tput el + + if [ "$current_index" -gt "$checkbox_page_list_count" ] + then + current_index=$checkbox_page_list_count + fi + + checkbox_page_list=() + checkbox_page_selected=() + checkbox_page_select_all=true + + for((i=0;i 选择, 确认)\"`${normal}\n\n\n" + fi + inquirer:on_checkbox_input_up + return + fi + + if [ "$checkbox_input_page" = true ] + then + checkbox_input_page=false + if [ -n "${checkbox_input_page_num:-}" ] + then + if [ "$checkbox_input_page_num" -gt "$checkbox_pages_count" ] || [ "$checkbox_input_page_num" -eq $((checkbox_pages_index+1)) ] + then + checkbox_input_page_num="" + return + fi + + checkbox_pages_index=$((checkbox_input_page_num-1)) + + checkbox_pages_tip="${dim}$checkbox_pages_arrows $((checkbox_pages_index+1))/$checkbox_pages_count `gettext \"页\"`${normal}" + + if [ "$checkbox_input_page_num" -eq "$checkbox_pages_count" ] + then + checkbox_page_list_count=$((checkbox_list_count-checkbox_pages_index*checkbox_list_perpage)) + else + checkbox_page_list_count=$checkbox_list_perpage + fi + + inquirer:page_instructions + tput cub "$(tput cols)" + tput cud $((checkbox_list_perpage-current_index+1)) + + for((i=0;i<=checkbox_list_perpage;i++)); + do + tput el + tput cuu1 + done + + tput el + + if [ "$current_index" -gt "$checkbox_page_list_count" ] + then + current_index=$checkbox_page_list_count + fi + + checkbox_page_list=() + checkbox_page_selected=() + checkbox_page_select_all=true + + for((i=0;i 选择, 确认)\"`${normal}\n\n\n" + fi + inquirer:on_checkbox_input_up + return + fi + + if [ "$checkbox_input_page" = true ] + then + checkbox_input_page=false + if [ -n "${checkbox_input_page_num:-}" ] + then + if [ "$checkbox_input_page_num" -gt "$checkbox_pages_count" ] || [ "$checkbox_input_page_num" -eq $((checkbox_pages_index+1)) ] + then + checkbox_input_page_num="" + return + fi + + checkbox_pages_index=$((checkbox_input_page_num-1)) + + checkbox_pages_tip="${dim}$checkbox_pages_arrows $((checkbox_pages_index+1))/$checkbox_pages_count `gettext \"页\"`${normal}" + + if [ "$checkbox_input_page_num" -eq "$checkbox_pages_count" ] + then + checkbox_page_list_count=$((checkbox_list_count-checkbox_pages_index*checkbox_list_perpage)) + else + checkbox_page_list_count=$checkbox_list_perpage + fi + + inquirer:page_instructions + tput cub "$(tput cols)" + tput cud $((checkbox_list_perpage-current_index+1)) + + for((i=0;i<=checkbox_list_perpage;i++)); + do + tput el + tput cuu1 + done + + tput el + + if [ "$current_index" -gt "$checkbox_page_list_count" ] + then + current_index=$checkbox_page_list_count + fi + + checkbox_page_list=() + checkbox_page_selected=() + checkbox_page_select_all=true + + for((i=0;i 选择, 确认)${normal}" + if [ "$checkbox_list_perpage" -gt 0 ] && [ "$checkbox_list_count" -gt "$checkbox_list_perpage" ] + then + checkbox_pages_count=$((checkbox_list_count/checkbox_list_perpage)) + if [[ $((checkbox_list_perpage*checkbox_pages_count)) -lt "$checkbox_list_count" ]] + then + ((checkbox_pages_count++)) + fi + checkbox_pages_tip="${dim}$checkbox_pages_arrows $((checkbox_pages_index+1))/$checkbox_pages_count `gettext \"页\"`${normal} " + fi + + inquirer:print "${green}?${normal} ${bold}${bg_black}${white}${prompt} ${checkbox_pages_tip:-}${dim}`gettext \"(按 选择, 确认)\"`${normal}\n" - for i in $(inquirer:gen_index ${#_checkbox_list[@]}) + for i in "${!checkbox_list[@]}" do - _checkbox_selected[i]=false + checkbox_selected[i]=false done - if [ -n "${3:-}" ] + var=("$3"[@]) + if [[ -n ${!var:-} ]] then - var=("$3"[@]) - _selected_indices=("${!var}") - for i in "${_selected_indices[@]}" + checkbox_selected_indices=("${!var}") + for i in "${checkbox_selected_indices[@]}" do - _checkbox_selected[i]=true + checkbox_selected[i]=true done + checkbox_selected_indices=() fi - for i in $(inquirer:gen_index ${#_checkbox_list[@]}) + for i in "${!checkbox_list[@]}" do - tput cub "$(tput cols)" - if [ $i = 0 ] + if [ "$i" = 0 ] then - if [ "${_checkbox_selected[i]}" = true ] + if [ "${checkbox_selected[i]}" = true ] then - inquirer:print "${cyan}${arrow}${green}${checked}${normal} ${_checkbox_list[i]} ${normal}" + inquirer:print "${cyan}${arrow}${green}${checked}${normal} ${checkbox_list[i]}\n" else - inquirer:print "${cyan}${arrow}${normal}${unchecked} ${_checkbox_list[i]} ${normal}" + inquirer:print "${cyan}${arrow}${normal}${unchecked} ${checkbox_list[i]}\n" fi + elif [ "$checkbox_pages_count" -gt 1 ] && [ "$i" = "$checkbox_list_perpage" ] + then + break else - if [ "${_checkbox_selected[i]}" = true ] + if [ "${checkbox_selected[i]}" = true ] then - inquirer:print " ${green}${checked}${normal} ${_checkbox_list[i]} ${normal}" + inquirer:print " ${green}${checked}${normal} ${checkbox_list[i]}\n" else - inquirer:print " ${unchecked} ${_checkbox_list[i]} ${normal}" + inquirer:print " ${unchecked} ${checkbox_list[i]}\n" fi fi - tput el + ((checkbox_page_list_count++)) + checkbox_page_list+=("${checkbox_list[i]}") done - for j in $(inquirer:gen_index ${#_checkbox_list[@]}) + for((i=0;i 上下移动)\"`${normal}\n" + + for i in "${!sort_options[@]}" + do + if [ $i = 0 ] + then + inquirer:print "${cyan}${arrow} ${sort_options[i]} ${normal}\n" + else + inquirer:print " ${sort_options[i]}\n" + fi done - inquirer:on_keypress inquirer:on_checkbox_input_up inquirer:on_checkbox_input_down inquirer:on_checkbox_input_space inquirer:on_checkbox_input_enter inquirer:on_default inquirer:on_default inquirer:on_checkbox_input_ascii + tput cuu ${#sort_options[@]} + + inquirer:on_keypress inquirer:on_sort_up inquirer:on_sort_down inquirer:on_sort_enter_space inquirer:on_sort_enter_space inquirer:on_default inquirer:on_default inquirer:on_sort_ascii } - inquirer:checkbox_input() { - inquirer:_checkbox_input "$1" "$2" - _checkbox_input_output_var_name=$3 - inquirer:select_indices _checkbox_list _checkbox_selected_indices $_checkbox_input_output_var_name - - unset _checkbox_list - unset _break_keypress - unset _first_keystroke - unset _current_index - unset _checkbox_input_output_var_name - unset _checkbox_selected_indices - unset _checkbox_selected_options + inquirer:sort_input() { + var_name=$3 + + inquirer:_sort_input "$1" "$2" + + read -r -a ${var_name?} <<< "${sort_options[@]}" inquirer:cleanup + + trap - EXIT } - inquirer:checkbox_input_indices() { - inquirer:_checkbox_input "$1" "$2" "$3" - _checkbox_input_output_var_name=$3 + inquirer:sort_input_indices() { + var_name=$3 - declare -a new_array - for i in $(inquirer:gen_index ${#_checkbox_selected_indices[@]}) - do - new_array+=("${_checkbox_selected_indices[i]}") - done - read -r -a ${_checkbox_input_output_var_name?} <<< "${new_array[@]}" - unset new_array + inquirer:_sort_input "$1" "$2" - unset _checkbox_list - unset _break_keypress - unset _first_keystroke - unset _current_index - unset _checkbox_input_output_var_name - unset _checkbox_selected_indices - unset _checkbox_selected_options + read -r -a ${var_name?} <<< "${sort_indices[@]}" inquirer:cleanup + + trap - EXIT } inquirer:on_list_input_up() { - inquirer:remove_list_instructions + inquirer:remove_instructions + + if [ "${#list_options[@]}" -eq 1 ] + then + return + fi + tput cub "$(tput cols)" - printf '%s' " ${_list_options[$_list_selected_index]}" - tput el + inquirer:print " ${list_options[current_index]}" - if [ $_list_selected_index = 0 ] + if [ $current_index = 0 ] then - _list_selected_index=$((${#_list_options[@]}-1)) - tput cud $((${#_list_options[@]}-1)) - tput cub "$(tput cols)" + current_index=$((${#list_options[@]}-1)) + tput cud $((${#list_options[@]}-1)) else - _list_selected_index=$((_list_selected_index-1)) - + current_index=$((current_index-1)) tput cuu1 - tput cub "$(tput cols)" - tput el fi - printf "${cyan}${arrow} %s ${normal}" "${_list_options[$_list_selected_index]}" + tput cub "$(tput cols)" + + inquirer:print "${cyan}${arrow} ${list_options[current_index]}${normal}" } inquirer:on_list_input_down() { - inquirer:remove_list_instructions + inquirer:remove_instructions + + if [ "${#list_options[@]}" -eq 1 ] + then + return + fi + tput cub "$(tput cols)" - printf '%s' " ${_list_options[$_list_selected_index]}" - tput el + inquirer:print " ${list_options[current_index]}" - if [ $_list_selected_index = $((${#_list_options[@]}-1)) ] + if [ $current_index = $((${#list_options[@]}-1)) ] then - _list_selected_index=0 - tput cuu $((${#_list_options[@]}-1)) - tput cub "$(tput cols)" + current_index=0 + tput cuu $((${#list_options[@]}-1)) else - _list_selected_index=$((_list_selected_index+1)) + current_index=$((current_index+1)) tput cud1 - tput cub "$(tput cols)" - tput el fi - printf "${cyan}${arrow} %s ${normal}" "${_list_options[$_list_selected_index]}" + + tput cub "$(tput cols)" + + inquirer:print "${cyan}${arrow} ${list_options[current_index]} ${normal}" } inquirer:on_list_input_enter_space() { - local OLD_IFS=$IFS - IFS=$'\n' + local i - tput cud $((${#_list_options[@]}-_list_selected_index)) + tput cud $((${#list_options[@]}-current_index)) tput cub "$(tput cols)" - for i in $(seq $((${#_list_options[@]}+1))) + for i in $(seq $((${#list_options[@]}+1))) do - tput el1 tput el tput cuu1 done - tput cub "$(tput cols)" tput cuf $((prompt_width+3)) - printf '%s' "${cyan}${_list_options[$_list_selected_index]}${normal}" - tput el + inquirer:print "${bg_black}${cyan}${list_options[current_index]}${normal}\n" - tput cud1 - tput cub "$(tput cols)" - tput el - - _break_keypress=true - IFS=$OLD_IFS + break_keypress=true } - inquirer:on_list_input_input_ascii() - { - local key=$1 - case "$key" in + inquirer:on_list_input_ascii() { + case "$1" in "w" ) inquirer:on_list_input_up;; "s" ) inquirer:on_list_input_down;; esac } - inquirer:remove_list_instructions() { - if [ "$_first_keystroke" = true ] + inquirer:_list_input() { + local i var=("$2"[@]) + list_options=("${!var}") + current_index=0 + + if [ "${#list_options[@]}" -eq 1 ] then - tput cuu $((_list_selected_index+1)) - tput cub "$(tput cols)" - tput cuf $((prompt_width+3)) - tput el - tput cud $((_list_selected_index+1)) - _first_keystroke=false + return fi - } - - inquirer:_list_input() { - local i j var=("$2"[@]) - _list_options=("${!var}") - _list_selected_index=0 - _first_keystroke=true + first_keystroke=true - trap inquirer:control_c SIGINT EXIT + trap inquirer:control_c EXIT stty -echo tput civis - inquirer:print "${green}?${normal} ${bold}${prompt}${normal} ${dim}(使用上下箭头选择)${normal}" + inquirer:print "${green}?${normal} ${bold}${bg_black}${white}${prompt} ${dim}`gettext \"(使用上下箭头选择)\"`${normal}\n" - for i in $(inquirer:gen_index ${#_list_options[@]}) + for i in "${!list_options[@]}" do - tput cub "$(tput cols)" if [ $i = 0 ] then - inquirer:print "${cyan}${arrow} ${_list_options[i]} ${normal}" + inquirer:print "${cyan}${arrow} ${list_options[i]} ${normal}\n" else - inquirer:print " ${_list_options[i]}" + inquirer:print " ${list_options[i]}\n" fi - tput el done - for j in $(inquirer:gen_index ${#_list_options[@]}) - do - tput cuu1 - done + tput cuu ${#list_options[@]} - inquirer:on_keypress inquirer:on_list_input_up inquirer:on_list_input_down inquirer:on_list_input_enter_space inquirer:on_list_input_enter_space inquirer:on_default inquirer:on_default inquirer:on_list_input_input_ascii + inquirer:on_keypress inquirer:on_list_input_up inquirer:on_list_input_down inquirer:on_list_input_enter_space inquirer:on_list_input_enter_space inquirer:on_default inquirer:on_default inquirer:on_list_input_ascii } inquirer:list_input() { - inquirer:_list_input "$1" "$2" var_name=$3 - read -r ${var_name?} <<< "${_list_options[$_list_selected_index]}" - unset _list_selected_index - unset _list_options - unset _break_keypress - unset _first_keystroke + + inquirer:_list_input "$1" "$2" + + read -r ${var_name?} <<< "${list_options[current_index]}" inquirer:cleanup + + trap - EXIT } inquirer:list_input_index() { - inquirer:_list_input "$1" "$2" var_name=$3 - read -r ${var_name?} <<< "$_list_selected_index" - unset _list_selected_index - unset _list_options - unset _break_keypress - unset _first_keystroke + + inquirer:_list_input "$1" "$2" + + read -r ${var_name?} <<< "$current_index" inquirer:cleanup + + trap - EXIT } inquirer:on_text_input_left() { - inquirer:remove_regex_failed - if [[ $_current_pos -gt 0 ]] + if [[ $current_pos -gt 0 ]] then - local current=${_text_input:$_current_pos:1} current_width + local current=${text_input:$current_pos:1} current_width current_width=$(inquirer:display_length "$current") tput cub $current_width - _current_pos=$((_current_pos-1)) + current_pos=$((current_pos-1)) fi } inquirer:on_text_input_right() { - inquirer:remove_regex_failed - if [[ $((_current_pos+1)) -eq ${#_text_input} ]] + if [[ $((current_pos+1)) -eq ${#text_input} ]] then tput cuf1 - _current_pos=$((_current_pos+1)) - elif [[ $_current_pos -lt ${#_text_input} ]] + current_pos=$((current_pos+1)) + elif [[ $current_pos -lt ${#text_input} ]] then - local next=${_text_input:$((_current_pos+1)):1} next_width + local next=${text_input:$((current_pos+1)):1} next_width next_width=$(inquirer:display_length "$next") tput cuf $next_width - _current_pos=$((_current_pos+1)) + current_pos=$((current_pos+1)) fi } inquirer:on_text_input_enter() { - inquirer:remove_regex_failed + text_input=${text_input:-$text_default} - _text_input=${_text_input:-$_text_default_value} + tput civis + tput cub "$(tput cols)" + tput el - if [[ $($_text_input_validator "$_text_input") = true ]] + if $text_input_validator "$text_input" then - tput cuu 1 - tput cub "$(tput cols)" + tput sc + tput cuu $((1+failed_count*3)) tput cuf $((prompt_width+3)) - printf '%s' "${cyan}${_text_input}${normal}" - tput el - tput cud1 - tput cub "$(tput cols)" - tput el - read -r ${var_name?} <<< "$_text_input" - _break_keypress=true + inquirer:print "${bg_black}${cyan}" + inquirer:print_input "${text_input}" + inquirer:print "${normal}" + tput rc + break_keypress=true else - _text_input_regex_failed=true - tput civis - tput cuu1 - tput cub "$(tput cols)" - tput cuf $((prompt_width+3)) - tput el + failed_count=$((failed_count+1)) tput cud1 - tput cub "$(tput cols)" - tput el + inquirer:print "${bg_black}${red}${text_input_regex_failed_msg}${normal}\n" tput cud1 - tput cub "$(tput cols)" - printf '%b' "${red}$_text_input_regex_failed_msg${normal}" - tput el - _text_input="" - _current_pos=0 - tput cnorm + if [ "$text_input" == "$text_default" ] + then + text_input="" + current_pos=0 + else + inquirer:print_input "${text_input}" + tput cub "$(tput cols)" + tput cuf "$current_pos" + fi fi + + tput cnorm } inquirer:on_text_input_ascii() { - inquirer:remove_regex_failed local c=${1:- } + local rest=${text_input:$current_pos} rest_width + local current=${text_input:$current_pos:1} current_width - local rest=${_text_input:$_current_pos} rest_width - local current=${_text_input:$_current_pos:1} current_width rest_width=$(inquirer:display_length "$rest") current_width=$(inquirer:display_length "$current") - - _text_input="${_text_input:0:$_current_pos}$c$rest" - _current_pos=$((_current_pos+1)) + text_input="${text_input:0:$current_pos}$c$rest" + current_pos=$((current_pos+1)) tput civis + [[ $current_width -gt 1 ]] && tput cub $((current_width-1)) - printf '%s' "$c$rest" - tput el + + inquirer:print_input "$c$rest" if [[ $rest_width -gt 0 ]] then tput cub $((rest_width-current_width+1)) fi - tput cnorm - } - - inquirer:display_length() { - local display_length=0 byte_len - local oLC_ALL=${LC_ALL:-} oLANG=${LANG:-} LC_ALL=${LC_ALL:-} LANG=${LANG:-} - - while IFS="" read -rsn1 char - do - case "$char" in - '') - ;; - *[$'\x80'-$'\xFF']*) - LC_ALL='' LANG=C - byte_len=${#char} - LC_ALL=$oLC_ALL LANG=$oLANG - if [[ $byte_len -eq 2 ]] - then - display_length=$((display_length+1)) - else - display_length=$((display_length+2)) - fi - ;; - *) - display_length=$((display_length+1)) - ;; - esac - done <<< "$1" - echo "$display_length" + tput cnorm } inquirer:on_text_input_not_ascii() { - inquirer:remove_regex_failed local c=$1 + local rest="${text_input:$current_pos}" rest_width + local current=${text_input:$current_pos:1} current_width - local rest="${_text_input:$_current_pos}" rest_width - local current=${_text_input:$_current_pos:1} current_width rest_width=$(inquirer:display_length "$rest") current_width=$(inquirer:display_length "$current") - - _text_input="${_text_input:0:$_current_pos}$c$rest" - _current_pos=$((_current_pos+1)) + text_input="${text_input:0:$current_pos}$c$rest" + current_pos=$((current_pos+1)) tput civis + [[ $current_width -gt 1 ]] && tput cub $((current_width-1)) - printf '%s' "$c$rest" - tput el + + inquirer:print_input "$c$rest" if [[ $rest_width -gt 0 ]] then tput cub $((rest_width-current_width+1)) fi + tput cnorm } inquirer:on_text_input_backspace() { - inquirer:remove_regex_failed - if [ $_current_pos -gt 0 ] || { [ $_current_pos -eq 0 ] && [ "${#_text_input}" -gt 0 ]; } + if [ $current_pos -gt 0 ] || { [ $current_pos -eq 0 ] && [ "${#text_input}" -gt 0 ]; } then local start rest rest_width del del_width next next_width offset - local current=${_text_input:$_current_pos:1} current_width + local current=${text_input:$current_pos:1} current_width current_width=$(inquirer:display_length "$current") tput civis - if [ $_current_pos -eq 0 ] + if [ $current_pos -eq 0 ] then - rest=${_text_input:$((_current_pos+1))} - next=${_text_input:$((_current_pos+1)):1} + rest=${text_input:$((current_pos+1))} + next=${text_input:$((current_pos+1)):1} rest_width=$(inquirer:display_length "$rest") next_width=$(inquirer:display_length "$next") offset=$((current_width-1)) [[ $offset -gt 0 ]] && tput cub $offset - printf '%s' "$rest" - tput el + inquirer:print_input "$rest" offset=$((rest_width-next_width+1)) [[ $offset -gt 0 ]] && tput cub $offset - _text_input=$rest + text_input="$rest" else - rest=${_text_input:$_current_pos} - start=${_text_input:0:$((_current_pos-1))} - del=${_text_input:$((_current_pos-1)):1} + rest=${text_input:$current_pos} + start=${text_input:0:$((current_pos-1))} + del=${text_input:$((current_pos-1)):1} rest_width=$(inquirer:display_length "$rest") del_width=$(inquirer:display_length "$del") - _current_pos=$((_current_pos-1)) + current_pos=$((current_pos-1)) if [[ $current_width -gt 1 ]] then tput cub $((del_width+current_width-1)) - printf '%s' "$rest" - tput el + inquirer:print_input "$rest" tput cub $((rest_width-current_width+1)) else tput cub $del_width - printf '%s' "$rest" - tput el + inquirer:print_input "$rest" [[ $rest_width -gt 0 ]] && tput cub $((rest_width-current_width+1)) fi - _text_input="$start$rest" + text_input="$start$rest" fi tput cnorm fi } - inquirer:remove_regex_failed() { - if [ "$_text_input_regex_failed" = true ] + inquirer:text_input_default_validator() { + return + } + + inquirer:text_input() { + var_name=$2 + text_default=${3:-} + text_input="" + current_pos=0 + failed_count=0 + local text_default_tip + + if [ -n "$text_default" ] + then + text_default_tip=" ${bold}${dim}($text_default)${normal}" + else + text_default_tip="${normal}" + fi + + text_input_validator=${4:-inquirer:text_input_default_validator} + text_input_regex_failed_msg=${5:-$(gettext "输入验证错误")} + + inquirer:print "${green}?${normal} ${bold}${bg_black}${white}${prompt}${text_default_tip}\n" + + trap inquirer:control_c EXIT + + stty -echo + tput cnorm + + inquirer:on_keypress inquirer:on_default inquirer:on_default inquirer:on_text_input_ascii inquirer:on_text_input_enter inquirer:on_text_input_left inquirer:on_text_input_right inquirer:on_text_input_ascii inquirer:on_text_input_backspace inquirer:on_text_input_not_ascii + read -r ${var_name?} <<< "$text_input" + + inquirer:cleanup + + trap - EXIT + } + + inquirer:date_pick_default_validator() { + if ! date +%s -d "$1" > /dev/null 2>&1 + then + return 1 + fi + return + } + + inquirer:remove_date_instructions() { + if [ "$first_keystroke" = true ] then - _text_input_regex_failed=false tput sc - tput cud1 - tput el1 + tput civis + tput cuu 1 + tput cub "$(tput cols)" + tput cuf $((prompt_width+3)) tput el tput rc + tput cnorm + first_keystroke=false fi } - inquirer:text_input_default_validator() { - echo true; + inquirer:on_date_pick_ascii() { + case "$1" in + "w" ) inquirer:on_date_pick_up;; + "s" ) inquirer:on_date_pick_down;; + "a" ) inquirer:on_date_pick_left;; + "d" ) inquirer:on_date_pick_right;; + esac } - inquirer:text_input() { - var_name=$2 - if [ -n "$_text_default_value" ] + inquirer:on_date_pick_up() { + inquirer:remove_date_instructions + case $current_pos in + 3) date_pick="$((${date_pick:0:4}+1))${date_pick:4}" + ;; + 6) + local month=$((10#${date_pick:5:2}+1)) + [ "$month" -eq 13 ] && month=1 + date_pick="${date_pick:0:5}$(printf %02d "$month")${date_pick:7}" + ;; + 9) + local day=$((10#${date_pick:8:2}+1)) + [ "$day" -eq 32 ] && day=1 + date_pick="${date_pick:0:8}$(printf %02d "$day")${date_pick:10}" + ;; + 12) + local hour=$(((10#${date_pick:11:2}+1)%24)) + date_pick="${date_pick:0:11}$(printf %02d "$hour")${date_pick:13}" + ;; + 15) + local min=$(((10#${date_pick:14:2}+1)%60)) + date_pick="${date_pick:0:14}$(printf %02d "$min")${date_pick:16}" + ;; + 18) + local sec=$(((10#${date_pick:17:2}+1)%60)) + date_pick="${date_pick:0:17}$(printf %02d "$sec")${date_pick:19}" + ;; + esac + + tput sc + tput civis + tput cub $current_pos + inquirer:print "$date_pick" + tput rc + tput cnorm + } + + inquirer:on_date_pick_down() { + inquirer:remove_date_instructions + case $current_pos in + 3) + local year=$((${date_pick:0:4}-1)) + [ "$year" -eq 2020 ] && return + date_pick="$year${date_pick:4}" + ;; + 6) + local month=$((10#${date_pick:5:2}-1)) + [ "$month" -eq 0 ] && month=12 + date_pick="${date_pick:0:5}$(printf %02d "$month")${date_pick:7}" + ;; + 9) + local day=$((10#${date_pick:8:2}-1)) + [ "$day" -eq 0 ] && day=31 + date_pick="${date_pick:0:8}$(printf %02d "$day")${date_pick:10}" + ;; + 12) + local hour=$(((10#${date_pick:11:2}+23)%24)) + date_pick="${date_pick:0:11}$(printf %02d "$hour")${date_pick:13}" + ;; + 15) + local min=$(((10#${date_pick:14:2}+59)%60)) + date_pick="${date_pick:0:14}$(printf %02d "$min")${date_pick:16}" + ;; + 18) + local sec=$(((10#${date_pick:17:2}+59)%60)) + date_pick="${date_pick:0:17}$(printf %02d "$sec")${date_pick:19}" + ;; + esac + + tput sc + tput civis + tput cub $current_pos + inquirer:print "$date_pick" + tput rc + tput cnorm + } + + inquirer:on_date_pick_left() { + inquirer:remove_date_instructions + if [[ $current_pos -gt 3 ]] + then + tput cub 3 + current_pos=$((current_pos-3)) + fi + } + + inquirer:on_date_pick_right() { + inquirer:remove_date_instructions + if [[ $current_pos -lt 18 ]] + then + tput cuf 3 + current_pos=$((current_pos+3)) + fi + } + + inquirer:on_date_pick_enter_space() { + tput civis + tput cub $current_pos + tput el + + if $date_pick_validator "$date_pick" then - _text_default_tip=" $dim($_text_default_value)" + tput sc + tput cuu $((1+failed_count*3)) + tput cuf $((prompt_width+3)) + inquirer:print "${bg_black}${cyan}${date_pick}${normal}" + tput rc + break_keypress=true else - _text_default_tip="" + failed_count=$((failed_count+1)) + tput cud1 + inquirer:print "${bg_black}${red}${date_pick_regex_failed_msg}${normal}\n" + tput cud1 + inquirer:print "${date_pick}" + tput cub $((19-current_pos)) fi - _text_input_regex_failed_msg=${4:-"输入验证错误"} - _text_input_validator=${5:-inquirer:text_input_default_validator} - _text_input_regex_failed=false - inquirer:print "${green}?${normal} ${bold}${prompt}$_text_default_tip${normal}" + tput cnorm + } + + inquirer:date_pick() { + var_name=$2 + date_pick_validator=${3:-inquirer:date_pick_default_validator} + date_pick_regex_failed_msg=${4:-$(gettext "时间验证错误")} + date_pick=$(printf '%(%Y-%m-%d %H:%M:%S)T' "${!var_name:--1}") + current_pos=12 + failed_count=0 + first_keystroke=true + + inquirer:print "${green}?${normal} ${bold}${bg_black}${white}${prompt} ${dim}`gettext \"(使用箭头选择)\"`${normal}\n" + inquirer:print "$date_pick" + tput cub 7 - trap inquirer:control_c SIGINT EXIT + trap inquirer:control_c EXIT stty -echo tput cnorm - inquirer:on_keypress inquirer:on_default inquirer:on_default inquirer:on_text_input_ascii inquirer:on_text_input_enter inquirer:on_text_input_left inquirer:on_text_input_right inquirer:on_text_input_ascii inquirer:on_text_input_backspace inquirer:on_text_input_not_ascii - read -r ${var_name?} <<< "$_text_input" + inquirer:on_keypress inquirer:on_date_pick_up inquirer:on_date_pick_down inquirer:on_date_pick_enter_space inquirer:on_date_pick_enter_space inquirer:on_date_pick_left inquirer:on_date_pick_right inquirer:on_date_pick_ascii + read -r ${var_name?} <<< $(date +%s -d "$date_pick") inquirer:cleanup + + trap - EXIT + } + + inquirer:remove_color_instructions() { + if [ "$first_keystroke" = true ] + then + tput cuu 1 + tput cub "$(tput cols)" + tput cuf $((prompt_width+3)) + tput el + tput cud 1 + first_keystroke=false + fi + } + + inquirer:on_color_pick_ascii() { + case "$1" in + "w" ) inquirer:on_color_pick_up;; + "s" ) inquirer:on_color_pick_down;; + "a" ) inquirer:on_color_pick_left;; + "d" ) inquirer:on_color_pick_right;; + esac + } + + inquirer:on_color_pick_up() { + inquirer:remove_color_instructions + tput cub "$(tput cols)" + colors_index=$(((colors_index+1)%16)) + inquirer:print "${bg_colors[bg_colors_index]}${colors[colors_index]}$text_default${normal}" + } + + inquirer:on_color_pick_down() { + inquirer:remove_color_instructions + tput cub "$(tput cols)" + colors_index=$(((colors_index-1)%16)) + inquirer:print "${bg_colors[bg_colors_index]}${colors[colors_index]}$text_default${normal}" + } + + inquirer:on_color_pick_left() { + inquirer:remove_color_instructions + tput cub "$(tput cols)" + bg_colors_index=$(((bg_colors_index-1)%17)) + inquirer:print "${bg_colors[bg_colors_index]}${colors[colors_index]}$text_default${normal}" + } + + inquirer:on_color_pick_right() { + inquirer:remove_color_instructions + tput cub "$(tput cols)" + bg_colors_index=$(((bg_colors_index+1)%17)) + inquirer:print "${bg_colors[bg_colors_index]}${colors[colors_index]}$text_default${normal}" + } + + inquirer:on_color_pick_enter_space() { + tput cub "$(tput cols)" + tput el + tput sc + tput cuu 1 + tput cuf $((prompt_width+3)) + inquirer:print "${bg_colors[bg_colors_index]}${colors[colors_index]}$text_default${normal}" + tput rc + break_keypress=true + + tput cnorm + } + + inquirer:color_pick() { + var_name=$2 + text_default="${3:-$(gettext "示例文字 ABC 123")}" + colors=( '\033[30m' '\033[31m' '\033[32m' '\033[33m' '\033[34m' '\033[35m' '\033[36m' '\033[37m' + '\033[90m' '\033[91m' '\033[92m' '\033[93m' '\033[94m' '\033[95m' '\033[96m' '\033[97m' ) + bg_colors=( '' '\033[40m' '\033[41m' '\033[42m' '\033[43m' '\033[44m' '\033[45m' '\033[46m' '\033[47m' + '\033[100m' '\033[101m' '\033[102m' '\033[103m' '\033[104m' '\033[105m' '\033[106m' '\033[107m' ) + colors_index=7 + bg_colors_index=0 + first_keystroke=true + + inquirer:print "${green}?${normal} ${bold}${bg_black}${white}${prompt} ${dim}`gettext \"(上下/左右 箭头选择 文字/背景颜色)\"`${normal}\n" + inquirer:print "${colors[colors_index]}${bg_colors[bg_colors_index]}$text_default${normal}" + + trap inquirer:control_c EXIT + + tput civis + + inquirer:on_keypress inquirer:on_color_pick_up inquirer:on_color_pick_down inquirer:on_color_pick_enter_space inquirer:on_color_pick_enter_space inquirer:on_color_pick_left inquirer:on_color_pick_right inquirer:on_color_pick_ascii + + read -r ${var_name?} <<< "${colors[colors_index]}${bg_colors[bg_colors_index]}" + + inquirer:cleanup + + trap - EXIT + } + + inquirer:display_length() { + local display_length=0 char_read byte_len char_len char_i char + local oLC_ALL=${LC_ALL:-} oLANG=${LANG:-} + + while IFS= read -rsn1 char + do + case "$char" in + '') + ;; + *[$'\x80'-$'\xFF']*) + char_read="${char_read:-}$char" + LC_ALL= LANG=C + byte_len=${#char_read} + LC_ALL=$oLC_ALL LANG=$oLANG + + if [ "$byte_len" -ne "${#char_read}" ] + then + if [[ $byte_len -le 2 ]] + then + display_length=$((display_length+1)) + elif [[ $byte_len -le 4 ]] + then + display_length=$((display_length+2)) + else + display_length=$((display_length+3)) + fi + char_read="" + fi + ;; + *) + display_length=$((display_length+1)) + ;; + esac + done <<< "${1:-}" + + echo "$display_length" } - local option=$1 + local option=$1 var_name \ + prompt=${2:-} \ + prompt_raw \ + prompt_width \ + break_keypress \ + first_keystroke \ + current_index \ + checkbox_list=() \ + checkbox_list_count \ + checkbox_list_perpage=0 \ + checkbox_page_list=() \ + checkbox_page_list_count=0 \ + checkbox_page_selected=() \ + checkbox_page_select_all=true \ + checkbox_input_page=false \ + checkbox_input_page_num \ + checkbox_input_search=false \ + checkbox_pages_tip \ + checkbox_pages_arrows="\xe2\x9d\xae\xe2\x9d\xaf" \ + checkbox_pages_index=0 \ + checkbox_pages_count=1 \ + checkbox_selected=() \ + checkbox_selected_indices=() \ + checkbox_selected_options=() \ + checkbox_input_failed_msg \ + sort_options=() \ + sort_indices=() \ + list_options=() \ + current_pos \ + failed_count \ + text_default \ + text_input \ + text_input_regex_failed_msg \ + text_input_validator \ + date_pick \ + date_pick_regex_failed_msg \ + date_pick_validator \ + colors=() \ + bg_colors=() \ + colors_index \ + bg_colors_index \ + arrow checked unchecked bold dim normal + + prompt_raw=$(printf '%b' "$prompt"|sed 's/\x1b\[[0-9;]*m//g') + prompt_width=$(inquirer:display_length "$prompt_raw") + + arrow='\xe2\x9d\xaf' + checked='\xe2\x97\x89' + unchecked='\xe2\x97\xaf' + red=${red:-'\033[31m'} + green=${green:-'\033[32m'} + yellow=${yellow:-'\033[33m'} + blue=${blue:-'\033[34m'} + cyan=${cyan:-'\033[36m'} + white=${white:-'\033[37m'} + bg_black=${bg_black:-'\033[40m'} + bold='\033[1m' + dim='\033[2m' + normal='\033[0m' + shift - local var_name prompt=${1:-} prompt_width _text_default_value=${3:-} _current_pos=0 _text_input="" _text_input_regex_failed_msg _text_input_validator _text_input_regex_failed - prompt_width=$(inquirer:display_length "$prompt") inquirer:$option "$@" } @@ -1566,4 +2595,4 @@ Cflags: -I\${includedir} Println "$info ffmpeg 编译成功\n" } -CompileFFmpeg \ No newline at end of file +CompileFFmpeg diff --git a/src/lhh b/src/lhh index 7e2b273..f1bc26d 100644 --- a/src/lhh +++ b/src/lhh @@ -300,7 +300,7 @@ reduce ({adapter,addtime,bookname,category_id,cover,drawing,m,muludir,original,p IFS="${delimiters[1]}" read -r -a result_publish <<< "$m_publish" IFS="${delimiters[1]}" read -r -a result_yd <<< "$m_yd" - local i j books new_book books_indices + local i j books new_book books_indices=() for((i=0;i