Sha256: c9ffe18becf431d1ff03e03527ec576f42d6e6b7347763b9ebf5f3c4298c1971

Contents?: true

Size: 1.87 KB

Versions: 17

Compression:

Stored size: 1.87 KB

Contents

class Reline::History < Array
  def initialize(config)
    @config = config
  end

  def to_s
    'HISTORY'
  end

  def delete_at(index)
    index = check_index(index)
    super(index)
  end

  def [](index)
    index = check_index(index) unless index.is_a?(Range)
    super(index)
  end

  def []=(index, val)
    index = check_index(index)
    super(index, String.new(val, encoding: Reline.encoding_system_needs))
  end

  def concat(*val)
    val.each do |v|
      push(*v)
    end
  end

  def push(*val)
    # If history_size is zero, all histories are dropped.
    return self if @config.history_size.zero?
    # If history_size is negative, history size is unlimited.
    if @config.history_size.positive?
      diff = size + val.size - @config.history_size
      if diff > 0
        if diff <= size
          shift(diff)
        else
          diff -= size
          clear
          val.shift(diff)
        end
      end
    end
    super(*(val.map{ |v|
      String.new(v, encoding: Reline.encoding_system_needs)
    }))
  end

  def <<(val)
    # If history_size is zero, all histories are dropped.
    return self if @config.history_size.zero?
    # If history_size is negative, history size is unlimited.
    if @config.history_size.positive?
      shift if size + 1 > @config.history_size
    end
    super(String.new(val, encoding: Reline.encoding_system_needs))
  end

  private def check_index(index)
    index += size if index < 0
    if index < -2147483648 or 2147483647 < index
      raise RangeError.new("integer #{index} too big to convert to 'int'")
    end
    # If history_size is negative, history size is unlimited.
    if @config.history_size.positive?
      if index < -@config.history_size or @config.history_size < index
        raise RangeError.new("index=<#{index}>")
      end
    end
    raise IndexError.new("index=<#{index}>") if index < 0 or size <= index
    index
  end
end

Version data entries

17 entries across 17 versions & 5 rubygems

Version Path
minato_ruby_api_client-0.2.2 vendor/bundle/ruby/3.2.0/gems/reline-0.5.9/lib/reline/history.rb
brakeman-6.2.2 bundle/ruby/3.1.0/gems/reline-0.5.10/lib/reline/history.rb
brakeman-6.2.2.rc1 bundle/ruby/3.3.0/gems/reline-0.5.10/lib/reline/history.rb
reline-0.5.10 lib/reline/history.rb
brakeman-6.2.1 bundle/ruby/3.1.0/gems/reline-0.5.9/lib/reline/history.rb
brakeman-6.2.0 bundle/ruby/3.1.0/gems/reline-0.5.9/lib/reline/history.rb
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/reline-0.5.9/lib/reline/history.rb
reline-0.5.9 lib/reline/history.rb
tinymce-rails-7.1.2 vendor/bundle/ruby/3.3.0/gems/reline-0.5.8/lib/reline/history.rb
reline-0.5.8 lib/reline/history.rb
reline-0.5.5 lib/reline/history.rb
reline-0.5.4 lib/reline/history.rb
reline-0.5.3 lib/reline/history.rb
reline-0.5.2 lib/reline/history.rb
reline-0.5.1 lib/reline/history.rb
reline-0.5.0 lib/reline/history.rb
reline-0.4.3 lib/reline/history.rb