Class: Byebug::History
- Inherits:
-
Object
- Object
- Byebug::History
- Defined in:
- lib/byebug/history.rb
Overview
Handles byebug’s history of commands.
Instance Attribute Summary collapse
-
#size ⇒ Object
readonly
Returns the value of attribute size.
Instance Method Summary collapse
-
#buffer ⇒ Object
Array holding the list of commands in history.
-
#clear ⇒ Object
Discards history.
-
#default_max_size ⇒ Object
Max number of commands to be displayed when no size has been specified.
-
#ignore?(buf) ⇒ Boolean
Whether a specific command should not be stored in history.
-
#initialize ⇒ History
constructor
A new instance of History.
-
#last_ids(number) ⇒ Object
Array of ids of the last
numbercommands. -
#pop ⇒ Object
Removes a command from Reline’s history.
-
#push(cmd) ⇒ Object
Adds a new command to Reline’s history.
-
#restore ⇒ Object
Restores history from disk.
-
#save ⇒ Object
Saves history to disk.
-
#specific_max_size(number) ⇒ Object
Max number of commands to be displayed when a size has been specified.
-
#to_s(n_cmds) ⇒ Object
Prints the requested numbers of history entries.
Constructor Details
#initialize ⇒ History
Returns a new instance of History.
12 13 14 |
# File 'lib/byebug/history.rb', line 12 def initialize @size = 0 end |
Instance Attribute Details
#size ⇒ Object (readonly)
Returns the value of attribute size.
10 11 12 |
# File 'lib/byebug/history.rb', line 10 def size @size end |
Instance Method Details
#buffer ⇒ Object
Array holding the list of commands in history
19 20 21 |
# File 'lib/byebug/history.rb', line 19 def buffer Reline::HISTORY.to_a end |
#clear ⇒ Object
Discards history.
48 49 50 |
# File 'lib/byebug/history.rb', line 48 def clear size.times { pop } end |
#default_max_size ⇒ Object
Max number of commands to be displayed when no size has been specified.
Never more than Setting.
95 96 97 |
# File 'lib/byebug/history.rb', line 95 def default_max_size [Setting[:histsize], size].min end |
#ignore?(buf) ⇒ Boolean
Whether a specific command should not be stored in history.
For now, empty lines and consecutive duplicates.
113 114 115 116 117 118 |
# File 'lib/byebug/history.rb', line 113 def ignore?(buf) return true if /^\s*$/.match?(buf) return false if Reline::HISTORY.empty? buffer[Reline::HISTORY.length - 1] == buf end |
#last_ids(number) ⇒ Object
Array of ids of the last number commands.
86 87 88 |
# File 'lib/byebug/history.rb', line 86 def last_ids(number) (1 + size - number..size).to_a end |
#pop ⇒ Object
Removes a command from Reline’s history.
65 66 67 68 |
# File 'lib/byebug/history.rb', line 65 def pop @size -= 1 Reline::HISTORY.pop end |
#push(cmd) ⇒ Object
Adds a new command to Reline’s history.
55 56 57 58 59 60 |
# File 'lib/byebug/history.rb', line 55 def push(cmd) return if ignore?(cmd) @size += 1 Reline::HISTORY.push(cmd) end |
#restore ⇒ Object
Restores history from disk.
26 27 28 29 30 |
# File 'lib/byebug/history.rb', line 26 def restore return unless File.exist?(Setting[:histfile]) File.readlines(Setting[:histfile]).reverse_each { |l| push(l.chomp) } end |
#save ⇒ Object
Saves history to disk.
35 36 37 38 39 40 41 42 43 |
# File 'lib/byebug/history.rb', line 35 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.
104 105 106 |
# File 'lib/byebug/history.rb', line 104 def specific_max_size(number) [size, number].min end |
#to_s(n_cmds) ⇒ Object
Prints the requested numbers of history entries.
73 74 75 76 77 78 79 80 81 |
# File 'lib/byebug/history.rb', line 73 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 |