class Byebug::History

Handles byebug's history of commands.

Attributes

size[RW]

Public Class Methods

new() click to toggle source
# File lib/byebug/history.rb, line 20
def initialize
  self.size = 0
end

Public Instance Methods

clear() click to toggle source

Discards history.

# File lib/byebug/history.rb, line 49
def clear
  size.times { pop }
end
default_max_size() click to toggle source

Max number of commands to be displayed when no size has been specified.

Never more than Setting.

# File lib/byebug/history.rb, line 96
def default_max_size
  [Setting[:histsize], self.size].min
end
ignore?(buf) click to toggle source

Whether a specific command should not be stored in history.

For now, empty lines and consecutive duplicates.

# File lib/byebug/history.rb, line 114
def ignore?(buf)
  return true if /^\s*$/ =~ buf
  return false if Readline::HISTORY.length == 0

  Readline::HISTORY[Readline::HISTORY.length - 1] == buf
end
last_ids(n) click to toggle source

Array of ids of the last n commands.

# File lib/byebug/history.rb, line 87
def last_ids(n)
  (1 + size - n..size).to_a
end
pop() click to toggle source

Removes a command from Readline's history.

# File lib/byebug/history.rb, line 66
def pop
  self.size -= 1
  Readline::HISTORY.pop
end
push(cmd) click to toggle source

Adds a new command to Readline's history.

# File lib/byebug/history.rb, line 56
def push(cmd)
  return if ignore?(cmd)

  self.size += 1
  Readline::HISTORY.push(cmd)
end
restore() click to toggle source

Restores history from disk.

# File lib/byebug/history.rb, line 27
def restore
  return unless File.exist?(Setting[:histfile])

  File.readlines(Setting[:histfile]).reverse_each { |l| push(l.chomp) }
end
save() click to toggle source

Saves history to disk.

# File lib/byebug/history.rb, line 36
def save
  n_cmds = Setting[:histsize] > size ? size : Setting[:histsize]

  open(Setting[:histfile], 'w') do |file|
    n_cmds.times { file.puts(pop) }
  end

  clear
end
specific_max_size(number) click to toggle source

Max number of commands to be displayed when a size has been specified.

The only bound here is not showing more items than available.

# File lib/byebug/history.rb, line 105
def specific_max_size(number)
  [self.size, number].min
end
to_s(n_cmds) click to toggle source

Prints the requested numbers of history entries.

# File lib/byebug/history.rb, line 74
def to_s(n_cmds)
  show_size = n_cmds ? specific_max_size(n_cmds) : default_max_size

  commands = Readline::HISTORY.to_a.last(show_size)

  last_ids(show_size).zip(commands).map do |l|
    format('%5d  %s', l[0], l[1])
  end.join("\n") + "\n"
end