Module: Legion::CLI::Chat::Checkpoint

Defined in:
lib/legion/cli/chat/checkpoint.rb

Defined Under Namespace

Classes: Entry

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.entriesObject (readonly)

Returns the value of attribute entries.



20
21
22
# File 'lib/legion/cli/chat/checkpoint.rb', line 20

def entries
  @entries
end

.max_depthObject

Returns the value of attribute max_depth.



19
20
21
# File 'lib/legion/cli/chat/checkpoint.rb', line 19

def max_depth
  @max_depth
end

.modeObject

Returns the value of attribute mode.



19
20
21
# File 'lib/legion/cli/chat/checkpoint.rb', line 19

def mode
  @mode
end

Class Method Details

.clearObject



76
77
78
79
# File 'lib/legion/cli/chat/checkpoint.rb', line 76

def clear
  cleanup_storage
  @entries.clear
end

.configure(max_depth: 10, mode: :per_edit) ⇒ Object



22
23
24
25
26
27
# File 'lib/legion/cli/chat/checkpoint.rb', line 22

def configure(max_depth: 10, mode: :per_edit)
  @max_depth = max_depth
  @mode = mode
  @entries = []
  @storage_dir = nil
end

.countObject



81
82
83
# File 'lib/legion/cli/chat/checkpoint.rb', line 81

def count
  @entries.length
end

.listObject



66
67
68
69
70
71
72
73
74
# File 'lib/legion/cli/chat/checkpoint.rb', line 66

def list
  @entries.map do |e|
    {
      path:      e.path,
      existed:   e.existed,
      timestamp: e.timestamp
    }
  end
end

.rewind(steps = 1) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/legion/cli/chat/checkpoint.rb', line 43

def rewind(steps = 1)
  return [] if @entries.empty?

  steps = [steps, @entries.length].min
  restored = []
  steps.times do
    entry = @entries.pop
    restore_entry(entry)
    restored << entry
  end
  restored
end

.rewind_file(path) ⇒ Object



56
57
58
59
60
61
62
63
64
# File 'lib/legion/cli/chat/checkpoint.rb', line 56

def rewind_file(path)
  expanded = File.expand_path(path)
  idx = @entries.rindex { |e| e.path == expanded }
  return nil unless idx

  entry = @entries.delete_at(idx)
  restore_entry(entry)
  entry
end

.save(path) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/legion/cli/chat/checkpoint.rb', line 29

def save(path)
  expanded = File.expand_path(path)
  entry = Entry.new(
    path:      expanded,
    content:   File.exist?(expanded) ? File.read(expanded, encoding: 'utf-8') : nil,
    existed:   File.exist?(expanded),
    timestamp: Time.now
  )
  @entries.push(entry)
  @entries.shift while @entries.length > @max_depth
  persist_entry(entry)
  entry
end