nonsequitur / inf-ruby

218 stars 69 forks source link

Rails console now includes project name and environment #188

Closed sachac closed 2 months ago

sachac commented 2 months ago

The Rails console now includes project name and environment in the prompt (https://railsatscale.com/2024-07-11-mid-year-review-irb-and-rails-console-enhancements-in-the-first-half-of-2024/#rails-console-improvements , ex: my-app(prod)>) I think the "\\(^[^%s]+\\)" regex that I'm adding here is a bit heavy-handed, but it makes M-p and M-n work. I figured I'd mention it anyway just in case it's not on your radar yet and just in case someone can come up with a better regular expression.

(setq inf-ruby-prompt-format
            (concat
             (mapconcat
                #'identity
                '("\\(^%s> *\\)"                    ; Simple
                    "\\(^(rdb:1) *\\)"          ; Debugger
                    "\\(^(rdbg[^)]*) *\\)"  ; Ruby Debug Gem
                    "\\(^(byebug) *\\)"         ; byebug
                    "\\(^\\(irb([^)]+)"         ; IRB default
                    "\\([[0-9]+] \\)?[Pp]ry ?([^)]+)"   ; Pry
                    "\\(^[^%s]+\\)"          ; new rails console with project name and environment
                    "\\(jruby-\\|JRUBY-\\)?[1-9]\\.[0-9]\\(\\.[0-9]+\\)*\\(-?p?[0-9]+\\)?" ; RVM
                    "^rbx-head\\)")                  ; RVM continued
                "\\|")
             ;; Statement and nesting counters, common to the last four.
             " ?[0-9:]* ?%s *\\)")
            inf-ruby-first-prompt-pattern
            (format inf-ruby-prompt-format ">" ">" ">")
            inf-ruby-prompt-pattern
            (format inf-ruby-prompt-format "[?>]" "*>" "[\]>*\"'/`]"))
dgutov commented 2 months ago

Hi Sacha, thanks for the report.

Here's a bit stricter solution, which might be important for consoles with long outputs, etc. Could you try it out?

diff --git a/inf-ruby.el b/inf-ruby.el
index 2e35c0b..1c653b6 100755
--- a/inf-ruby.el
+++ b/inf-ruby.el
@@ -151,6 +151,7 @@ commands as `ruby-send-last-stmt' or `ruby-switch-to-inf'."
       "\\(^(rdb:1) *\\)"                  ; Debugger
       "\\(^(rdbg[^)]*) *\\)"              ; Ruby Debug Gem
       "\\(^(byebug) *\\)"                 ; byebug
+      "\\(^[a-z0-9-_]+([a-z0-9-_]+)%s *\\)" ; Rails 7+: project name and environment
       "\\(^\\(irb([^)]+)"                 ; IRB default
       "\\([[0-9]+] \\)?[Pp]ry ?([^)]+)"   ; Pry
       "\\(jruby-\\|JRUBY-\\)?[1-9]\\.[0-9]\\(\\.[0-9]+\\)*\\(-?p?[0-9]+\\)?" ; RVM
@@ -162,10 +163,12 @@ commands as `ruby-send-last-stmt' or `ruby-switch-to-inf'."
 Two placeholders: first char in the Simple prompt, and the last
 graphical char in all other prompts.")

-(defvar inf-ruby-first-prompt-pattern (format inf-ruby-prompt-format ">" ">")
+(defvar inf-ruby-first-prompt-pattern (format inf-ruby-prompt-format ">" ">" ">")
   "First prompt regex pattern of Ruby interpreter.")

-(defvar inf-ruby-prompt-pattern (format inf-ruby-prompt-format "[?>]" "[\]>*\"'/`]")
+(defvar inf-ruby-prompt-pattern
+  (let ((delims "[\]>*\"'/`]"))
+    (format inf-ruby-prompt-format "[?>]" delims delims))
   "Prompt regex pattern of Ruby interpreter.")

 (defvar inf-ruby-mode-hook nil
dgutov commented 2 months ago

And pushed.