Class: Byebug::History

Inherits:
Object
  • Object
show all
Defined in:
lib/byebug/history.rb

Overview

Handles byebug’s history of commands.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeHistory

Returns a new instance of History.



22
23
24
# File 'lib/byebug/history.rb', line 22

def initialize
  self.size = 0
end

Instance Attribute Details

#sizeObject

Returns the value of attribute size.



20
21
22
# File 'lib/byebug/history.rb', line 20

def size
  @size
end

Instance Method Details

#bufferObject

Array holding the list of commands in history



29
30
31
# File 'lib/byebug/history.rb', line 29

def buffer
  Readline::HISTORY.to_a
end

#clearObject

Discards history.



58
59
60
# File 'lib/byebug/history.rb', line 58

def clear
  size.times { pop }
end

#default_max_sizeObject

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

Never more than Setting.



105
106
107
# File 'lib/byebug/history.rb', line 105

def default_max_size
  [Setting[:histsize], self.size].min
end

#ignore?(buf) ⇒ Boolean

Whether a specific command should not be stored in history.

For now, empty lines and consecutive duplicates.

Returns:

  • (Boolean)


123
124
125
126
127
128
# 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) ⇒ Object

Array of ids of the last number commands.



96
97
98
# File 'lib/byebug/history.rb', line 96

def last_ids(number)
  (1 + size - number..size).to_a
end

#popObject

Removes a command from Readline’s history.



75
76
77
78
# File 'lib/byebug/history.rb', line 75

def pop
  self.size -= 1
  Readline::HISTORY.pop
end

#push(cmd) ⇒ Object

Adds a new command to Readline’s history.



65
66
67
68
69
70
# File 'lib/byebug/history.rb', line 65

def push(cmd)
  return if ignore?(cmd)

  self.size += 1
  Readline::HISTORY.push(cmd)
end

#restoreObject

Restores history from disk.



36
37
38
39
40
# 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

#saveObject

Saves history to disk.



45
46
47
48
49
50
51
52
53
# 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) ⇒ Object

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

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



114
115
116
# File 'lib/byebug/history.rb', line 114

def specific_max_size(number)
  [self.size, number].min
end

#to_s(n_cmds) ⇒ Object

Prints the requested numbers of history entries.



83
84
85
86
87
88
89
90
91
# 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