Module: Rvim::UndoFile

Defined in:
lib/rvim/undo_file.rb

Constant Summary collapse

VERSION =
1

Class Method Summary collapse

Class Method Details

.cache_dirObject



9
10
11
12
13
# File 'lib/rvim/undo_file.rb', line 9

def self.cache_dir
  base = ENV['XDG_CACHE_HOME']
  base = File.expand_path('~/.cache') if base.nil? || base.empty?
  File.join(base, 'rvim', 'undo')
end

.path_for(filepath) ⇒ Object



15
16
17
18
# File 'lib/rvim/undo_file.rb', line 15

def self.path_for(filepath)
  encoded = File.expand_path(filepath.to_s).gsub(File::SEPARATOR, '%')
  File.join(cache_dir, encoded)
end

.read(filepath) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
# File 'lib/rvim/undo_file.rb', line 30

def self.read(filepath)
  target = path_for(filepath)
  return nil unless File.exist?(target)

  data = Marshal.load(File.binread(target))
  return nil unless data.is_a?(Hash) && data[:version] == VERSION

  [data[:history], data[:index]]
rescue => _e
  nil
end

.write(filepath, history, index) ⇒ Object



20
21
22
23
24
25
26
27
28
# File 'lib/rvim/undo_file.rb', line 20

def self.write(filepath, history, index)
  target = path_for(filepath)
  FileUtils.mkdir_p(File.dirname(target))
  payload = { version: VERSION, history: history, index: index }
  File.binwrite(target, Marshal.dump(payload))
  target
rescue => _e
  nil
end