简体   繁体   中英

How do I change the color of my Terminal.App when I log into my production remote on Heroku?

I remember there was an article, or a few, going around about how the author changes the color of the terminal from green (for development) to red (for production) based on the SSH address.

But I am not sure how to replicate that with Heroku console.

Ideally, I would like for it to be say blue, when I do heroku console --remote staging and then red, when I do heroku console --remote production .

Any suggestions anyone ?

In any file that is loaded as part of the production environment (say, config/environments/production.rb ), you can put:

if defined? IRB
  # whew!
  conf = IRB.conf[:PROMPT][IRB.conf[:PROMPT_MODE]]
  red = "\033[0;31m"
  reset = "\033[0m"
  conf[:PROMPT_S] = "#{red}>> #{reset}" # regular prompt
end

The crazy escape characters are ANSI color codes. The "\\033" is an escape character, and the rest is a code for a particular color or effect. You can find a list of other colors and effects here . That IRB.conf hash is a global conf for IRB. You may want to set a few other keys on it - they're documented here .

If you're not using Rails (and hence don't necessarily have an environment file), you can always check the current environment by using ENV['RACK_ENV'] , which should be set to 'production' on Heroku.

I do this by using the Marco Polo gem https://github.com/arches/marco-polo

You can then change your console prompt by setting the heroku config variable MARCO_POLO_APP_NAME. You can take advantage of escape codes to change the color. In my case, I set the production prompt to be white on a magenta background (hard to miss) using this control sequence for the value of MARCO_POLO_APP_NAME

[ESC][105;97;1mPRODUCTION[ESC][0m

Unfortunately, Stack Overflow won't let me post the escape character itself. You'll have to use Notepad++ and run a Regexp search and replace to replace [ESC] above with \\x1B. Then you can copy and paste into the value of MARCO_POLO_APP_NAME in the Heroku console. I couldn't manage to set it at the command line.

untested, but something along the lines of this in your .bashrc or whatever may be what you want (it's not completely safe but you should get the idea)

function heroku {
  REMOTE_TERMINAL_THEME_NAME="Solarized Light"
  CURTAB=$(osascript -e "tell application \"Terminal\" to get the selected tab of the front window")
  CURTHEME=$(osascript -e "tell application \"Terminal\" to get the name of current settings of the selected tab of the front window")
  HEROKU=$(which heroku)
  osascript -e "tell application \"Terminal\" to set current settings of $CURTAB to settings set \"$REMOTE_TERMINAL_THEME_NAME\""

  $HEROKU "$@"
  osascript -e "tell application \"Terminal\" to set current settings of $CURTAB to settings set  \"$CURTHEME\""
}

I use it for ssh but it's conceptually the same: override your command with a function that uses osascript to change the terminal settings, then change them back on exit. Switching based on arguments should be relatively easy to add.

You could automate it with bash or batch when you type it aliases are in my opinion one of the best things for cli/terminal devving. Theres an article somewhere where a simple echo command can change your color until you set it back with another echo.

So you could do this with your bashrc if your a nix user.

#pretend this is a bunch of prompt setup
#USER ALIASES
alias heroku-staging="export     PS1="\e[0;34m[Heroku Staging]$]" && heroku console --remote staging"

Then just type heroku-staging

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM