Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/sh
- colorscan() {
- awk '
- function fg(c, s) { return sprintf("\033[38;5;%03dm%s\033[0m", c, s) }
- function so(s) { return sprintf("\033[1m%s\033[0m", s) }
- /^>/ { print so(fg(119, $0)); next }
- /^ *\\_/ { print fg(242, $0); next }
- { print }'
- }
- clear_buf() {
- i=0
- while [ "$i" -le "$1" ]; do
- tput cup "$i" 0
- tput el
- : $(( i=i+1 ))
- done
- }
- hline=0
- bline=1
- bcol=1
- body_buf=$(mktemp /tmp/.mcurse_body.XXXXX)
- body_len=
- body_max=
- term_init() {
- cols=$(tput cols)
- rows=$(tput lines)
- start_headers=$(( rows / 3 ))
- start_body=$(( start_headers + 1 ))
- end_body=$(( rows - start_body - 2))
- term_clear_headers=$(clear_buf "$start_headers"; tput cup 0 0)
- term_clear_body=$(tput cup "$(( start_body ))" 0; tput ed)
- term_move_status=$(tput cup "$(( rows ))" $(( cols - 5 )); tput el)
- term_move_cmd=$(tput cup "$(( rows ))" 0)
- term_init_cmd=$(tput cnorm; tput el)
- term_done_cmd=$(tput civis)
- }
- statusline() {
- printf "%s%s" "$term_move_status" "$@"
- }
- cmdline() {
- printf "%s%s:" "$term_move_cmd" "$term_init_cmd"
- stty "$stty_default"
- tput cnorm
- read -r cmd
- case "$cmd" in
- "|"*) ;;
- "!"*) ;;
- esac
- printf "%s" "$term_done_cmd"
- stty -echo -icanon
- headers
- body
- }
- update() {
- bline=1
- [ "$bcol" -le 0 ] && bcol=1
- mshow 2>/dev/null | mcolor | cut -c "$(( bcol ))-$(( cols + bcol - 1 ))" | trunc_lines >"$body_buf"
- body_len=$(wc -l <"$body_buf")
- : $(( body_len = body_len + 1 ))
- : $(( body_max = body_len - end_body ))
- update_body=1
- }
- trunc_lines() {
- t=$(tput el)
- while read -r l; do
- printf "%s%s\n" "$l" "$t"
- done
- }
- headers() {
- printf "%s" "$term_clear_headers"
- : $(( x = hline - (start_headers / 2) ))
- : $(( y = hline + (start_headers / 2) ))
- [ "$x" -gt 0 ] && x="+$x"
- [ "$y" -gt 0 ] && y="+$y"
- COLUMNS=$cols mscan ".$x:.$y" 2>/dev/null | colorscan
- }
- body() {
- [ "$bline" -gt "$body_max" ] && bline=$body_max
- [ "$bline" -lt 1 ] && bline=1
- : $(( x = bline ))
- : $(( y = end_body + bline ))
- [ "${update_body}" = "${x},${y}p" ] && return
- printf "%s" "$term_clear_body"
- update_body="${x},${y}p"
- sed -n "$update_body" "$body_buf"
- [ "$body_len" -gt "$end_body" ] \
- && statusline "$(( 100 * y / body_len ))%"
- }
- draw() {
- headers
- body
- }
- init() {
- tput smcup # save position
- tput civis # cursor invisible
- term_init
- stty_default=$(stty -g)
- stty -echo -icanon
- update
- draw
- }
- close() {
- tput sgr0 # reset char attributes
- tput cnorm # normal cursor
- tput rmcup # restore position
- stty "$stty_default" # restore stty settings
- [ -e "$body_buf" ] && rm "$body_buf"
- exit "${1-0}"
- }
- readchar() {
- c=$(dd bs=1 count=1 2>/dev/null)
- printf '%d' "'$c"
- }
- in_buf=
- in_cnt1=1
- in_cnt2=1
- in_cur=
- in_read() {
- set -- $in_buf
- [ "$#" -eq 0 ] && in_cur=$(readchar) && return
- in_cur=$1; shift; in_buf=$@
- }
- in_back() {
- set -- "$in_cur" $in_buf
- in_buf=$@
- }
- in_prefix() {
- n=
- while [ "$in_cur" -le 59 ] && [ "$in_cur" -ge 47 ]; do
- : $(( n = n * 10 + $(printf \\$(printf "%03o" "$in_cur")) ))
- in_read
- done
- [ "$1" -eq "1" ] && in_cnt1=${n:-1} || in_cnt2=${n:-1}
- }
- in_motion() {
- case "$in_cur" in
- # $
- 36) mseq -C "$" ;;
- # ^
- 94) mseq -C 1 ;;
- # j
- 106) mseq -C ".+$in_cnt1" ;;
- # k
- 107) mseq -C ".-$in_cnt1" ;;
- # P
- 80) mseq -C "$(mseq ".-1=" | head -n1 | mscan -n)" ;;
- # N
- 78) mseq -C "$(( $(mseq ".=" | tail -n1 | mscan -n) + 1 ))" ;;
- # p
- 112) mseq -C "$(mseq "._" | head -n1 | mscan -n)" ;;
- # n
- 110) mseq -C "$(mseq "._" | tail -n1 | mscan -n)" ;;
- # [
- 91) mseq -C "$(mseq ".=" | head -n1 | mscan -n)" ;;
- # ]
- 93) mseq -C "$(mseq ".=" | tail -n1 | mscan -n)" ;;
- *) return 1 ;;
- esac
- }
- in_motionln() {
- cnt=$(( in_cnt1 * in_cnt2 ))
- mv=
- case "$in_cur" in
- # return j +
- 0|106|125) mv=".:$(mseq ".+$cnt" | mscan -n)" ;;
- # - k
- 45|107) mv="$(mseq ".-$cnt" | mscan -n):." ;;
- # G
- 71) mv=".:$" ;;
- *) return 1 ;;
- esac
- }
- in_action() {
- c=$in_cur
- in_read
- in_prefix 2
- in_motionln || [ "$in_cur" -eq "$c" ] && mv="."
- case "$c" in
- # d
- 100)
- mseq "$mv" | mflag -v -S : >/dev/null
- mseq -f : | mseq -S
- headers
- ;;
- # u
- 117)
- mseq "$mv" | mflag -v -s : >/dev/null
- mseq -f : | mseq -S
- headers
- ;;
- *) return 1 ;;
- esac
- }
- in_esc() {
- stty min 0 time 1
- c=$(readchar)
- rv=0
- echo "esc $c" >>./log
- case "$c" in
- 91)
- c=$(readchar)
- # page up/down
- [ "$c" -eq 53 ] && : $(( bline-=end_body ))
- [ "$c" -eq 54 ] && : $(( bline+=end_body ))
- c=$(readchar)
- body
- ;;
- *) c=$(readchar); c=$(readchar); rv=1 ;;
- esac
- stty min 1 time 0
- return $rv
- }
- trap 'close 130;' INT TERM
- init
- while :; do
- in_buf=
- in_cur=
- int_cnt1=1
- int_cnt2=1
- in_read
- [ "$in_cur" -eq 27 ] && in_esc && continue
- in_prefix 1
- in_motion && update && draw && continue
- case "$in_cur" in
- # d u
- 100|117) in_action ;;
- # D
- 68) in_cur="100"; in_back; in_action ;; # input buffer to dd
- # U
- 85) in_cur="117"; in_back; in_action ;; # input buffer to uu
- # e
- 101) ${EDITOR=ed} $(mseq .) && update && draw ;;
- # v
- 118) ${VISUAL=vi} $(mseq .) && update && draw ;;
- # L | C-l
- 76|12) tput clear && term_init && update && draw ;;
- # :
- 58) cmdline ;;
- # q
- 113) close ;;
- # c
- 99) hline=0 && headers ;;
- # J
- 74) : $(( bline+=in_cnt1 )) && body ;;
- # K
- 75) : $(( bline-=in_cnt1 )) && body ;;
- *) ;;
- esac
- printf "%s" "$term_move_cmd"
- done
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement