Created
December 8, 2008 11:02
-
-
Save jdelStrother/33427 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
# ------------------------------------------------------------------------ | |
# colorwrap | |
# | |
# This script wraps a command in a different background and foreground | |
# color. Depending on what the command is, it sets the color, runs the | |
# command, then sets the color back to what it was before | |
# | |
# To display the current colors being used: | |
# colorwrap.sh getcolor | |
# | |
# Here's an example of how to configure (t)csh to use it: | |
# alias ssh '${HOME}/opt/bin/colorwrap.sh \ssh \!*' | |
# | |
# Here's how you would use it configure bash: | |
# alias ssh='${HOME}/opt/bin/colorwrap.sh \ssh $*' | |
# | |
# JD Smith and Eric Kow | |
# | |
# This script is released to the public domain. | |
# Do whatever you want with it. | |
# ------------------------------------------------------------------------ | |
# edit this function to change to the commands/servers you want | |
# select_fg_color() { | |
# case "${1}" in | |
# # if the command contains a server name, we set accordingly | |
# *cogsci.ed.ac.uk) NEW_FG_COLOR='yellow';; | |
# *sourceforge.net) NEW_FG_COLOR='orange';; | |
# *loria*) NEW_FG_COLOR='cyan';; | |
# # You can get a fancy color by using the Apple's color- | |
# # selection GUI and then doing a colorwrap.sh getcolor | |
# *cis.upenn.edu) NEW_FG_COLOR='{17911, 7822, -1}';; | |
# # black on red (see below) | |
# su) NEW_FG_COLOR='black';; | |
# # this is a default value in case we don't specify otherwise | |
# *) NEW_FG_COLOR='green';; | |
# esac | |
# echo ${NEW_FG_COLOR} | |
# } | |
select_setting() { | |
echo `osascript -s s -e "tell application \"Terminal\" to get settings set named \"ssh\""` | |
} | |
# ---------------------------------------------------------------------- | |
# command line arguments | |
# ---------------------------------------------------------------------- | |
if [ $# -lt 1 ]; then | |
echo "usage: $0 cmd [args...]" | |
exit 1 | |
fi | |
COMMAND=$* | |
# ---------------------------------------------------------------------- | |
# colors | |
# ---------------------------------------------------------------------- | |
# Set the title bar name to ensure that the correct window | |
# is modified by the (slow) Applescripts even if you switch | |
# terminals | |
WINDOW_NAME="${COMMAND}_$$" | |
set_title() { | |
printf "\e]0;${WINDOW_NAME}\a" | |
} | |
unset_title() { | |
printf "\e]0;\a" | |
} | |
tell_prefix() { | |
echo 'tell application "Terminal" to tell'\ | |
'(first window whose name contains "'${WINDOW_NAME}'")' | |
} | |
get_setting() { | |
TELL=`tell_prefix` | |
echo `osascript -s s -e "${TELL} to get its current settings"` | |
} | |
# quote_color() { | |
# THE_COLOR=$* | |
# # quote the NEW_COLOR, if neccesary | |
# case ${THE_COLOR} in | |
# \"*\") ;; | |
# '{'*'}') ;; | |
# *) THE_COLOR='"'${THE_COLOR}'"' | |
# ;; | |
# esac | |
# echo ${THE_COLOR} | |
# } | |
set_setting() { | |
setting=${1} | |
TELL=`tell_prefix` | |
osascript -s s -e "${TELL} to set its current settings to ${setting}" | |
} | |
# get the original colors | |
set_title | |
OLD_SETTING=`get_setting` | |
# select the new colors | |
NEW_SETTING=`select_setting "${COMMAND}"` | |
# ---------------------------------------------------------------------- | |
# running the command | |
# ---------------------------------------------------------------------- | |
decolor_err() { | |
# reset the color to its old value | |
set_title | |
set_setting "${OLD_SETTING}" | |
unset_title | |
exit 1; | |
} | |
# we trap these signals in case the following | |
# quit/kill/abort/errors signals are sent to the command | |
# prematurely | |
trap decolor_err 1 2 3 6 | |
# set the color to the requested NEW_COLOR | |
set_setting "${NEW_SETTING}" | |
# run the script and save its exit status | |
${COMMAND} | |
CMDEXIT=$? | |
# reset the color to its old value | |
set_title | |
set_setting "${OLD_SETTING}" | |
unset_title | |
# exit with the exit status of the command we ran | |
exit ${CMDEXIT} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment