Skip to content

Instantly share code, notes, and snippets.

@cartolari
Last active May 23, 2016 18:29
Show Gist options
  • Select an option

  • Save cartolari/da28e8ac8d2fdeb376bc33f3b00a6bab to your computer and use it in GitHub Desktop.

Select an option

Save cartolari/da28e8ac8d2fdeb376bc33f3b00a6bab to your computer and use it in GitHub Desktop.
Colored prompts for Rails App
#!/bin/bash
## $HOME/.bashrc
DEFAULT='\e[39m'
LIGHT_RED='\e[91m'
LIGHT_GREEN='\e[92m'
LIGHT_YELLOW='\e[93m'
_color() {
if [ -z "$RAILS_ENV" ] || [ "$RAILS_ENV" = 'development' ] || [ "$RAILS_ENV" = 'test' ]
then
echo "$LIGHT_GREEN"
elif [ "$RAILS_ENV" = 'staging' ]; then
echo "$LIGHT_YELLOW"
else
echo "$LIGHT_RED"
fi
}
_rails_env() {
echo "${RAILS_ENV:-development}"
}
_prompt() {
echo "\[$(_color)\]$(_rails_env)\[${DEFAULT}\] \${debian_chroot:+(\$debian_chroot)}\u@\h:\w\$ "
}
PROMPT_COMMAND='PS1=$(_prompt)'
# -*- mode: ruby -*-
DEFAULT = "\e[0m".freeze
LIGHT_RED = "\e[91m".freeze
LIGHT_GREEN = "\e[92m".freeze
LIGHT_YELLOW = "\e[93m".freeze
def color_and_environment
case ENV['RAILS_ENV']
when 'production' then [LIGHT_RED, 'production']
when 'staging' then [LIGHT_YELLOW, 'staging']
else [LIGHT_GREEN, 'development']
end
end
def color_text(color, text)
return text unless Pry.color
"#{color}#{text}#{DEFAULT}"
end
if defined?(PryByebug)
Pry.commands.alias_command 'c', 'continue'
Pry.commands.alias_command 's', 'step'
Pry.commands.alias_command 'n', 'next'
Pry.commands.alias_command 'f', 'finish'
end
prompt = proc do |_obj, _nest_level, pry|
application_name = Rails.application.class.to_s.deconstantize
environment_with_color = color_text(*color_and_environment)
"[#{pry.input_array.size}] #{application_name}@#{environment_with_color}> "
end
Pry.prompt = [prompt, prompt]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment