Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add customized prompt for Rails console #50796

Merged
merged 1 commit into from
Jan 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 15 additions & 0 deletions railties/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
* Rails console now indicates 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
```

The environment name will also be colorized when the environment is
`development` (green), `test` (green), or `production` (red), if your
terminal supports it.

*Stan Lo*

* `bin/rails` now prints its help message when given an unrecognized bare
option.

Expand Down
60 changes: 48 additions & 12 deletions railties/lib/rails/commands/console/console_command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,53 @@ def filter_backtrace(bt)
end
end

class IRBConsole
def initialize
require "irb"
require "irb/completion"
IRB::WorkSpace.prepend(BacktraceCleaner)

if !Rails.env.local?
# Use env var here so users can override them with env var too
ENV["IRB_USE_AUTOCOMPLETE"] ||= "false"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any way we could set this on the IRB config rather than in ENV? Not a big deal, but it's best not to mutate ENV if we can.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we set it with IRB.conf, users can't override it with IRB_USE_AUTOCOMPLETE=true due to IRB's initialization order. This probably won't affect many users, but because I mentioned it was overridable in the original changelog I think it'd be a breaking-change.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't you set it in IRB.conf under a unless ENV["IRB_USE_AUTOCOMPLETE"]. Reading the env is fine, it's changing it that I think would be best avoided.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a good point. But for that change I want to convert related unit tests into integration (application) tests too, which is a bit out of scope for this PR IMO. Do you mind if I open another PR for it?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, it's a pre-existing issue, so I don't mind. I'll merge once you squash.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done 👍

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Follow up PR: #50814

end
end

def name
"IRB"
end

def start
IRB.setup(nil)

env = colorized_env

IRB.conf[:PROMPT][:RAILS_PROMPT] = {
PROMPT_I: "#{env}:%03n> ",
PROMPT_S: "#{env}:%03n%l ",
PROMPT_C: "#{env}:%03n* ",
Comment on lines +37 to +39
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we need line numbers

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's removed in #50814

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see Screenshot 2024-01-26 at 15 24 54

RETURN: "=> %s\n"
}

# Respect user's choice of prompt mode.
IRB.conf[:PROMPT_MODE] = :RAILS_PROMPT if IRB.conf[:PROMPT_MODE] == :DEFAULT
st0012 marked this conversation as resolved.
Show resolved Hide resolved
IRB::Irb.new.run(IRB.conf)
end

def colorized_env
case Rails.env
when "development"
IRB::Color.colorize("dev", [:GREEN])
when "test"
IRB::Color.colorize("test", [:GREEN])
when "production"
IRB::Color.colorize("prod", [:RED])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this be production?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

seems prod is intended
image

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The original discussion in #50770 decided to use abbreviations for development and production environments.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, same for dev, it should be development I think

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I disagree, but you can raise that in the original issue.

else
Rails.env
end
end
end

def self.start(*args)
new(*args).start
end
Expand All @@ -31,18 +78,7 @@ def initialize(app, options = {})

app.load_console

@console = app.config.console || begin
require "irb"
require "irb/completion"

IRB::WorkSpace.prepend(BacktraceCleaner)

if !Rails.env.local?
ENV["IRB_USE_AUTOCOMPLETE"] ||= "false"
end

IRB
end
@console = app.config.console || IRBConsole.new
end

def sandbox?
Expand Down
42 changes: 40 additions & 2 deletions railties/test/application/console_test.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# frozen_string_literal: true

require "tempfile"

require "isolation/abstract_unit"
require "console_helpers"

Expand Down Expand Up @@ -123,9 +125,9 @@ def write_prompt(command, expected_output = nil)
assert_output "> ", @primary
end

def spawn_console(options, wait_for_prompt: true)
def spawn_console(options, wait_for_prompt: true, env: {})
pid = Process.spawn(
{ "TERM" => "dumb" },
{ "TERM" => "dumb" }.merge(env),
"#{app_path}/bin/rails console #{options}",
in: @replica, out: @replica, err: @replica
)
Expand Down Expand Up @@ -209,4 +211,40 @@ def test_environment_option_and_irb_option
write_prompt "puts Rails.env", "puts Rails.env\r\ntest"
@primary.puts "quit"
end

def test_production_console_prompt
options = "-e production -- --nocolorize"
spawn_console(options)

write_prompt "123", "prod:001> 123"
end

def test_development_console_prompt
options = "-e development -- --nocolorize"
spawn_console(options)

write_prompt "123", "dev:001> 123"
end

def test_test_console_prompt
options = "-e test -- --nocolorize"
spawn_console(options)

write_prompt "123", "test:001> 123"
end

def test_console_respects_user_defined_prompt_mode
irbrc = Tempfile.new("irbrc")
irbrc.write <<-RUBY
IRB.conf[:PROMPT_MODE] = :SIMPLE
RUBY
irbrc.close

options = "-e test -- --nocolorize"
spawn_console(options, env: { "IRBRC" => irbrc.path })

write_prompt "123", ">> 123"
ensure
File.unlink(irbrc)
end
end
19 changes: 19 additions & 0 deletions railties/test/commands/console_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,25 @@ def test_console_doesnt_disable_IRB_auto_completion_in_local
ENV["IRB_USE_AUTOCOMPLETE"] = original_use_autocomplete
end

def test_prompt_env_colorization
irb_console = Rails::Console::IRBConsole.new
red = "\e[31m"
green = "\e[32m"
clear = "\e[0m"

Rails.env = "development"
assert_equal("#{green}dev#{clear}", irb_console.colorized_env)

Rails.env = "test"
assert_equal("#{green}test#{clear}", irb_console.colorized_env)

Rails.env = "production"
assert_equal("#{red}prod#{clear}", irb_console.colorized_env)

Rails.env = "custom_env"
assert_equal("custom_env", irb_console.colorized_env)
end

def test_default_environment_with_no_rails_env
with_rails_env nil do
start
Expand Down