From 93398ab9507dfaab31d76153b4036edbb0356358 Mon Sep 17 00:00:00 2001 From: jbranchaud Date: Thu, 13 Mar 2025 10:31:32 -0500 Subject: [PATCH] Add Add Color To The IRB Console Prompt as a Rails TIL --- README.md | 3 +- rails/add-color-to-the-irb-console-prompt.md | 62 ++++++++++++++++++++ 2 files changed, 64 insertions(+), 1 deletion(-) create mode 100644 rails/add-color-to-the-irb-console-prompt.md diff --git a/README.md b/README.md index c87d782..6d11812 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ pairing with smart people at Hashrocket. For a steady stream of TILs, [sign up for my newsletter](https://crafty-builder-6996.ck.page/e169c61186). -_1616 TILs and counting..._ +_1617 TILs and counting..._ See some of the other learning resources I work on: - [Ruby Operator Lookup](https://www.visualmode.dev/ruby-operators) @@ -977,6 +977,7 @@ If you've learned something here, support my efforts writing daily TILs by - [Add A Generated Column To A PostgreSQL Table](rails/add-a-generated-column-to-a-postgresql-table.md) - [Add A Reference Column With An Index](rails/add-a-reference-column-with-an-index.md) - [Add ActiveRecord Error Not Tied To Any Attribute](rails/add-activerecord-error-not-tied-to-any-attribute.md) +- [Add Color To The IRB Console Prompt](rails/add-color-to-the-irb-console-prompt.md) - [Add React With Webpacker To A New Rails App](rails/add-react-with-webpacker-to-a-new-rails-app.md) - [Add timestamptz Columns With The Migration DSL](rails/add-timestamptz-columns-with-the-migration-dsl.md) - [Adjust The Production Log Level](rails/adjust-the-production-log-level.md) diff --git a/rails/add-color-to-the-irb-console-prompt.md b/rails/add-color-to-the-irb-console-prompt.md new file mode 100644 index 0000000..ff7fb13 --- /dev/null +++ b/rails/add-color-to-the-irb-console-prompt.md @@ -0,0 +1,62 @@ +# Add Color To The IRB Console Prompt + +IRB has a little-known [`Color` +module](https://docs.ruby-lang.org/en/3.2/IRB/Color.html) with some helpers for +adding a splash of color to the IRB prompt. I like to clearly differentiate the +environment I'm in when connecting to the `rails console`, so I have a +customize the prompt to display and colorize the current environment. + +I can wrap any string in ANSI escape codes that instruct the terminal to style +the text with color. For instance, here is how I can style the word `DEV` to be +inverted against a blue background. + +```ruby +IRB::Color.colorize("DEV", [:BLUE, :BOLD, :REVERSE]) +``` + +which will clearly stand out from `PROD` against a red background: + +```ruby +IRB::Color.colorize("PROD", [:RED, :BOLD, :REVERSE]) +``` + +Here is a full example of customizing the prompt from the +`config/application.rb` file. + +```ruby +module MyApp + class Application < Rails::Application + # ... + + console do + # Get the application module name and convert to kebab-case + app_name = Rails.application.class.module_parent.name + kebab_name = app_name.underscore.dasherize + + # Environment color coding + env_colors = { + "development" => IRB::Color.colorize("DEV", [:BLUE, :BOLD, :REVERSE]), + "production" => IRB::Color.colorize("PROD", [:RED, :BOLD, :REVERSE]), + "test" => IRB::Color.colorize("TEST", [:YELLOW, :BOLD, :REVERSE]), + } + + colored_env = "(#{env_colors[Rails.env]})" + + # Docs: https://docs.ruby-lang.org/en/3.2/IRB.html#module-IRB-label-Customizing+the+IRB+Prompt + IRB.conf[:PROMPT][:RAILS_APP] = { + PROMPT_I: "#{kebab_name}#{colored_env}> ", + PROMPT_N: "#{kebab_name}#{colored_env}* ", + PROMPT_S: "#{kebab_name}#{colored_env}% ", + PROMPT_C: "#{kebab_name}#{colored_env}? ", + RETURN: "=> %s\n" + } + + # Set it as the current prompt + IRB.conf[:PROMPT_MODE] = :RAILS_APP + end + end +end +``` + +The Ruby docs have more about [IRB Prompt +Customization](https://docs.ruby-lang.org/en/3.2/IRB.html#module-IRB-label-Customizing+the+IRB+Prompt).