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.



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

def initialize
  self.size = 0
end

Instance Attribute Details

#sizeObject

Returns the value of attribute size.



18
19
20
# File 'lib/byebug/history.rb', line 18

def size
  @size
end

Instance Method Details

#clearObject

Discards history.



49
50
51
# File 'lib/byebug/history.rb', line 49

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.



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

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)


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

def ignore?(buf)
  return true if /^\s*$/ =~ buf
  return false if Readline::HISTORY.length == 0

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

#last_ids(n) ⇒ Object

Array of ids of the last n commands.



87
88
89
# File 'lib/byebug/history.rb', line 87

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

#popObject

Removes a command from Readline’s history.



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

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

#push(cmd) ⇒ Object

Adds a new command to Readline’s history.



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

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

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

#restoreObject

Restores history from disk.



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

def restore
  return unless File.exist?(Setting[:histfile])

  File.readlines(Setting[:histfile]).reverse_each { |l| push(l.chomp) }
end

#saveObject

Saves history to disk.



36
37
38
39
40
41
42
43
44
# File 'lib/byebug/history.rb', line 36

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.



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

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

#to_s(n_cmds) ⇒ Object

Prints the requested numbers of history entries.



74
75
76
77
78
79
80
81
82
# File 'lib/byebug/history.rb', line 74

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