class Intar::Prompt
Public Class Methods
new(histfile: nil, limit: nil)
click to toggle source
# File lib/intar/prompt.rb, line 13 def initialize histfile: nil, limit: nil @limit = limit.nonzero? Readline::HISTORY.clear @new = 0 end
Public Instance Methods
ask(prompt)
click to toggle source
# File lib/intar/prompt.rb, line 19 def ask prompt l = Readline.readline prompt rescue Interrupt puts "^C -- #{$!.inspect}" retry end
last()
click to toggle source
# File lib/intar/prompt.rb, line 26 def last Readline::HISTORY[-1] unless Readline::HISTORY.empty? end
limit_history(max)
click to toggle source
# File lib/intar/prompt.rb, line 87 def limit_history max n = Readline::HISTORY.length - max n.times { Readline::HISTORY.shift } @new > max and @new = max nil end
load_history(filepath)
click to toggle source
# File lib/intar/prompt.rb, line 46 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(item)
click to toggle source
# File lib/intar/prompt.rb, line 39 def push item item.empty? and return last != item or return Readline::HISTORY.push item @new += 1 end
save_history(filepath, maxsize)
click to toggle source
# File lib/intar/prompt.rb, line 63 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() { |l| ... }
click to toggle source
# File lib/intar/prompt.rb, line 30 def scan_history i = Readline::HISTORY.length while i > 0 do i -= 1 l = Readline::HISTORY[i] yield l end end
Private Instance Methods
lock_histfile(filepath) { || ... }
click to toggle source
# File lib/intar/prompt.rb, line 111 def lock_histfile filepath l = "#{filepath}.lock" loop do File.open l, File::CREAT|File::EXCL do end break rescue Errno::EEXIST puts "Lockfile #{l} exists." sleep 1 end yield ensure File.unlink l end
read_file_if(filepath) { |f| ... }
click to toggle source
# File lib/intar/prompt.rb, line 103 def read_file_if filepath if File.exist? filepath then File.open filepath do |f| yield f end end end
with_filepath(filepath) { |p| ... }
click to toggle source
# File lib/intar/prompt.rb, line 96 def with_filepath filepath if filepath then p = File.expand_path filepath, "~" yield p end end