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
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(n) ⇒ Object
Array of ids of the last n commands.
-
#pop ⇒ Object
Removes a command from Readline’s history.
-
#push(cmd) ⇒ Object
Adds a new command to Readline’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.
20 21 22 |
# File 'lib/byebug/history.rb', line 20 def initialize self.size = 0 end |
Instance Attribute Details
#size ⇒ Object
Returns the value of attribute size.
18 19 20 |
# File 'lib/byebug/history.rb', line 18 def size @size end |
Instance Method Details
#buffer ⇒ Object
Array holding the list of commands in history
27 28 29 |
# File 'lib/byebug/history.rb', line 27 def buffer Readline::HISTORY.to_a end |
#clear ⇒ Object
Discards history.
56 57 58 |
# File 'lib/byebug/history.rb', line 56 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.
103 104 105 |
# File 'lib/byebug/history.rb', line 103 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.
121 122 123 124 125 126 |
# File 'lib/byebug/history.rb', line 121 def ignore?(buf) return true if /^\s*$/ =~ buf return false if Readline::HISTORY.empty? buffer[Readline::HISTORY.length - 1] == buf end |
#last_ids(n) ⇒ Object
Array of ids of the last n commands.
94 95 96 |
# File 'lib/byebug/history.rb', line 94 def last_ids(n) (1 + size - n..size).to_a end |
#pop ⇒ Object
Removes a command from Readline’s history.
73 74 75 76 |
# File 'lib/byebug/history.rb', line 73 def pop self.size -= 1 Readline::HISTORY.pop end |
#push(cmd) ⇒ Object
Adds a new command to Readline’s history.
63 64 65 66 67 68 |
# File 'lib/byebug/history.rb', line 63 def push(cmd) return if ignore?(cmd) self.size += 1 Readline::HISTORY.push(cmd) end |
#restore ⇒ Object
Restores history from disk.
34 35 36 37 38 |
# File 'lib/byebug/history.rb', line 34 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.
43 44 45 46 47 48 49 50 51 |
# File 'lib/byebug/history.rb', line 43 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) ⇒ 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.
112 113 114 |
# File 'lib/byebug/history.rb', line 112 def specific_max_size(number) [self.size, number].min end |
#to_s(n_cmds) ⇒ Object
Prints the requested numbers of history entries.
81 82 83 84 85 86 87 88 89 |
# File 'lib/byebug/history.rb', line 81 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('%5d %s', l[0], l[1]) end.join("\n") + "\n" end |