Class: Intar::Prompt
Instance Method Summary collapse
- #ask(prompt) ⇒ Object
-
#initialize(histfile: nil, limit: nil) ⇒ Prompt
constructor
A new instance of Prompt.
- #last ⇒ Object
- #limit_history(max) ⇒ Object
- #load_history(filepath) ⇒ Object
- #push(text) ⇒ Object
- #push_history(item) ⇒ Object
- #save_history(filepath, maxsize) ⇒ Object
- #scan_history ⇒ Object
Constructor Details
#initialize(histfile: nil, limit: nil) ⇒ Prompt
13 14 15 16 17 |
# File 'lib/intar/prompt.rb', line 13 def initialize histfile: nil, limit: nil @limit = limit.nonzero? Readline::HISTORY.clear @new = 0 end |
Instance Method Details
#ask(prompt) ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/intar/prompt.rb', line 27 def ask prompt begin Readline.readline prompt ensure Readline.pre_input_hook = nil end rescue Interrupt puts retry rescue Exception puts raise end |
#last ⇒ Object
41 42 43 |
# File 'lib/intar/prompt.rb', line 41 def last Readline::HISTORY[-1] unless Readline::HISTORY.empty? end |
#limit_history(max) ⇒ Object
101 102 103 104 105 106 |
# File 'lib/intar/prompt.rb', line 101 def limit_history max n = Readline::HISTORY.length - max n.times { Readline::HISTORY.shift } @new > max and @new = max nil end |
#load_history(filepath) ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/intar/prompt.rb', line 60 def load_history filepath with_filepath filepath do |p| read_file_if p do |f| h = [] @new.times { h.push Readline::HISTORY.pop } Readline::HISTORY.clear f.each_line { |l| l.chomp! l.sub! "\r", "\n" Readline::HISTORY.push l } Readline::HISTORY.push h.pop while h.any? end nil end end |
#push(text) ⇒ Object
19 20 21 22 23 24 25 |
# File 'lib/intar/prompt.rb', line 19 def push text Readline.pre_input_hook = proc { Readline.insert_text text Readline.redisplay } nil end |
#push_history(item) ⇒ Object
53 54 55 56 57 58 |
# File 'lib/intar/prompt.rb', line 53 def push_history item item.empty? and return last != item or return Readline::HISTORY.push item @new += 1 end |
#save_history(filepath, maxsize) ⇒ Object
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/intar/prompt.rb', line 77 def save_history filepath, maxsize with_filepath filepath do |p| lock_histfile p do old, m = [], maxsize-@new read_file_if p do |f| f.each_line { |l| old.size >= m and old.shift old.push l } end File.open p, "w" do |f| old.each { |l| f.puts l } i = Readline::HISTORY.length - @new while i < Readline::HISTORY.length do l = Readline::HISTORY[ i].sub "\n", "\r" f.puts l i += 1 end end end nil end end |
#scan_history ⇒ Object
45 46 47 48 49 50 51 |
# File 'lib/intar/prompt.rb', line 45 def scan_history i = Readline::HISTORY.length while i > 0 do i -= 1 yield Readline::HISTORY[i] end end |