Guest User

Untitled

a guest
Feb 21st, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.77 KB | None | 0 0
  1. #!/bin/sh
  2.  
  3. # ------------------------------------------------------------------------
  4. # colorwrap
  5. #
  6. # This script wraps a command in a different background and foreground
  7. # color. Depending on what the command is, it sets the color, runs the
  8. # command, then sets the color back to what it was before
  9. #
  10. # To display the current colors being used:
  11. # colorwrap.sh getcolor
  12. #
  13. # Here's an example of how to configure (t)csh to use it:
  14. # alias ssh '${HOME}/opt/bin/colorwrap.sh \ssh \!*'
  15. #
  16. # Here's how you would use it configure bash:
  17. # alias ssh='${HOME}/opt/bin/colorwrap.sh \ssh $*'
  18. #
  19. # JD Smith and Eric Kow
  20. #
  21. # This script is released to the public domain.
  22. # Do whatever you want with it.
  23. # ------------------------------------------------------------------------
  24.  
  25. # edit this function to change to the commands/servers you want
  26. # select_fg_color() {
  27. # case "${1}" in
  28. # # if the command contains a server name, we set accordingly
  29. # *cogsci.ed.ac.uk) NEW_FG_COLOR='yellow';;
  30. # *sourceforge.net) NEW_FG_COLOR='orange';;
  31. # *loria*) NEW_FG_COLOR='cyan';;
  32. # # You can get a fancy color by using the Apple's color-
  33. # # selection GUI and then doing a colorwrap.sh getcolor
  34. # *cis.upenn.edu) NEW_FG_COLOR='{17911, 7822, -1}';;
  35. # # black on red (see below)
  36. # su) NEW_FG_COLOR='black';;
  37. # # this is a default value in case we don't specify otherwise
  38. # *) NEW_FG_COLOR='green';;
  39. # esac
  40. # echo ${NEW_FG_COLOR}
  41. # }
  42.  
  43. select_setting() {
  44. echo `osascript -s s -e "tell application \"Terminal\" to get settings set named \"ssh\""`
  45. }
  46.  
  47. # ----------------------------------------------------------------------
  48. # command line arguments
  49. # ----------------------------------------------------------------------
  50.  
  51. if [ $# -lt 1 ]; then
  52. echo "usage: $0 cmd [args...]"
  53. exit 1
  54. fi
  55.  
  56. COMMAND=$*
  57.  
  58. # ----------------------------------------------------------------------
  59. # colors
  60. # ----------------------------------------------------------------------
  61.  
  62. # Set the title bar name to ensure that the correct window
  63. # is modified by the (slow) Applescripts even if you switch
  64. # terminals
  65. WINDOW_NAME="${COMMAND}_$$"
  66. set_title() {
  67. printf "\e]0;${WINDOW_NAME}\a"
  68. }
  69. unset_title() {
  70. printf "\e]0;\a"
  71. }
  72.  
  73. tell_prefix() {
  74. echo 'tell application "Terminal" to tell'\
  75. '(first window whose name contains "'${WINDOW_NAME}'")'
  76. }
  77.  
  78. get_setting() {
  79. TELL=`tell_prefix`
  80. echo `osascript -s s -e "${TELL} to get its current settings"`
  81. }
  82.  
  83. # quote_color() {
  84. # THE_COLOR=$*
  85. # # quote the NEW_COLOR, if neccesary
  86. # case ${THE_COLOR} in
  87. # \"*\") ;;
  88. # '{'*'}') ;;
  89. # *) THE_COLOR='"'${THE_COLOR}'"'
  90. # ;;
  91. # esac
  92. # echo ${THE_COLOR}
  93. # }
  94.  
  95. set_setting() {
  96. setting=${1}
  97.  
  98. TELL=`tell_prefix`
  99. osascript -s s -e "${TELL} to set its current settings to ${setting}"
  100. }
  101.  
  102. # get the original colors
  103. set_title
  104. OLD_SETTING=`get_setting`
  105.  
  106. # select the new colors
  107. NEW_SETTING=`select_setting "${COMMAND}"`
  108.  
  109.  
  110. # ----------------------------------------------------------------------
  111. # running the command
  112. # ----------------------------------------------------------------------
  113.  
  114. decolor_err() {
  115. # reset the color to its old value
  116. set_title
  117. set_setting "${OLD_SETTING}"
  118. unset_title
  119. exit 1;
  120. }
  121.  
  122. # we trap these signals in case the following
  123. # quit/kill/abort/errors signals are sent to the command
  124. # prematurely
  125. trap decolor_err 1 2 3 6
  126.  
  127. # set the color to the requested NEW_COLOR
  128. set_setting "${NEW_SETTING}"
  129.  
  130. # run the script and save its exit status
  131. ${COMMAND}
  132. CMDEXIT=$?
  133.  
  134. # reset the color to its old value
  135. set_title
  136. set_setting "${OLD_SETTING}"
  137. unset_title
  138.  
  139. # exit with the exit status of the command we ran
  140. exit ${CMDEXIT}
Add Comment
Please, Sign In to add comment