Sha256: 07698139dd6ce222d346d8449abebe8efaaccfd0b19fa3b2008bf60ed70e2388

Contents?: true

Size: 1.07 KB

Versions: 1

Compression:

Stored size: 1.07 KB

Contents

require 'ripl/rc/u'

module Ripl::Rc::SqueezeHistory
  include Ripl::Rc::U

  # avoid some complicated conditions...
  def history
    super || (@history ||= [])
  end

  # write squeezed history
  def write_history
    return super if SqueezeHistory.disabled?
    File.open(history_file, 'w'){ |f|
      f.puts U.squeeze_history(history).join("\n")
    }
  end

  # squeeze history on memory too
  def eval_input input
    return super if SqueezeHistory.disabled?
    history.pop if input.strip == '' ||
                  (history.size > 1 && input == history[-2])
    super
  end

  def before_loop
    return super if SqueezeHistory.disabled?
    super if history.empty?
  end

  module Imp
    def squeeze_history history
      history.to_a.inject([]){ |result, item|
        if result.last == item
          result
        else
          result << item
        end
      }.last(Ripl.config[:rc_squeeze_history_size])
    end
  end
end

module Ripl::Rc::U; extend Ripl::Rc::SqueezeHistory::Imp; end

Ripl::Shell.include(Ripl::Rc::SqueezeHistory)
Ripl.config[:rc_squeeze_history_size] ||= 500

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
ripl-rc-0.2.0 lib/ripl/rc/squeeze_history.rb