class Byebug::History


Handles byebug’s history of commands.

def clear


Discards history.
def clear
  size.times { pop }
end

def default_max_size


Never more than Setting[:histsize].

Max number of commands to be displayed when no size has been specified.
def default_max_size
  [Setting[:histsize], self.size].min
end

def ignore?(buf)


For now, empty lines and consecutive duplicates.

Whether a specific command should not be stored in history.
def ignore?(buf)
  return true if /^\s*$/ =~ buf
  return false if Readline::HISTORY.length == 0
  Readline::HISTORY[Readline::HISTORY.length - 1] == buf
end

def initialize

def initialize
  self.size = 0
end

def last_ids(n)


Array of ids of the last n commands.
def last_ids(n)
  from, to = 1 + self.size - n, self.size
  (from..to).to_a
end

def pop


Removes a command from Readline's history.
def pop
  self.size -= 1
  Readline::HISTORY.pop
end

def push(cmd)


Adds a new command to Readline's history.
def push(cmd)
  return if ignore?(cmd)
  self.size += 1
  Readline::HISTORY.push(cmd)
end

def restore


Restores history from disk.
def restore
  return unless File.exist?(Setting[:histfile])
  File.readlines(Setting[:histfile]).reverse.each { |l| push(l.chomp) }
end

def save


Saves history to disk.
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

def specific_max_size(number)


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

Max number of commands to be displayed when a size has been specified.
def specific_max_size(number)
  [self.size, number].min
end

def to_s(n_cmds)


Prints the requested numbers of history entries.
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