Class: Yatte::RecoveryController

Inherits:
Object
  • Object
show all
Defined in:
lib/yatte/recovery_controller.rb

Overview

Owns swap-file state: edit/time counters, periodic swap saves, crash recovery prompts, and cleanup of swap files on exit.

Instance Method Summary collapse

Constructor Details

#initialize(input:, status:) ⇒ RecoveryController

Returns a new instance of RecoveryController.



7
8
9
10
11
12
# File 'lib/yatte/recovery_controller.rb', line 7

def initialize(input:, status:)
  @input = input
  @status = status
  @edits_since_swap = 0
  @last_swap_time = monotonic_time
end

Instance Method Details

#cleanup(tabs) ⇒ Object



34
35
36
37
38
39
# File 'lib/yatte/recovery_controller.rb', line 34

def cleanup(tabs)
  tabs.each do |tab|
    next unless tab.filename
    Recovery.remove(tab.filename) unless tab.buffer.dirty?
  end
end

#maybe_save(filename, buffer) ⇒ Object



23
24
25
26
27
28
29
30
31
32
# File 'lib/yatte/recovery_controller.rb', line 23

def maybe_save(filename, buffer)
  return unless filename && buffer.dirty?

  time_elapsed = monotonic_time - @last_swap_time
  if @edits_since_swap >= Recovery::EDIT_THRESHOLD ||
      time_elapsed >= Recovery::TIME_THRESHOLD
    Recovery.save(filename, buffer.to_s)
    reset
  end
end

#prompt_recovery(tabs) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/yatte/recovery_controller.rb', line 41

def prompt_recovery(tabs)
  tabs.each do |tab|
    next unless tab.filename && Recovery.exists?(tab.filename)

    @status.call(
      "Recovery file found for #{File.basename(tab.filename)}. Recover? (y/n)"
    )

    loop do
      key = @input.read_keypress
      case key
      when "y"
        restore_into(tab)
        Recovery.remove(tab.filename)
        @status.call("Recovered from swap file.")
        break
      when "n"
        Recovery.remove(tab.filename)
        @status.call("Swap file discarded.")
        break
      end
    end
  end
end

#record_editObject



14
15
16
# File 'lib/yatte/recovery_controller.rb', line 14

def record_edit
  @edits_since_swap += 1
end

#resetObject



18
19
20
21
# File 'lib/yatte/recovery_controller.rb', line 18

def reset
  @edits_since_swap = 0
  @last_swap_time = monotonic_time
end