/tools/Ruby/lib/ruby/1.8/irb/ext/save-history.rb

http://github.com/agross/netopenspace · Ruby · 99 lines · 59 code · 13 blank · 27 comment · 5 complexity · 01fa2d6d7f86fe4b469a3a2ca67a1323 MD5 · raw file

  1. #!/usr/local/bin/ruby
  2. #
  3. # save-history.rb -
  4. # $Release Version: 0.9.5$
  5. # $Revision: 24483 $
  6. # $Date: 2009-08-09 17:44:15 +0900 (Sun, 09 Aug 2009) $
  7. # by Keiju ISHITSUKAkeiju@ruby-lang.org)
  8. #
  9. # --
  10. #
  11. #
  12. #
  13. require "readline"
  14. module IRB
  15. module HistorySavingAbility
  16. @RCS_ID='-$Id: save-history.rb 24483 2009-08-09 08:44:15Z shyouhei $-'
  17. end
  18. class Context
  19. def init_save_history
  20. unless (class<<@io;self;end).include?(HistorySavingAbility)
  21. @io.extend(HistorySavingAbility)
  22. end
  23. end
  24. def save_history
  25. IRB.conf[:SAVE_HISTORY]
  26. end
  27. def save_history=(val)
  28. IRB.conf[:SAVE_HISTORY] = val
  29. if val
  30. main_context = IRB.conf[:MAIN_CONTEXT]
  31. main_context = self unless main_context
  32. main_context.init_save_history
  33. end
  34. end
  35. def history_file
  36. IRB.conf[:HISTORY_FILE]
  37. end
  38. def history_file=(hist)
  39. IRB.conf[:HISTORY_FILE] = hist
  40. end
  41. end
  42. module HistorySavingAbility
  43. include Readline
  44. # def HistorySavingAbility.create_finalizer
  45. # proc do
  46. # if num = IRB.conf[:SAVE_HISTORY] and (num = num.to_i) > 0
  47. # if hf = IRB.conf[:HISTORY_FILE]
  48. # file = File.expand_path(hf)
  49. # end
  50. # file = IRB.rc_file("_history") unless file
  51. # open(file, 'w' ) do |f|
  52. # hist = HISTORY.to_a
  53. # f.puts(hist[-num..-1] || hist)
  54. # end
  55. # end
  56. # end
  57. # end
  58. def HistorySavingAbility.extended(obj)
  59. # ObjectSpace.define_finalizer(obj, HistorySavingAbility.create_finalizer)
  60. IRB.conf[:AT_EXIT].push proc{obj.save_history}
  61. obj.load_history
  62. obj
  63. end
  64. def load_history
  65. hist = IRB.conf[:HISTORY_FILE]
  66. hist = IRB.rc_file("_history") unless hist
  67. if File.exist?(hist)
  68. open(hist) do |f|
  69. f.each {|l| HISTORY << l.chomp}
  70. end
  71. end
  72. end
  73. def save_history
  74. if num = IRB.conf[:SAVE_HISTORY] and (num = num.to_i) > 0
  75. if history_file = IRB.conf[:HISTORY_FILE]
  76. history_file = File.expand_path(history_file)
  77. end
  78. history_file = IRB.rc_file("_history") unless history_file
  79. open(history_file, 'w' ) do |f|
  80. hist = HISTORY.to_a
  81. f.puts(hist[-num..-1] || hist)
  82. end
  83. end
  84. end
  85. end
  86. end