#!/bin/bash
[ -z $1 ] echo "Input is invalid! " exit 1
txt=($(cat $1|sed 's/ \+/_/g'))
lines=${#txt[@]}
max_show=10 #最多显示行数(超过总行数则设为总行数)
[ ${max_show} -gt ${lines} ] max_show=${lines}
hlt_line=3 #高亮显示位置(不超过总行1/2,设为3实际是第4行)
[ ${hlt_line} -gt $(( lines / 2 )) ] hlt_line=$(( lines / 2 ))
sTTY=$(stty -g) #保存终端设置
clear
echo -ne "\033[?25l" #隐藏光标
spaces() {
local i j n
i=0; n=0
for (( j=0; j = ${lines}; j++ ));do
[ $i -lt ${#txt[$j]} ] (( i = ${#txt[$j]} )) (( n = $j ))
done
echo ${txt[$n]}|sed 's/./ /g'
}
space_line=$(spaces)
#传递的参数是欲显示行的序数:show 1 表示第一行高亮显示
show() {
local i j text crt_line
j=$1
for (( i=0; i max_show; i++ ));do
crt_line=$(( j + i - hlt_line ))
if [ $i -eq ${hlt_line} ] [ ${crt_line} -ge 0 ] [ ${crt_line} -le ${lines} ];then
text="\033[;32m"${txt[${crt_line}]}"\033[0m"
elif [ ${crt_line} -lt 0 ] || [ ${crt_line} -gt ${lines} ];then
text=${space_line}
else
text=${txt[${crt_line}]}
fi
echo -ne "\033[$((i+3));0H${space_line}"
echo -ne "\033[$((i+3));6H${text}"
done
}
#测试
for (( j = 0; j lines + hlt_line +1 ; j++ ));do
show $j
sleep 1
done