views:

93

answers:

2

Using putty in windows, you can save sessions that connect to a certain host and use a certain text color...this was super useful for me since I work with a bunch of remote hosts and I'm wondering if there is (there must be) a way to get Terminal (in Snow Leopard) to emulate this behavior.

I'm wondering how I would 1. Save a connection (e.g. [email protected]) and have that connection always open with a certain text color (e.g. #00ff00) 2. Ideally, have any terminal window detect what host it was in and change its color accordingly. So if I was in my regular Terminal environment and issued a successful ssh [email protected], it would automatically change the text color of that terminal window (or tab) to #00ff00

Let me know, thanks!

+2  A: 

In terminal you can define profiles with different window background color, opacity, etc. Also in profiles you can specify a startup command. You could set up a different profile for each host you use with a startup command of "ssh me@thathost", but this would only work for new windows. Profiles are easy to get to via Shell -> New Window.

dacc
oh, yeah that solves #1 for me, thanks a lot. I'm still interested to see if anybody can do #2, but if there's nothing after a day or so I'll gladly mark this as the correct answer
Neil Sarkar
This is great! And to think I almost suggested a mess of AppleScript and/or shell scripting and ANSI escape codes.
benzado
Yeah I was thinking about the escape char thing too. My PS1 magically changes the title bar to reflect the host and dir I'm in, but I can't remember how it works. You can try it by setting your PS1 like so. I use zsh but might work in bash: export PS1=$'%{\e[0;34m%}\{%{\e[0m%}%n%{\e[0;36m%}@%{\e[0m%}%m %~%{\e[0;34m%}\}%{\e[0m%}%{\e[0;36m%}$%{\e[0m%} '
dacc
Since the Terminal profiles are menu items, a convenient way to spawn them from the keyboard is to use ⌘? to search for a menu item, then start typing.
Nicholas Riley
+2  A: 

OK, if you insist on invoking ssh from the command line, here's something that should do the trick: write a shell script and save it somewhere as colorssh.sh. When it runs, it looks at its arguments for a matching host and sets the active terminal window's colors appropriately. Then it invokes the real ssh, passing along those arguments. When ssh returns execution to the script, it sets the colors back to normal.

Since you probably want to keep typing ssh instead of colorssh.sh, you can set an alias in your .profile.

As for the script itself? Here is teh codez:

#!/bin/bash

function setTerminalColors {
    osascript \
        -e "tell application \"Terminal\"" \
        -e "tell selected tab of front window" \
        -e "set normal text color to $1" \
        -e "set background color to $2" \
        -e "end tell" \
        -e "end tell"
}

for ARG in $*
do
    case "$ARG" in
        host.example.com)
        [email protected])
            setTerminalColors "{0,65535,65535}" "{65535,0,0}"
            ;;
        [email protected])
            setTerminalColors "{65535,65535,0}" "{0,65535,0}"
            ;;
    esac
done

ssh $*

# back to normal
setTerminalColors "{0,0,0}" "{65535,65535,65535}"

You'll have to edit the script to add new host/color combinations.

Note that colors must be specified as an RGB triplet of integers in the range 0-65535. I know, weird, right?

Technically, the AppleScript portion changing deprecated properties. You're supposed to change a window's colors via its "settings set" property, but I suspect that would change all windows using that settings set, not just the current one.

Also, this script assumes that black on white is your "normal" setting. If that's not the case you could change the script to save the current values before running or use the colors from the default settings set.

benzado
thanks, this is really cool and does exactly what I was asking for. I thought about it though and it's really not a big deal to just open a new tab with a saved profile so I'll probably do that more often. I'm guessing that's more commonly asked and more widely helpful so I'm going to mark it as the correct answer, but thanks again for the script as I'll be employing both of these methods.
Neil Sarkar
Neil, you're killing me! You know, if you want to be nitpicky, your question isn't really about programming in the first place. :-)
benzado