Sha256: 8105217ed44f9ab663138a2b87ba0f79b65bac085f8bba6efab3707423805c6a

Contents?: true

Size: 1.49 KB

Versions: 5

Compression:

Stored size: 1.49 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?
    history_file
    history_size
    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(history_size).join("\n")) }
  end



  private
  def history_file
    config[:history_file]      ||= File.join(Rib.home, 'history.rb')
  end

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

  def history_size
    config[:history_size] ||= 500
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
rib-1.0.5 lib/rib/core/history.rb
rib-1.0.4 lib/rib/core/history.rb
rib-1.0.3 lib/rib/core/history.rb
rib-1.0.2 lib/rib/core/history.rb
rib-1.0.1 lib/rib/core/history.rb