Guest User

Untitled

a guest
May 21st, 2018
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.01 KB | None | 0 0
  1. #!/bin/bash
  2. :<<'#license'
  3. DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
  4. Version 2, December 2004
  5.  
  6. Copyright (C) 2004 Sam Hocevar <sam@hocevar.net>
  7.  
  8. Everyone is permitted to copy and distribute verbatim or modified
  9. copies of this license document, and changing it is allowed as long
  10. as the name is changed.
  11.  
  12. DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
  13. TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
  14.  
  15. 0. You just DO WHAT THE FUCK YOU WANT TO.
  16. #license
  17.  
  18. :<<'#version'
  19. 0.0.1
  20. #version
  21.  
  22. :<<'#keybind'
  23. Default key bind is vi-like
  24. press h => move up directory
  25. press l(L) => chenge directory or edit file or execute file
  26. press j => move up line
  27. press k => move down line
  28. press e => select next file
  29. press b => select prev file
  30. press : => eval mode
  31. #keybind
  32.  
  33. #########################################################
  34.  
  35. declare STATUS=$(basename $(pwd)) # カレントディレクトリの名前
  36. declare -a DIR=() # カレントディレクトリのファイルを一つづつ格納
  37. declare -a LINE=() # DIRの要素を1行づつにくぎった値
  38. declare -i array_length=0 # DIR配列の最大値
  39. declare -i updown=0 # LINEの添字につかう
  40.  
  41. dir_read()
  42. {
  43. array_length=0
  44. local -i i=0
  45. local -i length=0
  46. DIR=($(ls -F))
  47. for DIR in ${DIR[@]};
  48. do
  49. if [ $length -ge 40 ]; then
  50. LINE=("${LINE[@]}" $(expr $i - 1))
  51. array_length=$(expr $array_length + 1)
  52. length=0
  53. fi
  54. length=$(expr $length + ${#DIR[$i]})
  55. i=$(expr $i + 1)
  56. done
  57. LINE=("${LINE[@]}" $(expr $i - 1))
  58. array_length=$(expr $array_length + 1)
  59. }
  60. #行のクリアとカーソルのポジションを先頭に移動する
  61. line_erase()
  62. { # \x1B[ => 16進数表記のESC文字
  63. printf "\x1B[2K\x1B[1G" # 2K => カレント行の削除
  64. }
  65. declare -a word=(0 1)
  66. declare -i x_max=0
  67. body_print()
  68. {
  69. word=(0 1)
  70. x_max=0
  71. local -i i=0
  72. local -i word_length=0
  73. if [ $updown -eq 0 ]; then
  74. while [ $i -lt ${LINE[0]} ];
  75. do
  76. printf "%s " ${DIR[$i]}
  77. word_length=$(expr $word_length + ${#DIR[$i]})
  78. word=("${word[@]}" $word_length)
  79. i=$(expr $i + 1)
  80. x_max=$(expr $x_max + 1)
  81. done
  82. else
  83. i=$(expr ${LINE[$(expr $updown - 1 )]})
  84. while [ $i -lt ${LINE[$updown]} ];
  85. do
  86. printf "%s " ${DIR[$i]}
  87. word_length=$(expr $word_length + ${#DIR[$i]})
  88. word=("${word[@]}" $word_length)
  89. i=$(expr $i + 1)
  90. x_max=$(expr $x_max + 1)
  91. done
  92. fi
  93. }
  94. line_print()
  95. {
  96. printf "< %-6s > " ${STATUS:0:6}
  97. printf "\x1B[?25l"
  98. body_print
  99. printf "(%3d/%3d)" $(expr $updown + 1) $array_length
  100. printf "\x1B[?25h"
  101. printf "\x1B[10G"
  102. }
  103.  
  104. move_cursor()
  105. {
  106. if [ $cursor_move_flag -ne 0 ]; then
  107. if [ $x -gt $x_max ]; then
  108. x=$(expr $x_max - 1)
  109. elif [ $x -le 0 ]; then
  110. x=0
  111. elif [ $x -eq 0 ]; then
  112. printf "\x1B[10G"
  113. printf "\x1B[0C"
  114. elif [ $x -eq 1 ]; then
  115. printf "\x1B[10G"
  116. printf "\x1B[2C"
  117. else
  118. printf "\x1B[10G"
  119. printf "\x1B[%dC" $(expr ${word[$x]} + $x + 1)
  120. fi
  121. fi
  122. }
  123.  
  124. line_down()
  125. {
  126. updown=$(expr $updown + 1)
  127. if [ $updown -gt $(expr $array_length - 1) ]; then
  128. updown=$(expr $updown - 1)
  129. fi
  130. }
  131. line_up()
  132. {
  133. updown=$(expr $updown - 1)
  134. if [ $updown -le -1 ]; then
  135. updown=0
  136. fi
  137. }
  138. declare -i cursor_move_flag=0
  139. declare -i x=0
  140. file_select()
  141. {
  142. local file
  143. file=$(body_print | awk -v position="${x}" '{printf $position}' | tr '\*' ' ')
  144. if [ -d "$file" ]; then
  145. cd "$file" >/dev/null 2>&1
  146. STATUS=$(basename $(pwd))
  147. updown=0
  148. dir_read
  149. x=0
  150. elif [ -x "$file" ]; then
  151. line_erase
  152. read -p "Do you want to run $file ? y/n: " anser
  153. case $anser in
  154. 'y')
  155. ./"$file"
  156. ;;
  157.  
  158. 'n')
  159. :
  160. ;;
  161. *)
  162. ;;
  163. esac
  164. elif [ -f "$file" ]; then
  165. vim "$file"
  166. fi
  167. }
  168.  
  169. dir_read
  170. line_erase
  171. line_print
  172. while :
  173. do
  174.  
  175. read -s -n1 c
  176.  
  177. case $c in
  178.  
  179. 'j')
  180. line_down
  181. x=0
  182. line_erase
  183. line_print
  184. ;;
  185.  
  186. 'k')
  187. line_up
  188. line_erase
  189. line_print
  190. x=0
  191. ;;
  192.  
  193. 'q')
  194. break
  195. ;;
  196.  
  197. 'h')
  198. { #もどるボタン( < )を押したっぽくみせる
  199. printf "\x1B[1G"
  200. sleep 0.1
  201. printf "\x1B[10G"
  202. }
  203. #pushdするかどうかの判断
  204. if [ "$STATUS" != "/" ]; then
  205. pushd . >/dev/null 2>&1
  206. cd ..
  207. STATUS=$(basename $(pwd))
  208. updown=0
  209. fi
  210. dir_read
  211. x=0
  212. line_erase
  213. line_print
  214. ;;
  215.  
  216. "l")
  217. if [ $cursor_move_flag -eq 0 ]; then
  218. popd >/dev/null 2>&1
  219. STATUS=$(basename $(pwd))
  220. updown=0
  221. dir_read
  222. x=0
  223. else
  224. file_select
  225. fi
  226. line_erase
  227. line_print
  228. ;;
  229.  
  230. 'e')
  231. x=$(expr $x + 1)
  232. cursor_move_flag=1
  233. move_cursor
  234. ;;
  235. 'b')
  236. x=$(expr $x - 1)
  237. move_cursor
  238. ;;
  239.  
  240. ':') #入力した文字列をevalする
  241. line_erase
  242. read -e -p ": " command; eval "$command"
  243. line_erase
  244. line_print
  245. ;;
  246.  
  247. esac
  248. done
  249. line_erase
  250. printf "\x1B[1Gbye!\n"
Add Comment
Please, Sign In to add comment