randalltux

live command coloring

Oct 8th, 2014
234
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.58 KB | None | 0 0
  1. #!/usr/local/bin/zsh
  2. ZLE_RESERVED_WORD_STYLE='fg=yellow,bold'
  3. ZLE_ALIAS_STYLE='fg=magenta,bold'
  4. ZLE_BUILTIN_STYLE='fg=cyan,bold'
  5. ZLE_FUNCTION_STYLE='fg=blue,bold'
  6. ZLE_COMMAND_STYLE='fg=green,bold'
  7. ZLE_COMMAND_UNKNOWN_TOKEN_STYLE='fg=red,bold'
  8.  
  9. ZLE_HYPHEN_CLI_OPTION='fg=yellow,bold'
  10. ZLE_DOUBLE_HYPHEN_CLI_OPTION='fg=green,bold'
  11. ZLE_SINGLE_QUOTED='fg=magenta,bold'
  12. ZLE_DOUBLE_QUOTED='fg=red,bold'
  13. ZLE_BACK_QUOTED='fg=cyan,bold'
  14. ZLE_GLOBING='fg=blue,bold'
  15.  
  16. ZLE_DEFAULT='fg=white,bold'
  17.  
  18. ZLE_TOKENS_FOLLOWED_BY_COMMANDS=('|' '||' ';' '&' '&&' 'sudo' 'start' 'time' 'strace' '§')
  19.  
  20. # Recolorize the current ZLE buffer.
  21. colorize-zle-buffer() {
  22. region_highlight=()
  23. colorize=true
  24. start_pos=0
  25. for arg in ${(z)BUFFER}; do
  26. ((start_pos+=${#BUFFER[$start_pos+1,-1]}-${#${BUFFER[$start_pos+1,-1]## #}}))
  27. ((end_pos=$start_pos+${#arg}))
  28. if $colorize; then
  29. colorize=false
  30. res=$(LC_ALL=C builtin type $arg 2>/dev/null)
  31. case $res in
  32. *'reserved word'*) style=$ZLE_RESERVED_WORD_STYLE;;
  33. *'an alias'*) style=$ZLE_ALIAS_STYLE;;
  34. *'shell builtin'*) style=$ZLE_BUILTIN_STYLE;;
  35. *'shell function'*) style=$ZLE_FUNCTION_STYLE;;
  36. *"$cmd is"*) style=$ZLE_COMMAND_STYLE;;
  37. *) style=$ZLE_COMMAND_UNKNOWN_TOKEN_STYLE;;
  38. esac
  39. else
  40. case $arg in
  41. '--'*) style=$ZLE_DOUBLE_HYPHEN_CLI_OPTION;;
  42. '-'*) style=$ZLE_HYPHEN_CLI_OPTION;;
  43. "'"*"'") style=$ZLE_SINGLE_QUOTED;;
  44. '"'*'"') style=$ZLE_DOUBLE_QUOTED;;
  45. '`'*'`') style=$ZLE_BACK_QUOTED;;
  46. *"*"*) style=$ZLE_GLOBING;;
  47. *) style=$ZLE_DEFAULT;;
  48. esac
  49. fi
  50. region_highlight+=("$start_pos $end_pos $style")
  51. [[ ${${ZLE_TOKENS_FOLLOWED_BY_COMMANDS[(r)${arg//|/\|}]}:+yes} = 'yes' ]] && colorize=true
  52. start_pos=$end_pos
  53. done
  54. }
  55.  
  56. # Bind the function to ZLE events.
  57. ZLE_COLORED_FUNCTIONS=(
  58. self-insert
  59. delete-char
  60. backward-delete-char
  61. kill-word
  62. backward-kill-word
  63. up-line-or-history
  64. down-line-or-history
  65. beginning-of-history
  66. end-of-history
  67. undo
  68. redo
  69. yank
  70. )
  71.  
  72. for f in $ZLE_COLORED_FUNCTIONS; do
  73. eval "$f() { zle .$f && colorize-zle-buffer } ; zle -N $f"
  74. done
  75.  
  76. # Expand or complete hack :
  77.  
  78. # create an expansion widget which mimics the original "expand-or-complete" (you can see the default setup using "zle -l -L")
  79. zle -C orig-expand-or-complete .expand-or-complete _main_complete
  80.  
  81. # use the orig-expand-or-complete inside the colorize function (for some reason, using the ".expand-or-complete" widget doesn't work the same)
  82. expand-or-complete() { builtin zle orig-expand-or-complete && colorize-zle-buffer }
  83. zle -N expand-or-complete
Add Comment
Please, Sign In to add comment