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.


10
11
12
# File 'lib/byebug/history.rb', line 10

def initialize
  self.size = 0
end

Instance Attribute Details

#sizeObject

Returns the value of attribute size


8
9
10
# File 'lib/byebug/history.rb', line 8

def size
  @size
end

Instance Method Details

#clearObject

Discards history.


39
40
41
# File 'lib/byebug/history.rb', line 39

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.


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

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)

106
107
108
109
110
111
# File 'lib/byebug/history.rb', line 106

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.


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

def last_ids(n)
  from, to = 1 + self.size - n, self.size

  (from..to).to_a
end

#popObject

Removes a command from Readline's history.


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

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

#push(cmd) ⇒ Object

Adds a new command to Readline's history.


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

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

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

#restoreObject

Restores history from disk.


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

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

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

#saveObject

Saves history to disk.


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

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.


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

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

#to_s(n_cmds) ⇒ Object

Prints the requested numbers of history entries.


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

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