Skip to content
Kward Search API index

Class: Kward::PromptInterface::EditorFileMarker

Inherits:
Object
  • Object
show all
Defined in:
lib/kward/prompt_interface/editor/file_marker.rb

Overview

Tracks the on-disk identity and original content for an editor buffer.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path:, content:, new_file: false) ⇒ EditorFileMarker

Returns a new instance of EditorFileMarker.



11
12
13
14
15
16
# File 'lib/kward/prompt_interface/editor/file_marker.rb', line 11

def initialize(path:, content:, new_file: false)
  @path = path.to_s
  @content = content.to_s
  @digest = Digest::SHA256.hexdigest(@content)
  refresh unless new_file
end

Instance Attribute Details

#contentObject (readonly)

Returns the value of attribute content.



9
10
11
# File 'lib/kward/prompt_interface/editor/file_marker.rb', line 9

def content
  @content
end

#digestObject (readonly)

Returns the value of attribute digest.



9
10
11
# File 'lib/kward/prompt_interface/editor/file_marker.rb', line 9

def digest
  @digest
end

#mtimeObject (readonly)

Returns the value of attribute mtime.



9
10
11
# File 'lib/kward/prompt_interface/editor/file_marker.rb', line 9

def mtime
  @mtime
end

#sizeObject (readonly)

Returns the value of attribute size.



9
10
11
# File 'lib/kward/prompt_interface/editor/file_marker.rb', line 9

def size
  @size
end

Instance Method Details

#changed_on_disk?(new_file: false) ⇒ Boolean

Returns:

  • (Boolean)


29
30
31
32
33
34
35
36
37
# File 'lib/kward/prompt_interface/editor/file_marker.rb', line 29

def changed_on_disk?(new_file: false)
  return false if new_file && !File.exist?(@path)
  return true if new_file && File.exist?(@path)
  return true unless File.exist?(@path)

  File.read(@path) != @content
rescue StandardError
  true
end

#refresh(content = @content) ⇒ Object



18
19
20
21
22
23
24
25
26
27
# File 'lib/kward/prompt_interface/editor/file_marker.rb', line 18

def refresh(content = @content)
  @content = content.to_s
  @digest = Digest::SHA256.hexdigest(@content)
  stat = File.stat(@path)
  @mtime = stat.mtime
  @size = stat.size
rescue StandardError
  @mtime = nil
  @size = nil
end