class Byebug::History - byebug-11.0.1 Documentation

# 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 22
    def initialize
      self.size = 0
    end

### Public Instance Methods

buffer() click to toggle source

Array holding the list of commands in history

    # File lib/byebug/history.rb, line 29
    def buffer
      Readline::HISTORY.to_a
    end

clear() click to toggle source

Discards history.

    # File lib/byebug/history.rb, line 58
    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 105
    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 123
    def ignore?(buf)
      return true if /^\s*$/ =~ buf
      return false if Readline::HISTORY.empty?

      buffer[Readline::HISTORY.length - 1] == buf
    end

last_ids(number) click to toggle source

Array of ids of the last `number` commands.

    # File lib/byebug/history.rb, line 96
    def last_ids(number)
      (1 + size - number..size).to_a
    end

pop() click to toggle source

Removes a command from Readline's history.

    # File lib/byebug/history.rb, line 75
    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 65
    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 36
    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 45
    def save
      n_cmds = Setting[:histsize] > size ? size : Setting[:histsize]

      File.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 114
    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 83
    def to_s(n_cmds)
      show_size = n_cmds ? specific_max_size(n_cmds) : default_max_size

      commands = buffer.last(show_size)

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