Class: Kward::NativeShell::History
- Inherits:
-
Object
- Object
- Kward::NativeShell::History
- Defined in:
- lib/kward/shell/native_history.rb
Overview
Standalone command history. No implicit disk storage; unsafe/unavailable HISTFILE paths fall back to memory without altering their permissions.
Instance Attribute Summary collapse
-
#entries ⇒ Object
readonly
Returns the value of attribute entries.
-
#next_number ⇒ Object
readonly
Returns the value of attribute next_number.
Instance Method Summary collapse
- #add(text) ⇒ Object
- #commit_pending ⇒ Object
- #discard_pending ⇒ Object
-
#initialize(variables, directory:) ⇒ History
constructor
A new instance of History.
- #paused=(paused) ⇒ Object
- #paused? ⇒ Boolean
- #refresh ⇒ Object
- #select(first, last, listing:) ⇒ Object
- #with_entry(text) ⇒ Object
Constructor Details
#initialize(variables, directory:) ⇒ History
Returns a new instance of History.
12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/kward/shell/native_history.rb', line 12 def initialize(variables, directory:) size = variables["HISTSIZE"] @size = size&.match?(/\A[0-9]+\z/) ? size.to_i : 512 @entries = [] @next_number = 1 @owner = Process.pid path = variables["HISTFILE"] @path = directory.resolve(path) if path && !path.empty? refresh rescue SystemCallError, IOError, ArgumentError @path = nil end |
Instance Attribute Details
#entries ⇒ Object (readonly)
Returns the value of attribute entries.
10 11 12 |
# File 'lib/kward/shell/native_history.rb', line 10 def entries @entries end |
#next_number ⇒ Object (readonly)
Returns the value of attribute next_number.
10 11 12 |
# File 'lib/kward/shell/native_history.rb', line 10 def next_number @next_number end |
Instance Method Details
#add(text) ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/kward/shell/native_history.rb', line 52 def add(text) return if paused? || text.strip.empty? text = text.chomp saved = with_lock do entries = read_entries number = [@next_number, entries.last&.first.to_i + 1].max entries = (entries << [number, text]).last(@size) persist(entries) update_entries(entries, number + 1) end update_entries((@entries + [[@next_number, text]]).last(@size), @next_number + 1) unless saved end |
#commit_pending ⇒ Object
43 44 45 46 |
# File 'lib/kward/shell/native_history.rb', line 43 def commit_pending pending, @pending = @pending, nil add(pending) if pending end |
#discard_pending ⇒ Object
48 49 50 |
# File 'lib/kward/shell/native_history.rb', line 48 def discard_pending @pending = nil end |
#paused=(paused) ⇒ Object
29 30 31 32 |
# File 'lib/kward/shell/native_history.rb', line 29 def paused=(paused) discard_pending if paused @paused = paused end |
#paused? ⇒ Boolean
25 26 27 |
# File 'lib/kward/shell/native_history.rb', line 25 def paused? @paused == true end |
#refresh ⇒ Object
65 66 67 68 69 70 |
# File 'lib/kward/shell/native_history.rb', line 65 def refresh with_lock do entries = read_entries update_entries(entries.last(@size), entries.last&.first.to_i + 1) end end |
#select(first, last, listing:) ⇒ Object
72 73 74 75 76 77 78 79 80 |
# File 'lib/kward/shell/native_history.rb', line 72 def select(first, last, listing:) raise ExecutionError, "fc: history is empty" if @entries.empty? first ||= listing ? "-16" : "-1" last ||= listing ? "-1" : first from = index(first, range: listing || first != last) to = index(last, range: listing || first != last) selected = @entries[[from, to].min..[from, to].max] from > to ? selected.reverse : selected end |
#with_entry(text) ⇒ Object
34 35 36 37 38 39 40 41 |
# File 'lib/kward/shell/native_history.rb', line 34 def with_entry(text) return yield if paused? @pending = text unless text.strip.empty? yield ensure commit_pending end |