Sha256: def03d40c1ff2d80b1852de484119cd59698baac0ca78d2bef6866ad09e2a321

Contents?: true

Size: 980 Bytes

Versions: 2

Compression:

Stored size: 980 Bytes

Contents

require 'ripl/rc/u'

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

  # 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.to_a[-2])
                  # EditLine is really broken, to_a is needed for it
    super
  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

2 entries across 2 versions & 1 rubygems

Version Path
ripl-rc-0.2.3 lib/ripl/rc/squeeze_history.rb
ripl-rc-0.2.2 lib/ripl/rc/squeeze_history.rb