From 2cb99ae1aa0c9696dc5a61fe00d3ce2f45af844d Mon Sep 17 00:00:00 2001 From: Yi Zhang Date: Sat, 29 Sep 2018 22:28:38 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0dispHelp=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 从固定宽度显示改变为按单词分割显示 --- dispOptions.sh | 236 +++++++++++++++++++++++++++++++++++-------------- 1 file changed, 170 insertions(+), 66 deletions(-) diff --git a/dispOptions.sh b/dispOptions.sh index 87664b4..58f1051 100755 --- a/dispOptions.sh +++ b/dispOptions.sh @@ -1,92 +1,196 @@ #!/bin/bash # 一些输出帮助文档的函数 # 显示命令简介 两个参数 命令名称 命令简介 +#function dispTitle(){ +# winWidth=`tput cols` +# +# cutLength=`expr ${winWidth} - 19` +# segment=${2:0:${cutLength}} +# printf "%s\n%-10s%s\n" "${1}" " " "${segment}" +# +# times=1 +# while (( ${#segment} == ${cutLength} )) +# do +# st=`expr ${cutLength} \* ${times}` +# segment=${2:${st}:${cutLength}} +# printf "%-10s%s\n" " " "${segment}" +# times=`expr ${times} + 1` +# done +# printf "\n" +#} + function dispTitle(){ winWidth=`tput cols` + message=${2} - cutLength=`expr ${winWidth} - 19` - segment=${2:0:${cutLength}} - printf "%s\n%-10s%s\n" "${1}" " " "${segment}" + printf "%s\n%-9s" "${1}" - times=1 - while (( ${#segment} == ${cutLength} )) - do - st=`expr ${cutLength} \* ${times}` - segment=${2:${st}:${cutLength}} - printf "%-10s%s\n" " " "${segment}" - times=`expr ${times} + 1` + head_length=19 + print_length=${head_length} + for segment in ${message[@]}; do + print_length=`expr ${print_length} + ${#segment} + 1` + if [[ ${print_length} -le ${winWidth} ]]; then + printf " %s" "${segment}" + else + print_length=`expr ${head_length} + ${#segment} + 1` + printf "\n%-10s%s" " " "${segment}" + fi done - printf "\n" + printf "\n\n" } + # 显示作者信息 一个参数 作者信息 +#function dispAuthorInfo(){ +# winWidth=`tput cols` +# +# cutLength=`expr ${winWidth} - 19` +# segment=${1:0:${cutLength}} +# +# printf "%-10s%s\n" "Author:" "${segment}" +# +# times=1 +# while (( ${#segment} == ${cutLength} )) +# do +# st=`expr ${cutLength} \* ${times}` +# segment=${1:${st}:${cutLength}} +# printf "%-10s%s\n" " " "${segment}" +# times=`expr ${times} + 1` +# done +# printf "\n" +#} + function dispAuthorInfo(){ winWidth=`tput cols` + message=${1} - cutLength=`expr ${winWidth} - 19` - segment=${1:0:${cutLength}} + printf "%-9s" "Author:" - printf "%-10s%s\n" "Author:" "${segment}" + head_length=19 + print_length=${head_length} + for segment in ${message[@]}; do + print_length=`expr ${print_length} + ${#segment} + 1` + if [[ ${print_length} -le ${winWidth} ]]; then + printf " %s" "${segment}" + else + print_length=`expr ${head_length} + ${#segment} + 1` + printf "\n%-10s%s" " " "${segment}" + fi + done + printf "\n\n" +} - times=1 - while (( ${#segment} == ${cutLength} )) - do - st=`expr ${cutLength} \* ${times}` - segment=${1:${st}:${cutLength}} - printf "%-10s%s\n" " " "${segment}" - times=`expr ${times} + 1` +# 显示用法 一个参数 做法信息 +#function dispUsage(){ +# winWidth=`tput cols` +# +# cutLength=`expr ${winWidth} - 19` +# segment=${1:0:${cutLength}} +# +# printf "%-10s%s\n" "Usage:" "${segment}" +# +# times=1 +# while (( ${#segment} == ${cutLength} )) +# do +# st=`expr ${cutLength} \* ${times}` +# segment=${1:${st}:${cutLength}} +# printf "%-10s%s\n" " " "${segment}" +# times=`expr ${times} + 1` +# done +# printf "\nOptions:\n" +#} + +function dispUsage(){ + winWidth=`tput cols` + message=${1} + + printf "%-9s" "Usage:" + + head_length=19 + print_length=${head_length} + for segment in ${message[@]}; do + print_length=`expr ${print_length} + ${#segment} + 1` + if [[ ${print_length} -le ${winWidth} ]]; then + printf " %s" "${segment}" + else + print_length=`expr ${head_length} + ${#segment} + 1` + printf "\n%-10s%s" " " "${segment}" + fi + done + printf "\n\n" +} + +# 短命令说明 两个参数 命令符号 命令说明 +#function dispOptionShort(){ +# winWidth=`tput cols` +# +# cutLength=`expr ${winWidth} - 19` +# segment=${2:0:${cutLength}} +# printf "%-4s%-6s%s\n" " " "${1}" "${segment}" +# +# times=1 +# while (( ${#segment} == ${cutLength} )) +# do +# st=`expr ${cutLength} \* ${times}` +# segment=${2:${st}:${cutLength}} +# printf "%-10s%s\n" " " "${segment}" +# times=`expr ${times} + 1` +# done +#} + +function dispOptionShort(){ + winWidth=`tput cols` + message=${2} + + printf "%-4s%-5s" " " "${1}" + + head_length=19 + print_length=${head_length} + for segment in ${message[@]}; do + print_length=`expr ${print_length} + ${#segment} + 1` + if [[ ${print_length} -le ${winWidth} ]]; then + printf " %s" "${segment}" + else + print_length=`expr ${head_length} + ${#segment} + 1` + printf "\n%-10s%s" " " "${segment}" + fi done printf "\n" } -# 显示用法 一个参数 做法信息 -function dispUsage(){ - winWidth=`tput cols` - cutLength=`expr ${winWidth} - 19` - segment=${1:0:${cutLength}} - - printf "%-10s%s\n" "Usage:" "${segment}" - - times=1 - while (( ${#segment} == ${cutLength} )) - do - st=`expr ${cutLength} \* ${times}` - segment=${1:${st}:${cutLength}} - printf "%-10s%s\n" " " "${segment}" - times=`expr ${times} + 1` - done - printf "\nOptions:\n" -} -# 短命令说明 两个参数 命令符号 命令说明 -function dispOptionShort(){ - winWidth=`tput cols` - - cutLength=`expr ${winWidth} - 19` - segment=${2:0:${cutLength}} - printf "%-4s%-6s%s\n" " " "${1}" "${segment}" - - times=1 - while (( ${#segment} == ${cutLength} )) - do - st=`expr ${cutLength} \* ${times}` - segment=${2:${st}:${cutLength}} - printf "%-10s%s\n" " " "${segment}" - times=`expr ${times} + 1` - done -} # 长命令说明 三个参数 短命令符号 长命令符号 命令说明 +#function dispOptionLong(){ +# winWidth=`tput cols` +# +# cutLength=`expr ${winWidth} - 32` +# segment=${3:0:${cutLength}} +# printf "%-4s%-6s%-12s%s\n" " " "${1}" "${2}" "${segment}" +# +# times=1 +# while (( ${#segment} == ${cutLength} )) +# do +# st=`expr ${cutLength} \* ${times}` +# segment=${3:${st}:${cutLength}} +# printf "%-22s%s\n" " " "${segment}" +# times=`expr ${times} + 1` +# done +#} + function dispOptionLong(){ winWidth=`tput cols` + message=${3} - cutLength=`expr ${winWidth} - 32` - segment=${3:0:${cutLength}} - printf "%-4s%-6s%-12s%s\n" " " "${1}" "${2}" "${segment}" + printf "%-4s%-6s%-11s" " " "${1}" "${2}" - times=1 - while (( ${#segment} == ${cutLength} )) - do - st=`expr ${cutLength} \* ${times}` - segment=${3:${st}:${cutLength}} - printf "%-22s%s\n" " " "${segment}" - times=`expr ${times} + 1` + head_length=32 + print_length=${head_length} + for segment in ${message[@]}; do + print_length=`expr ${print_length} + ${#segment} + 1` + if [[ ${print_length} -le ${winWidth} ]]; then + printf " %s" "${segment}" + else + print_length=`expr ${head_length} + ${#segment} + 1` + printf "\n%-22s%s" " " "${segment}" + fi done + printf "\n" } \ No newline at end of file