Guest User

KingBash First Bash Version

a guest
Jan 23rd, 2013
33
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 8.70 KB | None | 0 0
  1. #! /bin/echo This file must be sourced: .
  2.  
  3. function kb_highlight() {
  4.     case $cmdname in
  5. #Video
  6. mplayer|mwrap|vlc|gmplayer|smplayer|mencoder|kmplayer|Parole|whaawmp|dragonplayer|ffmpeg)
  7. extlist='*.mkv|*.m4v|*.mp*|*.avi|*.wmv|*.rmvb|*.as?|*.divx|*.vob|*.ogm|*.rm|*.flv|*.part|*.iso|*.ogg|*.wav|*.flac|*.m4a'
  8. ;;
  9. #Audio
  10. mpg123|mpg123s|mpg321|mp3blaster|cmus|cplay|moc|xmms|xmms2|sonata|deadbeef|ogg123|mnama)
  11. extlist='*.mp?|*.aac|*.wav|*.ogg|*.gsm|*.dct|*.flac|*.au|*.aiff|*.vox|*.wma|*.aac|*.ra|*.m4a'
  12. ;;
  13. #PDF
  14. llpp|xpdf|epdfview|evince|foxit|mupdf|okular|apvlv|zathura)
  15. extlist='*.pdf'
  16. ;;
  17. #Images
  18. feh|geeqie|gqview|eog|gpicview|gthumb|mirage|qiv|ristretto|xnview|xv|viewnior)
  19. extlist='*.jpg|*.jpeg|*.png|*.gif|*.bmp|*.ico*|*.tif*'
  20. ;;
  21. #Games
  22. sdlmame|openmsx|msxplay|zsnes|desmume|VirtualBoy)
  23. extlist='*.rom|*.dsk'
  24. ;;
  25. #Wine
  26. wine|winewrap|wineconsole)
  27. extlist='*.exe|*.com|*.bat'
  28. ;;
  29. #Archives
  30. atool|x|xi|gunzip|extract|unzip|unrar|zip|rar|7z|mcomix|v)
  31. extlist='*.tgz|*.zip|*.rar|*.bz2|*.gz|*.tar|*.exe|*.pk3|*.lha|*.Z|*.lzma'
  32. ;;
  33. #Text
  34. vim|nano|acme|beaver|geany|leafpad|medit|mousepad|pyroom|sam|vi|gvim|emacs|tea|scite)
  35. extlist='*.txt|*.rc|*.sh|*.c|*.bash|*.py|*.ini'
  36. ;;
  37. *) extlist='-Unmatch-';;
  38. esac
  39. }
  40.  
  41. function kb_menu() {
  42.     local end=0 h dirlist notdirlist total colorsw page pages sel=0 count key newk change=1 lim=7 displayres tempfile
  43.     tput rmam #don't wrap long lines
  44.     IFS=$'\n'
  45.     prompt="$2"
  46.  
  47.     while ((end==0)); do
  48.  
  49.     # build a new list
  50.     if ((change == 1)); then
  51.         change=0
  52.         if [[ "$1" == command ]]; then
  53.             displayres=( $(compgen -A command -A alias -A builtin -A function $prompt | sort -u) )
  54.         elif [[ "$1" == file ]]; then
  55.             colorsw=0
  56.             dirlist=()
  57.             notdirlist=()
  58.             displayres=()
  59. #           for tempfile in $prompt*; do
  60.             for tempfile in $prompt@($extlist); do
  61.                 [[ -d "$tempfile" ]] && tempfile+=/
  62.                 if [[ "$cmdname" =~ ^(c|cd|popd|rmdir)$ ]]; then
  63.                   [[ -d "$tempfile" ]] && dirlist+=( "$tempfile" ) || notdirlist+=( "$tempfile" )
  64.                 else
  65.                    displayres+=( "$tempfile" )
  66.                 fi
  67.                 ((colorsw++))
  68.             done
  69.             for tempfile in $prompt!($extlist); do
  70.                 [[ -d "$tempfile" ]] && tempfile+=/
  71.                 if [[ "$cmdname" =~ ^(c|cd|popd|rmdir)$ ]]; then
  72.                   [[ -d "$tempfile" ]] && dirlist+=( "$tempfile" ) || notdirlist+=( "$tempfile" )
  73.                 else
  74.                    displayres+=( "$tempfile" )
  75.                 fi
  76.             done
  77.         fi
  78.         if [[ "$cmdname" =~ ^(c|cd|popd|rmdir)$ ]]; then
  79.             colorsw=${#dirlist[@]}
  80.             displayres=( "${dirlist[@]}" "${notdirlist[@]}" )
  81.         fi
  82.         total=${#displayres[@]}
  83.  
  84.         # on zero results retry without front match
  85.         if [[ "$1" == file ]] && ((total==0)); then
  86.             colorsw=0
  87.             if [[ "${prompt//[^\/]}" ]]; then
  88. #           for tempfile in ${prompt%/*}/*${prompt##*/}*; do
  89.             for tempfile in ${prompt%/*}/*${prompt##*/}@($extlist); do
  90.                 [[ -d "$tempfile" ]] && tempfile+=/
  91.                 if [[ "$cmdname" =~ ^(c|cd|popd|rmdir)$ ]]; then
  92.                   [[ -d "$tempfile" ]] && dirlist+=( "$tempfile" ) || notdirlist+=( "$tempfile" )
  93.                 else
  94.                    displayres+=( "$tempfile" )
  95.                 fi
  96.                 ((colorsw++))
  97.             done
  98.             for tempfile in ${prompt%/*}/*${prompt##*/}!($extlist); do
  99.                 [[ -d "$tempfile" ]] && tempfile+=/
  100.                 if [[ "$cmdname" =~ ^(c|cd|popd|rmdir)$ ]]; then
  101.                   [[ -d "$tempfile" ]] && dirlist+=( "$tempfile" ) || notdirlist+=( "$tempfile" )
  102.                 else
  103.                    displayres+=( "$tempfile" )
  104.                 fi
  105.             done
  106.             else
  107. #           for tempfile in *$prompt*; do
  108.             for tempfile in *$prompt@($extlist); do
  109.                 [[ -d "$tempfile" ]] && tempfile+=/
  110.                 if [[ "$cmdname" =~ ^(c|cd|popd|rmdir)$ ]]; then
  111.                   [[ -d "$tempfile" ]] && dirlist+=( "$tempfile" ) || notdirlist+=( "$tempfile" )
  112.                 else
  113.                    displayres+=( "$tempfile" )
  114.                 fi
  115.                 ((colorsw++))
  116.             done
  117.             for tempfile in *$prompt!($extlist); do
  118.                 [[ -d "$tempfile" ]] && tempfile+=/
  119.                 if [[ "$cmdname" =~ ^(c|cd|popd|rmdir)$ ]]; then
  120.                   [[ -d "$tempfile" ]] && dirlist+=( "$tempfile" ) || notdirlist+=( "$tempfile" )
  121.                 else
  122.                    displayres+=( "$tempfile" )
  123.                 fi
  124.             done
  125.             fi
  126.             if [[ "$cmdname" =~ ^(c|cd|popd|rmdir)$ ]]; then
  127.                 colorsw=${#dirlist[@]}
  128.                 displayres=( "${dirlist[@]}" "${notdirlist[@]}" )
  129.             fi
  130.             total=${#displayres[@]}
  131.         fi
  132.         ((total==1)) && prompt=${displayres[0]} && break
  133.         (( total < lim )) && h=$total || h=$lim
  134.         pages=$(( total /lim))
  135.     fi
  136.     ((sel < 0)) && sel=0
  137.     ((sel >= total)) && sel=$((total -1))
  138.  
  139.     page=$((sel /lim))
  140.     echo "p$page/$pages: $before$prompt"$'\e[47m \e[0m '
  141.     echo -ne '\e[A'
  142.     for ((count = lim *(sel /lim); count < lim +lim *(sel /lim); count++)); do
  143.         echo -ne '\e[0m'
  144.         [[ $count == $total && $page == 0 ]] && break || echo
  145.         (($sel == $count)) && echo -n "X " || echo -n "  "
  146.         ((count < colorsw)) && echo -ne '\e[31m'
  147.         echo -n "${displayres[$count]}"
  148.     done
  149.  
  150.     tput civis >&2 #turn cursor invisible
  151.     read -n1 key
  152.     [[ $key ]] || key=ENTER
  153.     #[[ "$key" == ' ' ]] && key=Space
  154.     if [[ "$key" == $'\e' ]]; then
  155.         key=ESC
  156.         while true; do
  157.             read -n 1 -t 0.0001 -s newk
  158.             [[ "$newk" == $'\e' ]] && newk=ESC
  159.             key=${key}${newk}
  160.             [[ -z "$newk" || "$newk" =~ [~A-Da-d] ]] && break
  161.         done
  162.     fi
  163.     case "$key" in
  164.         'ESC[A')    ((sel--));; #UP
  165.         'ESC[B')    ((sel++));; #DOWN
  166.         'ESC[6~')   ((sel+=7));; #PGDN
  167.         'ESC[5~')   ((sel-=7));; #PGUP
  168.         'ESC[4~'|'ESC[F'|'ESC[8~'|'ESCOF')  ((sel=$total));; #END
  169.         'ESC[1~'|'ESC[H'|'ESC[7~'|'ESCOH')  ((sel=0));; #HOME
  170.         'ENTER')    [[ "${displayres[$sel]}" ]] && prompt="${displayres[$sel]}"; end=1;; #ENTER
  171.         'ESC[2~')   appendprompt=\ $prompt; prompt="${displayres[$sel]}"; end=1;; #INSERT
  172.         'ESC')      nospace=1; end=1;; #ESC
  173.         $'\x7f'|$'\x08')    change=1; [[ $prompt ]] && prompt=${prompt::-1};; #BACKSPACE
  174.         'ESC'*) :;; #UNUSED KEY
  175.         *) change=1; prompt+=$key;;
  176.     esac
  177.    
  178.     for (( count=0; count < $h; count++)); do
  179.         echo -ne "\e[2K\e[A\r"
  180.     done
  181.     tput cnorm >&2 #restore cursor visibility
  182.     echo -ne '\e[2K\r'
  183.     done
  184. }
  185. function kb_replace_var() {
  186.     local var fill
  187.     OIFS=$IFS
  188.     IFS=$'\n'
  189.     last="${last/\~/$HOME}"
  190.     [[ "${last//[^$]}" ]] && \
  191.     for var in $(echo "$last" | /usr/bin/grep -o '\$[a-zA-Z_][a-zA-Z0-9_]*' | tr -d '$'); do
  192.         fill=${!var}
  193.         last="${last/\$$var/$fill}"
  194.     done
  195.     IFS=$OIFS
  196. }
  197. function kb_write_prompt() {
  198.     local out escquote lang
  199.     IFS=$' \t\n'
  200.     dir=${1: -1}
  201.     [[ "$dir" == / ]] && dir= || dir=' '
  202.     (( nospace == 1 )) && { dir= && nospace=; } || nospace=\'
  203.     escquote=${1//\'/\'\\\'\'}
  204.     out="$before$nospace$escquote$nospace$dir$appendprompt"
  205.     lang=$LC_ALL
  206.     LC_ALL=C
  207.     ((READLINE_POINT+=${#out} - ${#READFIRST}))
  208.     LC_ALL=$LC_ALL
  209.     READLINE_LINE=$out$READLAST
  210. }
  211. function kb_main() {
  212.     local cmdname extlist appendprompt recursive quotes last before psheight nospace=0 extglob nocaseglob nullglob READFIRST READLAST
  213.     READFIRST="${READLINE_LINE::$READLINE_POINT}"
  214.     READLAST="${READLINE_LINE:$READLINE_POINT}"
  215.     psheight=$( echo -e "$PS1" | wc -l)
  216.     ((psheight--))
  217.     IFS=$' \t\n'
  218.  
  219.     # is the line has no words, add a tab.
  220.     if [[ "$READFIRST" == ^[[:blank:]]*$ ]]; then
  221.         READLINE_LINE="$READFIRST"$'\t'"$READLAST"
  222.         ((READLINE_POINT++))
  223.         return
  224.     fi
  225.  
  226.     # count non-escaped quotes
  227.     quotes=${READFIRST//\\\'}
  228.     quotes=${quotes//[^\']}
  229.     quotes=${#quotes}
  230.  
  231.     # the last word is empty, start completing from scratch
  232.     if [[ "${READFIRST: -1}" == ' ' && "${READFIRST: -2:1}" != '\' ]] && (( quotes % 2 == 0)); then
  233.         before=$READFIRST
  234.         last=
  235.     # complete a command (TODO:add sudo and such)
  236.     elif [[ $READFIRST =~ ^\ *[^\ ]+$ ]]; then
  237.         kb_menu command "$READFIRST"
  238.         prompt+=" "
  239.         ((READLINE_POINT+=${#prompt} - ${#READFIRST}))
  240.         READLINE_LINE=$prompt$READLAST
  241.         IFS=$' \t\n'
  242.         echo -ne "\e[${psheight}A\r\e[2K"
  243.         return
  244.  
  245.     # complete part of a word
  246.     else
  247.         last="${READFIRST##* }"
  248.         recursive="${READFIRST% *}"
  249. #       last="${a[$((words -1))]}"
  250.     fi
  251.  
  252.     nocaseglob=$(shopt nocaseglob | /usr/bin/grep -F on)
  253.     [[ $nocaseglob ]] || shopt -s nocaseglob
  254.     nullglob=$(shopt nullglob | /usr/bin/grep -F on)
  255.     [[ $nullglob ]] || shopt -s nullglob
  256.     extglob=$(shopt extglob | /usr/bin/grep -F on)
  257.     [[ $extglob ]] || shopt -s extglob
  258.     cmdname=$(echo $READFIRST)
  259.     cmdname=${cmdname%% *}
  260.     kb_highlight "$cmdname"
  261.  
  262.     # add words that end in \ to the $last word
  263.     while [[ -z "$before" ]] ; do
  264.         if [[ "${recursive: -1}" == '\' ]]; then
  265.             last="${recursive##* } $last"
  266.             recursive=${recursive% *}
  267.         else
  268.             before=$recursive\
  269.         fi
  270.     done
  271.  
  272.     if (( quotes % 2 == 1 )) && ! [[ "$last" =~ \' ]]; then
  273.         # $last will be part of a quote instead.
  274.         # assume last quote is not escape.
  275.         before="${READFIRST%\'*}"
  276.         last="${READFIRST##*\'}"
  277.     fi
  278.  
  279.     kb_replace_var
  280.  
  281.     last=${last//\'}
  282.  
  283.     kb_menu 'file' "$last"
  284.     kb_write_prompt "$prompt"
  285.  
  286.     IFS=$' \t\n'
  287.     echo -ne "\r\e[2K\e[${psheight}A\r\e[2K"
  288.     [[ $nocaseglob ]] || shopt -u nocaseglob
  289.     [[ $nullglob ]] || shopt -u nullglob
  290.     [[ $extglob ]] || shopt -u extglob
  291.     tput smam >&2 #restore line wrap
  292. }
  293. bind -x '"\t":kb_main'
Advertisement
Add Comment
Please, Sign In to add comment