Guest User

Untitled

a guest
Feb 21st, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.42 KB | None | 0 0
  1. # bleything's shell history for irb
  2. def history(how_many = 50)
  3. history_size = Readline::HISTORY.size
  4.  
  5. # no lines, get out of here
  6. puts "No history" and return if history_size == 0
  7.  
  8. start_index = 0
  9.  
  10. # not enough lines, only show what we have
  11. if history_size <= how_many
  12. how_many = history_size - 1
  13. end_index = how_many
  14. else
  15. end_index = history_size - 1 # -1 to adjust for array offset
  16. start_index = end_index - how_many
  17. end
  18.  
  19. start_index.upto(end_index) {|i| print_line i}
  20. nil
  21. end
  22. alias :h :history
  23.  
  24. # -2 because -1 is ourself
  25. def history_do(lines = (Readline::HISTORY.size - 2))
  26. irb_eval lines
  27. nil
  28. end
  29. alias :h! :history_do
  30.  
  31. def history_write(filename, lines)
  32. file = File.open(filename, 'w')
  33.  
  34. get_lines(lines).each do |l|
  35. file << "#{l}\n"
  36. end
  37.  
  38. file.close
  39. end
  40. alias :hw :history_write
  41.  
  42. private
  43. def get_line(line_number)
  44. Readline::HISTORY[line_number]
  45. end
  46.  
  47. def get_lines(lines = [])
  48. return [get_line(lines)] if lines.is_a? Fixnum
  49.  
  50. out = []
  51.  
  52. lines = lines.to_a if lines.is_a? Range
  53.  
  54. lines.each do |l|
  55. out << Readline::HISTORY[l]
  56. end
  57.  
  58. return out
  59. end
  60.  
  61. def print_line(line_number, show_line_numbers = true)
  62. print "[%04d] " % line_number if show_line_numbers
  63. puts get_line(line_number)
  64. end
  65.  
  66. def irb_eval(lines)
  67. to_eval = get_lines(lines)
  68.  
  69. eval to_eval.join("\n")
  70.  
  71. to_eval.each {|l| Readline::HISTORY << l}
  72. end
Add Comment
Please, Sign In to add comment