Sha256: f185fe91e28f49a967b3b829d997fb15e32b003bbdfac039a841208fa44482cc

Contents?: true

Size: 1.14 KB

Versions: 23

Compression:

Stored size: 1.14 KB

Contents

require 'rib/more/multiline_history'

module Rib::MultilineHistoryFile
  extend Rib::Plugin
  Shell.use(self)

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

  def before_loop
    return super if MultilineHistoryFile.disabled?
    multiline_history_file_token
    super
  end

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

  def read_history
    return super if MultilineHistoryFile.disabled?
    buffer = []
    File.exist?(history_file_path) && history.empty? &&
      IO.readlines(history_file_path).each{ |line|
        if line.end_with?(
             "#{config[:multiline_history_file_token]}\n")
          buffer << line[0...
                         -multiline_history_file_token.size-1] + "\n"
        else
          history << (buffer.join + line).chomp
          buffer = []
        end
      }
  end

  def write_history
    return super if MultilineHistoryFile.disabled?
    # TODO: hisotroy.map is MRI 1.9+
    config[:history] = history.to_a.map{ |line|
      line.gsub("\n", "#{config[:multiline_history_file_token]}\n")
    }
    super
  end



  private
  def multiline_history_file_token
    config[:multiline_history_file_token] ||= ' '
  end
end

Version data entries

23 entries across 23 versions & 1 rubygems

Version Path
rib-1.5.1 lib/rib/more/multiline_history_file.rb
rib-1.5.0 lib/rib/more/multiline_history_file.rb
rib-1.4.0 lib/rib/more/multiline_history_file.rb
rib-1.3.1 lib/rib/more/multiline_history_file.rb
rib-1.3.0 lib/rib/more/multiline_history_file.rb
rib-1.2.91 lib/rib/more/multiline_history_file.rb
rib-1.2.9 lib/rib/more/multiline_history_file.rb
rib-1.2.8 lib/rib/more/multiline_history_file.rb
rib-1.2.7 lib/rib/more/multiline_history_file.rb
rib-1.2.6 lib/rib/more/multiline_history_file.rb
rib-1.2.5 lib/rib/more/multiline_history_file.rb
rib-1.2.4 lib/rib/more/multiline_history_file.rb
rib-1.2.3 lib/rib/more/multiline_history_file.rb
rib-1.2.2 lib/rib/more/multiline_history_file.rb
rib-1.2.1 lib/rib/more/multiline_history_file.rb
rib-1.2.0 lib/rib/more/multiline_history_file.rb
rib-1.1.6 lib/rib/more/multiline_history_file.rb
rib-1.1.5 lib/rib/more/multiline_history_file.rb
rib-1.1.4 lib/rib/more/multiline_history_file.rb
rib-1.1.3 lib/rib/more/multiline_history_file.rb