-
Notifications
You must be signed in to change notification settings - Fork 21.4k
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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" | ||
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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think we need line numbers There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's removed in #50814 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
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]) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shouldn't this be production? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, same for dev, it should be development I think There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
@@ -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? | ||
|
There was a problem hiding this comment.
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.There was a problem hiding this comment.
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 withIRB_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.There was a problem hiding this comment.
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 aunless ENV["IRB_USE_AUTOCOMPLETE"]
. Reading the env is fine, it's changing it that I think would be best avoided.There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done 👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Follow up PR: #50814