如何格式化 irb 命令提示符

发布于 2024-11-08 06:46:14 字数 421 浏览 1 评论 0 原文

以前我使用的是 Ruby 1.8,我的 irb 命令提示符看起来像这样:

Air ~: irb
>> a = 1
=> 1
>> b = 2
=> 2
>> a + b
=> 3

我安装了 rvm(和 Ruby 1.9.2),现在我的 irb 命令提示符看起来像这样:

Air ~: irb
ruby-1.9.2-p180 :001 > a = 1
 => 1 
ruby-1.9.2-p180 :002 > b = 2
 => 2 
ruby-1.9.2-p180 :003 > a + b
 => 3 

有没有办法从命令行删除 ruby-1.9.2-p180 :001

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
>> a + b
=> 3

I installed rvm (and Ruby 1.9.2) and now my irb command prompt looks like this:

Air ~: irb
ruby-1.9.2-p180 :001 > a = 1
 => 1 
ruby-1.9.2-p180 :002 > b = 2
 => 2 
ruby-1.9.2-p180 :003 > a + b
 => 3 

Is there a way to remove the ruby-1.9.2-p180 :001 from the command line?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

烟花易冷人易散 2024-11-15 06:46:14

irb 手册页有一个关于“自定义提示"。这是我的示例:

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

要使用它,请将其添加到您的 ~/.irbrc 文件中(如果不存在则创建它。)

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.)

缪败 2024-11-15 06:46:14

在您的 ~/.irbrc 中,只需添加

IRB.conf[:PROMPT_MODE] = :SIMPLE

In your ~/.irbrc, simply add

IRB.conf[:PROMPT_MODE] = :SIMPLE
听不够的曲调 2024-11-15 06:46:14

当您通常运行 irb 命令时,请尝试运行 irb --simple-prompt 。这大大缩短了提示并使其更容易理解。

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.

半窗疏影 2024-11-15 06:46:14
irb --simple-prompt

在 Lynda.com 上看到了这个

irb --simple-prompt

saw this in Lynda.com

合约呢 2024-11-15 06:46:14

为了避免始终在命令行上给出您想要的提示,您可以 通过~/.irbrc配置文件配置提示符:

$ echo "IRB.conf[:PROMPT_MODE] = :DEFAULT" > ~/.irbrc
$ irb
irb(main):001:0> quit
$ echo "IRB.conf[:PROMPT_MODE] = :SIMPLE" > ~/.irbrc
$ irb
>> quit
$ 

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
$ 
來不及說愛妳 2024-11-15 06:46:14

无论谁想添加提示时间戳,这个 还不可能(检查“特殊字符串”部分),所以我以猴子补丁的方式实现了它:

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

现在,将其添加到您的lib/ 项目文件夹(如果是 Rails 项目,请确保 lib/config/application 中的 config.autoload_paths 的一部分。 rb)或者以更激进的方式(不推荐),在本地 ruby​​ 实例和 lib/irb.rb 文件href="https://github.com/ruby/ruby/blob/93ca212ddac5ac49134f2058c24db3948b6695c6/lib/irb.rb#L699-L736" rel="nofollow noreferrer">def提示方法,向该方法添加一个新的 when 条件,例如:

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

then 在您的.irbrc 文件(它可能位于您的主文件夹或根项目文件夹中)您可以修改提示。我正在添加当前的提示,但请根据您的需要进行调整:

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

然后启动 irb 或rails console 并检查效果:

[1][13:01:15](deve)> 'say hello to your new prompt'
=> "say hello to your new prompt"
[2][13:01:23](deve)>

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)>
箹锭⒈辈孓 2024-11-15 06:46:14

请参阅 RVM 中的有关 IRB 提示的说明

请注意,您可以在主文件夹中创建一个 .irbrc 文件,用于 IRB 的各种设置。
例如,请参阅本文档中的“配置提示

”还可以 puts IRB.conf[:PROMPT_MODE]puts IRB.conf 查看当前生效的所有各种设置。例如,在您的情况下, :PROMPT_MODE 可能设置为“RVM”。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文