是否可以将 IRB 提示配置为动态更改?

发布于 2024-11-08 21:35:30 字数 670 浏览 0 评论 0原文

我想在 IRB 中的文件系统中导航并更改提示符以反映当前工作目录,但我不知道如何在每个命令后更新提示符。最终,我希望在日常工作中更多地使用 IRB,让 bash 消失。我在我的 .irbrc 中尝试了此操作:

require 'fileutils'
include FileUtils

IRB.conf[:PROMPT][:CUSTOM] = {
    :PROMPT_N => "\e[1m:\e[m ",
    :PROMPT_I => "\e[1m#{pwd} >\e[m ",
    :PROMPT_S => "FOO",
    :PROMPT_C => "\e[1m#{pwd} >\e[m ",
    :RETURN => ""
}
IRB.conf[:PROMPT_MODE] = :CUSTOM

但是 IRB 提示未更新:

julianmann@mango:~ > irb
/users/julianmann > puts pwd
/users/julianmann
/users/julianmann > cd 'dev'
/users/julianmann > puts pwd
/users/julianmann/dev
/users/julianmann > 

我真的希望更改提示。

I'd like to navigate around the filesystem in IRB and have the prompt change to reflect the current working directory, but I can't figure out how to make the prompt update after each command. Ultimately I'd like to use IRB in day to day work a lot more and let bash slip away. I tried this in my .irbrc:

require 'fileutils'
include FileUtils

IRB.conf[:PROMPT][:CUSTOM] = {
    :PROMPT_N => "\e[1m:\e[m ",
    :PROMPT_I => "\e[1m#{pwd} >\e[m ",
    :PROMPT_S => "FOO",
    :PROMPT_C => "\e[1m#{pwd} >\e[m ",
    :RETURN => ""
}
IRB.conf[:PROMPT_MODE] = :CUSTOM

But the IRB prompt is not updated:

julianmann@mango:~ > irb
/users/julianmann > puts pwd
/users/julianmann
/users/julianmann > cd 'dev'
/users/julianmann > puts pwd
/users/julianmann/dev
/users/julianmann > 

I'd really like the prompt to change.

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

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

发布评论

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

评论(6

娇俏 2024-11-15 21:35:30

这是获取工作目录的快速技巧。它有点脆弱,但它可以在 ruby​​ 1.8.7 和 1.9.2 上运行。

将提示字符串设置为如下所示:

"%N(%m):%03n:%i %~> ".tap {|s| def s.dup; gsub('%~', Dir.pwd); end }

irb 本身无法理解“%~”指令,因此我用它来进行替换。此 hack 依赖于 irb 调用 dup 来生成提示的事实。

Here's a quick hack to get the working dir. It's sort of fragile, but it worked on ruby 1.8.7 and 1.9.2.

Set your prompt string to something like this:

"%N(%m):%03n:%i %~> ".tap {|s| def s.dup; gsub('%~', Dir.pwd); end }

The "%~" directive is not understood by irb itself, so I used it to do the replacement. This hack relies on the fact that irb calls dup to generate the prompt.

允世 2024-11-15 21:35:30

另一种选择是使用 fresh。它基于 irb 替代方案 ripl 并且还显示当前目录作为提示:]

Another option is to use fresh. It's based on the irb alternative ripl and also shows the current directory as its prompt :]

注定孤独终老 2024-11-15 21:35:30

您必须像这样运行(别名)irb

irb --prompt custom

或者将 IRB.conf[:PROMPT_MODE] = :CUSTOM 添加到您的

.irbrc这不是您问题的准确答案。但您可以尝试使用 RUSH

它没有当前工作目录的概念,但很容易配置。

You have to run(alias) irb like so

irb --prompt custom

Or alternatively add IRB.conf[:PROMPT_MODE] = :CUSTOM to your .irbrc

P.S. This isn't an EXACT answer to your question. But you might try using RUSH.

It doesn't have the concept of a current working directory, but it is easily configurable.

给我一枪 2024-11-15 21:35:30

虽然它有点静态
但看看,它可能对你有帮助
在 Linux ( Ubuntu 14.04) 中,

您只需按照一些简单的步骤即可更改 irb 控制台的恼人提示

打开终端

,转到位置 /home/leapfrog/.rvm/scripts

$ cd ~/.rvm/scripts

打开文件“irbrc.rb”,使用超级用户权限覆盖

$ sudo gedit irbrc.rb

你可以看到这样的一部分代码。将前者替换为后者代码

# Set up the prompt to be RVM specific.
#@prompt = {
# :PROMPT_I => "#{rvm_ruby_string} :%03n > ", # default prompt
# :PROMPT_S => "#{rvm_ruby_string} :%03n%l> ", # known continuation
# :PROMPT_C => "#{rvm_ruby_string} :%03n > ",
# :PROMPT_N => "#{rvm_ruby_string} :%03n?> ", # unknown continuation
# :RETURN => " => %s \n",
# :AUTO_INDENT => true
#}

@prompt = {
 :PROMPT_I => "ROR: %03n > ", # default prompt
 :PROMPT_S => "%03n%l> ", # known continuation
 :PROMPT_C => "%03n > ",
 :PROMPT_N => "%03n?> ", # unknown continuation
 :RETURN => " O/P => %s \n",
 :AUTO_INDENT => true
}

只需保存文件并重新启动 irb 控制台
更多模式你可以看这个链接
https://cbabhusal.wordpress.com/ 2014/12/22/ruby-rvm-change-prompt-of-irb/

