Merge pull request #50825 from Shopify/update-rails-console-prompt

Update rails console prompt
This commit is contained in:
Jean Boussier 2024-01-21 00:36:25 +01:00 committed by GitHub
commit 3064d4f53a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 22 additions and 15 deletions

View File

@ -1,13 +1,16 @@
* Rails console now indicates the current Rails environment:
* Rails console now indicates application name and the current Rails environment:
```txt
dev:001> # for RAILS_ENV=development
test:001> # for RAILS_ENV=test
prod:001> # for RAILS_ENV=production
my_env:001> # for RAILS_ENV=my_env
my-app(dev)> # for RAILS_ENV=development
my-app(test)> # for RAILS_ENV=test
my-app(prod)> # for RAILS_ENV=production
my-app(my_env)> # for RAILS_ENV=my_env
```
The environment name will also be colorized when the environment is
The application name is derived from the application's module name from `config/application.rb`.
For example, `MyApp` will displayed as `my-app` in the prompt.
Additionally, the environment name will be colorized when the environment is
`development` (green), `test` (green), or `production` (red), if your
terminal supports it.

View File

@ -13,7 +13,9 @@ module Rails
end
class IRBConsole
def initialize
def initialize(app)
@app = app
require "irb"
require "irb/completion"
@ -33,11 +35,13 @@ module Rails
end
env = colorized_env
app_name = @app.class.module_parent_name.underscore.dasherize
prompt_prefix = "#{app_name}(#{env})"
IRB.conf[:PROMPT][:RAILS_PROMPT] = {
PROMPT_I: "#{env}:%03n> ",
PROMPT_S: "#{env}:%03n%l ",
PROMPT_C: "#{env}:%03n* ",
PROMPT_I: "#{prompt_prefix}> ",
PROMPT_S: "#{prompt_prefix}%l ",
PROMPT_C: "#{prompt_prefix}* ",
RETURN: "=> %s\n"
}
@ -79,7 +83,7 @@ module Rails
app.load_console
@console = app.config.console || IRBConsole.new
@console = app.config.console || IRBConsole.new(app)
end
def sandbox?

View File

@ -216,21 +216,21 @@ class FullStackConsoleTest < ActiveSupport::TestCase
options = "-e production"
spawn_console(options)
write_prompt "123", "prod:001> 123"
write_prompt "123", "app-template(prod)> 123"
end
def test_development_console_prompt
options = "-e development"
spawn_console(options)
write_prompt "123", "dev:001> 123"
write_prompt "123", "app-template(dev)> 123"
end
def test_test_console_prompt
options = "-e test"
spawn_console(options)
write_prompt "123", "test:001> 123"
write_prompt "123", "app-template(test)> 123"
end
def test_console_respects_user_defined_prompt_mode

View File

@ -59,7 +59,7 @@ class Rails::ConsoleTest < ActiveSupport::TestCase
end
def test_prompt_env_colorization
irb_console = Rails::Console::IRBConsole.new
irb_console = Rails::Console::IRBConsole.new(app)
red = "\e[31m"
green = "\e[32m"
clear = "\e[0m"