Skip to content

Commit 3064d4f

Browse files
authored
Merge pull request rails#50825 from Shopify/update-rails-console-prompt
Update rails console prompt
2 parents f84b428 + 09b2aec commit 3064d4f

File tree

4 files changed

+22
-15
lines changed

4 files changed

+22
-15
lines changed

railties/CHANGELOG.md

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
1-
* Rails console now indicates the current Rails environment:
1+
* Rails console now indicates application name and the current Rails environment:
22

33
```txt
4-
dev:001> # for RAILS_ENV=development
5-
test:001> # for RAILS_ENV=test
6-
prod:001> # for RAILS_ENV=production
7-
my_env:001> # for RAILS_ENV=my_env
4+
my-app(dev)> # for RAILS_ENV=development
5+
my-app(test)> # for RAILS_ENV=test
6+
my-app(prod)> # for RAILS_ENV=production
7+
my-app(my_env)> # for RAILS_ENV=my_env
88
```
99
10-
The environment name will also be colorized when the environment is
10+
The application name is derived from the application's module name from `config/application.rb`.
11+
For example, `MyApp` will displayed as `my-app` in the prompt.
12+
13+
Additionally, the environment name will be colorized when the environment is
1114
`development` (green), `test` (green), or `production` (red), if your
1215
terminal supports it.
1316

railties/lib/rails/commands/console/console_command.rb

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ def filter_backtrace(bt)
1313
end
1414

1515
class IRBConsole
16-
def initialize
16+
def initialize(app)
17+
@app = app
18+
1719
require "irb"
1820
require "irb/completion"
1921

@@ -33,11 +35,13 @@ def start
3335
end
3436

3537
env = colorized_env
38+
app_name = @app.class.module_parent_name.underscore.dasherize
39+
prompt_prefix = "#{app_name}(#{env})"
3640

3741
IRB.conf[:PROMPT][:RAILS_PROMPT] = {
38-
PROMPT_I: "#{env}:%03n> ",
39-
PROMPT_S: "#{env}:%03n%l ",
40-
PROMPT_C: "#{env}:%03n* ",
42+
PROMPT_I: "#{prompt_prefix}> ",
43+
PROMPT_S: "#{prompt_prefix}%l ",
44+
PROMPT_C: "#{prompt_prefix}* ",
4145
RETURN: "=> %s\n"
4246
}
4347

@@ -79,7 +83,7 @@ def initialize(app, options = {})
7983

8084
app.load_console
8185

82-
@console = app.config.console || IRBConsole.new
86+
@console = app.config.console || IRBConsole.new(app)
8387
end
8488

8589
def sandbox?

railties/test/application/console_test.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -216,21 +216,21 @@ def test_production_console_prompt
216216
options = "-e production"
217217
spawn_console(options)
218218

219-
write_prompt "123", "prod:001> 123"
219+
write_prompt "123", "app-template(prod)> 123"
220220
end
221221

222222
def test_development_console_prompt
223223
options = "-e development"
224224
spawn_console(options)
225225

226-
write_prompt "123", "dev:001> 123"
226+
write_prompt "123", "app-template(dev)> 123"
227227
end
228228

229229
def test_test_console_prompt
230230
options = "-e test"
231231
spawn_console(options)
232232

233-
write_prompt "123", "test:001> 123"
233+
write_prompt "123", "app-template(test)> 123"
234234
end
235235

236236
def test_console_respects_user_defined_prompt_mode

railties/test/commands/console_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ def test_console_defaults_to_IRB
5959
end
6060

6161
def test_prompt_env_colorization
62-
irb_console = Rails::Console::IRBConsole.new
62+
irb_console = Rails::Console::IRBConsole.new(app)
6363
red = "\e[31m"
6464
green = "\e[32m"
6565
clear = "\e[0m"

0 commit comments

Comments
 (0)