2019-06-23 00:29:09 -04:00
|
|
|
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)
|
2019-12-01 08:11:59 -05:00
|
|
|
index = check_index(index) unless index.is_a?(Range)
|
2019-06-23 00:29:09 -04:00
|
|
|
super(index)
|
|
|
|
end
|
|
|
|
|
|
|
|
def []=(index, val)
|
|
|
|
index = check_index(index)
|
2020-01-12 08:24:17 -05:00
|
|
|
super(index, String.new(val, encoding: Reline.encoding_system_needs))
|
2019-06-23 00:29:09 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def concat(*val)
|
|
|
|
val.each do |v|
|
|
|
|
push(*v)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def push(*val)
|
|
|
|
diff = size + val.size - @config.history_size
|
|
|
|
if diff > 0
|
|
|
|
if diff <= size
|
|
|
|
shift(diff)
|
|
|
|
else
|
|
|
|
diff -= size
|
|
|
|
clear
|
|
|
|
val.shift(diff)
|
|
|
|
end
|
|
|
|
end
|
2020-01-12 08:24:17 -05:00
|
|
|
super(*(val.map{ |v| String.new(v, encoding: Reline.encoding_system_needs) }))
|
2019-06-23 00:29:09 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def <<(val)
|
|
|
|
shift if size + 1 > @config.history_size
|
2020-01-12 08:24:17 -05:00
|
|
|
super(String.new(val, encoding: Reline.encoding_system_needs))
|
2019-06-23 00:29:09 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
private def check_index(index)
|
|
|
|
index += size if index < 0
|
|
|
|
raise RangeError.new("index=<#{index}>") if index < -@config.history_size or @config.history_size < index
|
|
|
|
raise IndexError.new("index=<#{index}>") if index < 0 or size <= index
|
|
|
|
index
|
|
|
|
end
|
|
|
|
end
|