简体   繁体   English

当我在Heroku上登录我的生产遥控器时,如何更改Terminal.App的颜色?

[英]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. 我记得有一篇或者几篇关于作者如何根据SSH地址将终端颜色从绿色(用于开发)更改为红色(用于生产)的文章。

But I am not sure how to replicate that with Heroku console. 但我不知道如何使用Heroku控制台复制它。

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 . 理想情况下,我想说它是蓝色的,当我做heroku console --remote staging然后红色,当我做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: 在作为生产环境的一部分加载的任何文件中(例如, config/environments/production.rb ),您可以放置​​:

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. 疯狂的转义字符是ANSI颜色代码。 The "\\033" is an escape character, and the rest is a code for a particular color or effect. "\\033"是一个转义字符,其余的是特定颜色或效果的代码。 You can find a list of other colors and effects here . 您可以在此处找到其他颜色和效果的列表。 That IRB.conf hash is a global conf for IRB. IRB.conf哈希是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. 如果您没有使用Rails(因此不一定有环境文件),您可以使用ENV['RACK_ENV']来检查当前环境,这应该在Heroku上设置为'production'

I do this by using the Marco Polo gem https://github.com/arches/marco-polo 我通过使用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. 然后,您可以通过设置heroku配置变量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 就我而言,我使用此控制序列将生产提示设置为洋红色背景上的白色(很难错过)MARCO_POLO_APP_NAME的值

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

Unfortunately, Stack Overflow won't let me post the escape character itself. 不幸的是,Stack Overflow不会让我发布转义字符本身。 You'll have to use Notepad++ and run a Regexp search and replace to replace [ESC] above with \\x1B. 你必须使用Notepad ++并运行Regexp搜索和替换,将上面的[ESC]替换为\\ x1B。 Then you can copy and paste into the value of MARCO_POLO_APP_NAME in the Heroku console. 然后,您可以复制并粘贴到Heroku控制台中的MARCO_POLO_APP_NAME值。 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) 未经测试,但你的.bashrc或其他任何你可能想要的东西(它不是完全安全,但你应该得到的想法)

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. 我将它用于ssh但它在概念上是相同的:用一个使用osascript更改终端设置的函数覆盖你的命令,然后在退出时更改它们。 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. 您可以在键入时使用bash或batch自动化它。别名在我看来是cli / terminal devving最好的东西之一。 Theres an article somewhere where a simple echo command can change your color until you set it back with another echo. 这是一篇文章,其中一个简单的echo命令可以改变你的颜色,直到你用另一个回声设置它。

So you could do this with your bashrc if your a nix user. 如果您是一名nix用户,那么您可以使用bashrc执行此操作。

#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 然后输入heroku-staging

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Terminal.app中进行bash反向搜索工作而不显示乱码输出? - How do I make bash reverse-search work in Terminal.app without it displaying garbled output? 如何从Mac OS上的终端获取到计算机的远程登录IP? - How do I get remote login IP to my machine from terminal on Mac OS? 在Terminal.app中按Control + - Control + , in Terminal.app PS1中的什么导致我的Terminal.app命令卡在屏幕上? - What in PS1 is causing my Terminal.app commands to get stuck on the screen? 在OS X上的Terminal.app中打开BASH shell时,如何设置PATH env变量? - How is the PATH env variable set when opening a BASH shell in Terminal.app on OS X? 打开我的bash终端时,会弹出while循环git消息,我该如何删除它 - When opening up my bash terminal a while loop git message pops up, how do I remove it 将脚本粘贴到Terminal.app中时错误地使用了STDIN - Erroneously using STDIN when pasting a script into the Terminal.app 我可以将bash提示符的输入颜色更改为与终端默认值不同的输入颜色 - Can I change the input color of my bash prompt to something different than the terminal default 如何让我的Mac终端对bash脚本进行颜色编码? - How can I get my mac terminal to color code my bash script? 当我打开一个新终端时,我的.bashrc中设置的提示颜色不会出现,但只有当我再次发送它时才会出现。 我该如何解决? - Colors for my prompt set in my .bashrc do not appear when I open a new terminal, but only when I source it again. How do I fix this?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM