Sha256: f3d4cb64925bee9c54a19bea5aa1abf01e3548778ec2626e92bdce5293d7035d

Contents?: true

Size: 1.42 KB

Versions: 9

Compression:

Stored size: 1.42 KB

Contents

require 'rib'
require 'fileutils'

module Rib::History
  include Rib::Plugin
  Shell.use(self)

  # --------------- Rib API ---------------

  def before_loop
    return super if History.disabled?
    config[:history_file] ||= File.join(Rib.home, 'history.rb')
    config[:history_size] ||= 500
    FileUtils.mkdir_p(File.dirname(history_file_path))
    read_history
    Rib.say("History read from: #{history_file_path}") if $VERBOSE
    super
  end

  def after_loop
    return super if History.disabled?
    write_history
    Rib.say("History wrote to: #{history_file_path}") if $VERBOSE
    super
  end

  def get_input
    return super if History.disabled?
    (history << super).last
  end

  # --------------- Plugin API ---------------

  # The history data
  def history; config[:history] ||= []; end

  # Read config[:history_file] into #history, handled in history_file plugin
  def read_history
    return super if History.disabled?
    File.exist?(history_file_path) && history.empty? &&
      File.readlines(history_file_path).each{ |e| history << e.chomp }
  end

  # Write #history into config[:history_file], handled in history_file plugin
  def write_history
    return super if History.disabled?
    File.open(history_file_path, 'w'){ |f|
      f.puts(history.to_a.last(config[:history_size]).join("\n")) }
  end



  private
  def history_file_path
    config[:history_file_path] ||= File.expand_path(config[:history_file])
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
rib-1.0.0 lib/rib/core/history.rb
rib-0.9.9 lib/rib/core/history.rb
rib-0.9.5 lib/rib/core/history.rb
rib-0.9.5.pre.1 lib/rib/core/history.rb
rib-0.9.5.pre.0 lib/rib/core/history.rb
rib-0.9.4 lib/rib/core/history.rb
rib-0.9.3 lib/rib/core/history.rb
rib-0.9.2 lib/rib/core/history.rb
rib-0.9.1 lib/rib/core/history.rb