Advertisement
Guest User

Untitled

a guest
Jan 24th, 2017
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.07 KB | None | 0 0
  1. #!/bin/sh
  2.  
  3. colorscan() {
  4. awk '
  5. function fg(c, s) { return sprintf("\033[38;5;%03dm%s\033[0m", c, s) }
  6. function so(s) { return sprintf("\033[1m%s\033[0m", s) }
  7. /^>/ { print so(fg(119, $0)); next }
  8. /^ *\\_/ { print fg(242, $0); next }
  9. { print }'
  10. }
  11.  
  12. clear_buf() {
  13. i=0
  14. while [ "$i" -le "$1" ]; do
  15. tput cup "$i" 0
  16. tput el
  17. : $(( i=i+1 ))
  18. done
  19. }
  20.  
  21. hline=0
  22. bline=1
  23. bcol=1
  24.  
  25. body_buf=$(mktemp /tmp/.mcurse_body.XXXXX)
  26. body_len=
  27. body_max=
  28.  
  29. term_init() {
  30. cols=$(tput cols)
  31. rows=$(tput lines)
  32. start_headers=$(( rows / 3 ))
  33. start_body=$(( start_headers + 1 ))
  34. end_body=$(( rows - start_body - 2))
  35. term_clear_headers=$(clear_buf "$start_headers"; tput cup 0 0)
  36. term_clear_body=$(tput cup "$(( start_body ))" 0; tput ed)
  37. term_move_status=$(tput cup "$(( rows ))" $(( cols - 5 )); tput el)
  38. term_move_cmd=$(tput cup "$(( rows ))" 0)
  39. term_init_cmd=$(tput cnorm; tput el)
  40. term_done_cmd=$(tput civis)
  41. }
  42.  
  43. statusline() {
  44. printf "%s%s" "$term_move_status" "$@"
  45. }
  46.  
  47. cmdline() {
  48. printf "%s%s:" "$term_move_cmd" "$term_init_cmd"
  49. stty "$stty_default"
  50. tput cnorm
  51. read -r cmd
  52. case "$cmd" in
  53. "|"*) ;;
  54. "!"*) ;;
  55. esac
  56. printf "%s" "$term_done_cmd"
  57. stty -echo -icanon
  58. headers
  59. body
  60. }
  61.  
  62. update() {
  63. bline=1
  64. [ "$bcol" -le 0 ] && bcol=1
  65. mshow 2>/dev/null | mcolor | cut -c "$(( bcol ))-$(( cols + bcol - 1 ))" | trunc_lines >"$body_buf"
  66. body_len=$(wc -l <"$body_buf")
  67. : $(( body_len = body_len + 1 ))
  68. : $(( body_max = body_len - end_body ))
  69. update_body=1
  70. }
  71.  
  72. trunc_lines() {
  73. t=$(tput el)
  74. while read -r l; do
  75. printf "%s%s\n" "$l" "$t"
  76. done
  77. }
  78.  
  79. headers() {
  80. printf "%s" "$term_clear_headers"
  81.  
  82. : $(( x = hline - (start_headers / 2) ))
  83. : $(( y = hline + (start_headers / 2) ))
  84.  
  85. [ "$x" -gt 0 ] && x="+$x"
  86. [ "$y" -gt 0 ] && y="+$y"
  87.  
  88. COLUMNS=$cols mscan ".$x:.$y" 2>/dev/null | colorscan
  89. }
  90.  
  91. body() {
  92. [ "$bline" -gt "$body_max" ] && bline=$body_max
  93. [ "$bline" -lt 1 ] && bline=1
  94.  
  95. : $(( x = bline ))
  96. : $(( y = end_body + bline ))
  97.  
  98. [ "${update_body}" = "${x},${y}p" ] && return
  99.  
  100. printf "%s" "$term_clear_body"
  101.  
  102. update_body="${x},${y}p"
  103. sed -n "$update_body" "$body_buf"
  104.  
  105. [ "$body_len" -gt "$end_body" ] \
  106. && statusline "$(( 100 * y / body_len ))%"
  107. }
  108.  
  109. draw() {
  110. headers
  111. body
  112. }
  113.  
  114. init() {
  115. tput smcup # save position
  116. tput civis # cursor invisible
  117. term_init
  118.  
  119. stty_default=$(stty -g)
  120. stty -echo -icanon
  121.  
  122. update
  123. draw
  124. }
  125.  
  126. close() {
  127. tput sgr0 # reset char attributes
  128. tput cnorm # normal cursor
  129. tput rmcup # restore position
  130. stty "$stty_default" # restore stty settings
  131. [ -e "$body_buf" ] && rm "$body_buf"
  132. exit "${1-0}"
  133. }
  134.  
  135. readchar() {
  136. c=$(dd bs=1 count=1 2>/dev/null)
  137. printf '%d' "'$c"
  138. }
  139.  
  140.  
  141. in_buf=
  142. in_cnt1=1
  143. in_cnt2=1
  144. in_cur=
  145.  
  146. in_read() {
  147. set -- $in_buf
  148. [ "$#" -eq 0 ] && in_cur=$(readchar) && return
  149. in_cur=$1; shift; in_buf=$@
  150. }
  151.  
  152. in_back() {
  153. set -- "$in_cur" $in_buf
  154. in_buf=$@
  155. }
  156.  
  157. in_prefix() {
  158. n=
  159. while [ "$in_cur" -le 59 ] && [ "$in_cur" -ge 47 ]; do
  160. : $(( n = n * 10 + $(printf \\$(printf "%03o" "$in_cur")) ))
  161. in_read
  162. done
  163. [ "$1" -eq "1" ] && in_cnt1=${n:-1} || in_cnt2=${n:-1}
  164. }
  165.  
  166. in_motion() {
  167. case "$in_cur" in
  168. # $
  169. 36) mseq -C "$" ;;
  170. # ^
  171. 94) mseq -C 1 ;;
  172. # j
  173. 106) mseq -C ".+$in_cnt1" ;;
  174. # k
  175. 107) mseq -C ".-$in_cnt1" ;;
  176. # P
  177. 80) mseq -C "$(mseq ".-1=" | head -n1 | mscan -n)" ;;
  178. # N
  179. 78) mseq -C "$(( $(mseq ".=" | tail -n1 | mscan -n) + 1 ))" ;;
  180. # p
  181. 112) mseq -C "$(mseq "._" | head -n1 | mscan -n)" ;;
  182. # n
  183. 110) mseq -C "$(mseq "._" | tail -n1 | mscan -n)" ;;
  184. # [
  185. 91) mseq -C "$(mseq ".=" | head -n1 | mscan -n)" ;;
  186. # ]
  187. 93) mseq -C "$(mseq ".=" | tail -n1 | mscan -n)" ;;
  188. *) return 1 ;;
  189. esac
  190. }
  191.  
  192. in_motionln() {
  193. cnt=$(( in_cnt1 * in_cnt2 ))
  194. mv=
  195. case "$in_cur" in
  196. # return j +
  197. 0|106|125) mv=".:$(mseq ".+$cnt" | mscan -n)" ;;
  198. # - k
  199. 45|107) mv="$(mseq ".-$cnt" | mscan -n):." ;;
  200. # G
  201. 71) mv=".:$" ;;
  202. *) return 1 ;;
  203. esac
  204. }
  205.  
  206. in_action() {
  207. c=$in_cur
  208. in_read
  209. in_prefix 2
  210. in_motionln || [ "$in_cur" -eq "$c" ] && mv="."
  211. case "$c" in
  212. # d
  213. 100)
  214. mseq "$mv" | mflag -v -S : >/dev/null
  215. mseq -f : | mseq -S
  216. headers
  217. ;;
  218. # u
  219. 117)
  220. mseq "$mv" | mflag -v -s : >/dev/null
  221. mseq -f : | mseq -S
  222. headers
  223. ;;
  224. *) return 1 ;;
  225. esac
  226. }
  227.  
  228. in_esc() {
  229. stty min 0 time 1
  230. c=$(readchar)
  231. rv=0
  232. echo "esc $c" >>./log
  233. case "$c" in
  234. 91)
  235. c=$(readchar)
  236. # page up/down
  237. [ "$c" -eq 53 ] && : $(( bline-=end_body ))
  238. [ "$c" -eq 54 ] && : $(( bline+=end_body ))
  239. c=$(readchar)
  240. body
  241. ;;
  242. *) c=$(readchar); c=$(readchar); rv=1 ;;
  243. esac
  244. stty min 1 time 0
  245. return $rv
  246. }
  247.  
  248. trap 'close 130;' INT TERM
  249.  
  250. init
  251.  
  252. while :; do
  253. in_buf=
  254. in_cur=
  255. int_cnt1=1
  256. int_cnt2=1
  257.  
  258. in_read
  259. [ "$in_cur" -eq 27 ] && in_esc && continue
  260.  
  261. in_prefix 1
  262. in_motion && update && draw && continue
  263.  
  264. case "$in_cur" in
  265. # d u
  266. 100|117) in_action ;;
  267. # D
  268. 68) in_cur="100"; in_back; in_action ;; # input buffer to dd
  269. # U
  270. 85) in_cur="117"; in_back; in_action ;; # input buffer to uu
  271. # e
  272. 101) ${EDITOR=ed} $(mseq .) && update && draw ;;
  273. # v
  274. 118) ${VISUAL=vi} $(mseq .) && update && draw ;;
  275. # L | C-l
  276. 76|12) tput clear && term_init && update && draw ;;
  277. # :
  278. 58) cmdline ;;
  279. # q
  280. 113) close ;;
  281. # c
  282. 99) hline=0 && headers ;;
  283. # J
  284. 74) : $(( bline+=in_cnt1 )) && body ;;
  285. # K
  286. 75) : $(( bline-=in_cnt1 )) && body ;;
  287. *) ;;
  288. esac
  289. printf "%s" "$term_move_cmd"
  290. done
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement