Created
October 18, 2024 18:20
-
-
Save Drowze/edc11d30fdc7190040e3f025dde414ab to your computer and use it in GitHub Desktop.
Avoid overwriting irb history with pry (when opening a pry session inside an existing irb session)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'readline' | |
def __flush_history | |
Readline::HISTORY.slice!(0..-1) | |
end | |
def __restore_history(irb_history) | |
__flush_history | |
irb_history.each { |line| Readline::HISTORY << line } | |
end | |
# pry initializes its history from the file before the before_session hook | |
# is called, so we must collect the history before that | |
__irb_history = __flush_history # rubocop:disable Lint/UnderscorePrefixedVariableName | |
Pry.config.history_file = '~/.pry_history' | |
Pry.config.hooks.add_hook(:before_session, :trigger_save_irb_history) do | |
__irb_history = __flush_history if __irb_history.nil? | |
Pry.history.load # reload the pry history from file | |
end | |
Pry.config.hooks.add_hook(:after_session, :trigger_restore_irb_history) do | |
__restore_history(__irb_history) | |
__irb_history = nil | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment