Sha256: dee0abdd6f7d832f25266b1f76c79bf3d6cbf7eaa2e9e728cc261f0797a6c864

Contents?: true

Size: 1.51 KB

Versions: 5

Compression:

Stored size: 1.51 KB

Contents

require 'rib'
require 'fileutils'

module Rib; module History
  extend 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?
    history_text = "#{history.to_a.last(history_size).join("\n")}\n"
    File.write(history_file_path, history_text)
  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; end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
rib-1.6.1 lib/rib/core/history.rb
rib-1.6.0 lib/rib/core/history.rb
rib-1.5.4 lib/rib/core/history.rb
rib-1.5.3 lib/rib/core/history.rb
rib-1.5.2 lib/rib/core/history.rb