class Intar::History

Constants

IND
IND_RE

Public Class Methods

new(filename) click to toggle source
# File lib/intar.rb, line 62
def initialize filename
  @filename = filename
  return unless @filename
  h = "~" unless @filename.starts_with "./"
  @filename = File.expand_path @filename, h
  File.exists? @filename and File.open @filename do |h|
    c = []
    h.each { |l|
      case l
        when IND_RE then c.push $'
        else             push c.join.chomp ; c.clear
      end
    }
    push c.join.chomp
  end
  @num = Readline::HISTORY.length
end

Public Instance Methods

finish(max) click to toggle source
# File lib/intar.rb, line 80
def finish max
  return unless @filename
  @num.times { Readline::HISTORY.shift }
  File.open @filename, "a" do |h|
    a = []
    while (c = Readline::HISTORY.shift) do a.push c end
    if a.any? then
      h.puts "# #{Time.now}"
      a.each { |c|
        c.each_line { |l| h.puts IND + l }
        h.puts "-"
      }
      i = a.length
      h.puts "# #{i} #{entry_str i} added"
    end
  end
  n = File.open @filename do |h|
    h.inject 0 do |i,l| i += 1 if l =~ IND_RE ; i end
  end
  i = max - n
  if i < 0 then
    f = nil
    File.open @filename do |h|
      h.each_line { |l|
        f.push l if f
        case l
          when IND_RE then i += 1
          else             f ||= [] if i >= 0
        end
      }
    end
    f and File.open @filename, "w" do |h| h.puts f end
  end
rescue Errno
  # Forget it if there isn't enough disk space.
end
push(l) click to toggle source
# File lib/intar.rb, line 117
def push l
  Readline::HISTORY.push l unless l.empty?
end

Private Instance Methods

entry_str(i) click to toggle source
# File lib/intar.rb, line 123
def entry_str i
  i == 1 ? "entry" : "entries"
end