Class: PrettyIRB::HistoryManager
- Inherits:
-
Object
- Object
- PrettyIRB::HistoryManager
- Defined in:
- lib/pretty_irb/history_manager.rb
Instance Attribute Summary collapse
-
#entries ⇒ Object
readonly
Returns the value of attribute entries.
Instance Method Summary collapse
-
#add(code, result = nil, error = nil) ⇒ Object
Add entry to history.
-
#all ⇒ Object
Get all history.
-
#clear ⇒ Object
Clear history.
-
#export(filename) ⇒ Object
Export history to file.
-
#get(line_number) ⇒ Object
Get entry by line number.
-
#initialize ⇒ HistoryManager
constructor
A new instance of HistoryManager.
-
#last(n = 10) ⇒ Object
Get last N entries.
-
#search(keyword) ⇒ Object
Search history by keyword.
Constructor Details
#initialize ⇒ HistoryManager
Returns a new instance of HistoryManager.
7 8 9 |
# File 'lib/pretty_irb/history_manager.rb', line 7 def initialize @entries = [] end |
Instance Attribute Details
#entries ⇒ Object (readonly)
Returns the value of attribute entries.
5 6 7 |
# File 'lib/pretty_irb/history_manager.rb', line 5 def entries @entries end |
Instance Method Details
#add(code, result = nil, error = nil) ⇒ Object
Add entry to history
12 13 14 15 16 17 18 19 20 |
# File 'lib/pretty_irb/history_manager.rb', line 12 def add(code, result = nil, error = nil) @entries << { code: code, result: result, error: error, timestamp: Time.now, line_number: @entries.length + 1 } end |
#all ⇒ Object
Get all history
34 35 36 |
# File 'lib/pretty_irb/history_manager.rb', line 34 def all format_history(@entries) end |
#clear ⇒ Object
Clear history
46 47 48 49 |
# File 'lib/pretty_irb/history_manager.rb', line 46 def clear @entries = [] "✓ History cleared" end |
#export(filename) ⇒ Object
Export history to file
52 53 54 55 |
# File 'lib/pretty_irb/history_manager.rb', line 52 def export(filename) File.write(filename, format_for_export) "✓ History exported to #{filename}" end |
#get(line_number) ⇒ Object
Get entry by line number
39 40 41 42 43 |
# File 'lib/pretty_irb/history_manager.rb', line 39 def get(line_number) entry = @entries[line_number - 1] return "" unless entry "#{entry[:line_number]}: #{entry[:code]}\n=> #{entry[:result]}" end |
#last(n = 10) ⇒ Object
Get last N entries
29 30 31 |
# File 'lib/pretty_irb/history_manager.rb', line 29 def last(n = 10) format_history(@entries.last(n)) end |
#search(keyword) ⇒ Object
Search history by keyword
23 24 25 26 |
# File 'lib/pretty_irb/history_manager.rb', line 23 def search(keyword) results = @entries.select { |entry| entry[:code].include?(keyword) } format_search_results(results) end |