Its kind of static though
but have a look, it may help you
IN Linux ( Ubuntu 14.04)

You can change the irritating prompt of the irb console just by following some simple steps

Open your terminal

goto the location /home/leapfrog/.rvm/scripts

$ cd ~/.rvm/scripts

Open the file ‘irbrc.rb’, use superuser power to over-write the

$ sudo gedit irbrc.rb

You can see a portion of code like this. Replace the former with latter codes

# Set up the prompt to be RVM specific.
#@prompt = {
# :PROMPT_I => "#{rvm_ruby_string} :%03n > ", # default prompt
# :PROMPT_S => "#{rvm_ruby_string} :%03n%l> ", # known continuation
# :PROMPT_C => "#{rvm_ruby_string} :%03n > ",
# :PROMPT_N => "#{rvm_ruby_string} :%03n?> ", # unknown continuation
# :RETURN => " => %s \n",
# :AUTO_INDENT => true
#}

@prompt = {
 :PROMPT_I => "ROR: %03n > ", # default prompt
 :PROMPT_S => "%03n%l> ", # known continuation
 :PROMPT_C => "%03n > ",
 :PROMPT_N => "%03n?> ", # unknown continuation
 :RETURN => " O/P => %s \n",
 :AUTO_INDENT => true
}

Just save the file and restart the irb console
Further mode you can see this link
https://cbabhusal.wordpress.com/2014/12/22/ruby-rvm-change-prompt-of-irb/

左耳近心 2024-11-15 21:35:30

做出贡献,尽管为时已晚:可以在 IRB 环境初始化后更改提示,例如通过 IRB.conf[:MAIN_CONTEXT] 上的某些值

如果它可以对讨论 c = IRB.conf[:MAIN_CONTEXT] 的绑定影响提示格式的字段可能包括以下

  • c.prompt_c
  • c.prompt_i
  • c.prompt_n
  • c.prompt_s
  • c.return_format
  • c.auto_indent_mode
  • c.prompt_mode

直接更新 prompt_i 字段的示例:


irb(main):009:0> IRB.conf[:MAIN_CONTEXT].prompt_i="%N %m %i >>"
=> "%N %m %i >>"

irb main 0 >>

IRB 模块文档 (3.0.0) 它可能不受支持 -目前 - 在 IRB 中使用显示提示时将计算的表达式。每个提示字符串可以简单地用作文字格式字符串。

尽管有一些限制,但在 IRB 初始化后可以更新 IRB 提示。

免责声明:这不能保证更新 IRB.conf[:MAIN_CONTEXT] 下与提示相关的所有状态值

If it may serve to contribute to the discussion, albeit belatedly: It may be possible to change the prompt after the IRB environment is initialized, such as via some values on IRB.conf[:MAIN_CONTEXT]

For a binding of c = IRB.conf[:MAIN_CONTEXT] the fields affecting prompt formatting may include the following

  • c.prompt_c
  • c.prompt_i
  • c.prompt_n
  • c.prompt_s
  • c.return_format
  • c.auto_indent_mode
  • c.prompt_mode

An example in updating the prompt_i field directly:


irb(main):009:0> IRB.conf[:MAIN_CONTEXT].prompt_i="%N %m %i >>"
=> "%N %m %i >>"

irb main 0 >>

Outside of the set of formatting specifiers documented in the IRB module documentation (3.0.0) it may not be supported - at present - to use an expression that would be evaluated when the prompt is displayed, in IRB. Each of the prompt strings may be used as simply a literal format string.

With some limitations albeit, but it may be possible to update the IRB prompt after IRB is initialized.

Disclaimer: This is not guaranteed to update all state values related to the prompt, under IRB.conf[:MAIN_CONTEXT]

旧竹 2024-11-15 21:35:30

Kelvin 聪明的“快速破解”答案依赖于 dup,但它不再适用于最新版本的 IRB(现在是 v1.11.0)。

这是允许提供动态 IRB 提示的替代方案,它利用了 main.to_s 在 IRB 提示配置中作为 %m 提供的事实:

# Save this file as `.irbrc`.
#
# Put it in your home directory to make it your default,
# or in a particular directory where you want it.
#
# Note: If you have an `.irbrc` in your home directory it
#       will take precedence over a local `.irbrc`.

# define `main.to_s` to provide dynamic context info.
# `main.to_s` is `%m` in the IRB prompt.
# `%m` is included in the default IRB prompt.
def self.to_s
  "#{Dir.pwd}"
end

# This should be the default already, but adding this line
# ensures you use an IRB prompt config that includes `%m`.
IRB.conf[:PROMPT_MODE] = :DEFAULT

Kelvin's clever "quick hack" answer, which relied on dup, doesn't work anymore with the latest version of IRB (v1.11.0 right now).

Here's an alternative that allows providing a dynamic IRB prompt, which takes advantage of the fact that main.to_s is provided as %m in the IRB prompt configs:

# Save this file as `.irbrc`.
#
# Put it in your home directory to make it your default,
# or in a particular directory where you want it.
#
# Note: If you have an `.irbrc` in your home directory it
#       will take precedence over a local `.irbrc`.

# define `main.to_s` to provide dynamic context info.
# `main.to_s` is `%m` in the IRB prompt.
# `%m` is included in the default IRB prompt.
def self.to_s
  "#{Dir.pwd}"
end

# This should be the default already, but adding this line
# ensures you use an IRB prompt config that includes `%m`.
IRB.conf[:PROMPT_MODE] = :DEFAULT
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文