How to format irb command prompt

前端 未结 7 1146
隐瞒了意图╮
隐瞒了意图╮ 2020-12-09 17:40

Previously I was using Ruby 1.8 and my irb command prompt used to look like this:

Air ~: irb
>> a = 1
=> 1
>> b = 2
=> 2
>&         


        
相关标签:
7条回答
  • 2020-12-09 18:16

    In your ~/.irbrc, simply add

    IRB.conf[:PROMPT_MODE] = :SIMPLE
    
    0 讨论(0)
  • 2020-12-09 18:16

    See this note about IRB prompt in RVM.

    Note that you can create a .irbrc file in your home folder for various settings for IRB. For example, see "Configuring the Prompt" in this document

    You can also puts IRB.conf[:PROMPT_MODE] or puts IRB.conf to see all the various settings currently in effect. For example, the :PROMPT_MODE is probably set to "RVM" in your case.

    0 讨论(0)
  • 2020-12-09 18:19

    To avoid giving the prompt you wish on the command line all the time, you can configure the prompt via the ~/.irbrc config file:

    $ echo "IRB.conf[:PROMPT_MODE] = :DEFAULT" > ~/.irbrc
    $ irb
    irb(main):001:0> quit
    $ echo "IRB.conf[:PROMPT_MODE] = :SIMPLE" > ~/.irbrc
    $ irb
    >> quit
    $ 
    
    0 讨论(0)
  • 2020-12-09 18:27

    The irb man page has a section on "Customizing prompt". Here's mine for example:

    IRB.conf[:PROMPT][:CUSTOM] = {
      :PROMPT_I => ">> ",
      :PROMPT_S => "%l>> ",
      :PROMPT_C => ".. ",
      :PROMPT_N => ".. ",
      :RETURN => "=> %s\n"
    }
    IRB.conf[:PROMPT_MODE] = :CUSTOM
    IRB.conf[:AUTO_INDENT] = true
    

    To use this, add it to your ~/.irbrc file (creating it if it doesn't exist.)

    0 讨论(0)
  • 2020-12-09 18:32

    When you would usually run the irb command, try running irb --simple-prompt instead. That greatly shortens the prompt and makes it easier to understand.

    0 讨论(0)
  • 2020-12-09 18:38

    Whoever want to add a prompt timestamp, this isn't possible yet (check "special strings" section), so I implemented it in a monkey-patchy way:

    module IrbTimePrompt
      def prompt(prompt, ltype, indent, line_no)
        # I used %T as time format, but you could use whatever you want to.
        # Check https://apidock.com/ruby/Time/strftime for more options
        p = prompt.dup.gsub(/%t/, Time.new.strftime('%T'))
        super(p, ltype, indent, line_no)
      end
    end
    
    module IRB
      class Irb
        prepend IrbTimePrompt
      end
    end
    

    Now, add this to your lib/ project folder (in case is a Rails project, ensure lib/ is part of config.autoload_paths in config/application.rb) or in a more aggresive way (not recommended), look for lib/irb.rb file in your local ruby instance and in def prompt method, add a new when condition to the method, like:

        when "t"
          Time.now.strftime('%-d-%-m %T%Z')
    

    then in your .irbrc file (it could be located in your home folder or root project folder) you could modify your prompt. I'm adding my current prompt, but please adjust it to your needs:

    def rails_prompt
      # This is my base prompt, displaying line number and time
      def_prompt = '[%01n][%t]'
      # Maybe you're only running as `irb` an not `rails console`, so check first
      # if rails is available
      if defined? Rails
        app_env = Rails.env[0...4]
        if Rails.env.production?
          puts "\n\e[1m\e[41mWARNING: YOU ARE USING RAILS CONSOLE IN PRODUCTION!\n" \
               "Changing data can cause serious data loss.\n" \
               "Make sure you know what you're doing.\e[0m\e[22m\n\n"
          app_env = "\e[31m#{app_env}\e[0m" # red
        else
          app_env = "\e[32m#{app_env}\e[0m" # green
        end
        def_prompt << "(\e[1m#{app_env}\e[22m)" # bold
      end
    
      IRB.conf[:PROMPT] ||= {}
      IRB.conf[:PROMPT][:WITH_TIME] = {
        PROMPT_I: "#{def_prompt}> ",
        PROMPT_N: "#{def_prompt}| ",
        PROMPT_C: "#{def_prompt}| ",
        PROMPT_S: "#{def_prompt}%l ",
        RETURN: "=> %s\n",
        AUTO_INDENT: true,
      }
      IRB.conf[:PROMPT_MODE] = :WITH_TIME
    end
    
    rails_prompt
    

    Then start irb or rails console and check the awesomeness:

    [1][13:01:15](deve)> 'say hello to your new prompt'
    => "say hello to your new prompt"
    [2][13:01:23](deve)>
    
    0 讨论(0)
提交回复
热议问题