【问题标题】:why does pry history keep cloberring irb history?为什么 pry history 不断破坏 irb 的历史?
【发布时间】:2015-12-29 20:44:43
【问题描述】:

当我使用 pry 时,pry 的历史会不断覆盖我的 irb 历史,这很烦人。如果我使用 pry,我想查看 pry 历史,如果我使用 irb,我想查看 irb 历史。我的配置是否存在明显问题?

~/.irbrc 看起来像

IRB.conf[:AUTO_INDENT] = true
IRB.conf[:USE_READLINE] = true
IRB.conf[:LOAD_MODULES] = [] unless IRB.conf.key?(:LOAD_MODULES)
unless IRB.conf[:LOAD_MODULES].include?('irb/completion')
  IRB.conf[:LOAD_MODULES] << 'irb/completion'
end
IRB.conf[:SAVE_HISTORY] = 1000

我的.pryrc 文件是空的,根据文档向我表明 pry 应该使用 .pry_history,这似乎正在发生。

/etc/irbrc 看起来像

# Some default enhancements/settings for IRB, based on
# http://wiki.rubygarden.org/Ruby/page/show/Irb/TipsAndTricks

unless defined? ETC_IRBRC_LOADED

  # Require RubyGems by default.
  require 'rubygems'

  # Activate auto-completion.
  require 'irb/completion'

  # Use the simple prompt if possible.
  IRB.conf[:PROMPT_MODE] = :SIMPLE if IRB.conf[:PROMPT_MODE] == :DEFAULT

  # Setup permanent history.
  HISTFILE = "~/.irb_history"
  MAXHISTSIZE = 100
  begin
    histfile = File::expand_path(HISTFILE)
    if File::exists?(histfile)
      lines = IO::readlines(histfile).collect { |line| line.chomp }
      puts "Read #{lines.nitems} saved history commands from '#{histfile}'." if $VERBOSE
      Readline::HISTORY.push(*lines)
    else
      puts "History file '#{histfile}' was empty or non-existant." if $VERBOSE
    end
    Kernel::at_exit do
      lines = Readline::HISTORY.to_a.reverse.uniq.reverse
      lines = lines[-MAXHISTSIZE, MAXHISTSIZE] if lines.nitems > MAXHISTSIZE
      puts "Saving #{lines.length} history lines to '#{histfile}'." if $VERBOSE
      File::open(histfile, File::WRONLY|File::CREAT|File::TRUNC) { |io| io.puts lines.join("\n") }
    end
  rescue => e
    puts "Error when configuring permanent history: #{e}" if $VERBOSE
  end

  ETC_IRBRC_LOADED=true
end

【问题讨论】:

  • 如果在 .pryrc 文件中指定历史文件位置会发生什么(例如 here)?
  • 我不确定这是否有意义,因为我确定 .pryrc 正在工作,我认为手头的真正问题是如何排除写入 .pry_history 的命令也被写入进入 .irb_history 文件
  • 我实际上告诉 pry 明确地使用 .irb_history 并且这似乎运作良好,我可以从任何地方访问任何命令,这很好,所以我想我会坚持我得到的。
  • 等等,什么?你是说问题是 pry 正在写 irb_history 和 pry_history (而且你不希望它破坏 irb_history)?但后来你告诉 pry 明确写信给 irb_history,现在你很高兴吗?
  • 嗯,我不知道如何解释,但我认为整个 pry_history 正在覆盖 irb 历史并破坏 irb 历史,方法是设置 pry 显式写入 irb_history,现在整个 irb_history 不是t 被覆盖,所以基本上,我不知道,但我对我所拥有的更满意 :)

标签: ruby pry


【解决方案1】:

为什么会出现问题

Pry 和 IRB 都将他们的历史记录写到 Readline::HISTORY。当您从 IRB(或rails console)进入 Pry 时,您的所有 IRB 历史记录已经在 Readline::HISTORY 中。然后,Pry 会在此基础上加载其历史记录。当您退出 Pry 时,Readline::HISTORY 不会更改,因此您最终会回到 IRB,所有 Pry 的历史都附加到 IRB 的历史中。最后,当您退出 IRB 时,它会将包括 Pry 在内的历史记录写入 IRB 的历史记录文件,从而破坏它。

问题的当前状态

我做了一些搜索,发现这是带有 Pry 的 known issue。我也对这个问题非常感兴趣,所以我设法找到了一个正在等待pull request review 的解决方案。

合并之前的解决方法

如果您想在它被合并之前试用它,您可以从我在 Pry 的 fork 上创建的分支中获取版本。您可以在分支save_irb_history_and_replace_on_close 下的https://github.com/agrberg/pry 找到它。如果您使用bundler,您需要做的就是添加或更新您的线路以进行撬动:

gem 'pry', git: 'git@github.com:agrberg/pry.git', branch: 'save_irb_history_and_replace_on_close'

【讨论】:

    猜你喜欢
    • 2020-09-25
    • 1970-01-01
    • 2016-10-17
    • 2011-01-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-09
    相关资源
    最近更新 更多