Skip to content
Kward Search API index

Class: Kward::PromptInterface::EditorRunnerState

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(language:, source:) ⇒ EditorRunnerState

Returns a new instance of EditorRunnerState.



10
11
12
13
14
15
16
17
18
19
# File 'lib/kward/prompt_interface/editor/runner_state.rb', line 10

def initialize(language:, source:)
  @language = language.to_sym
  @source_digest = Digest::SHA256.hexdigest(source.to_s)
  @mutex = Mutex.new
  @running = true
  @cancel_requested = false
  @result = nil
  @error = nil
  @scroll_row = 0
end

Instance Attribute Details

#languageObject (readonly)

Returns the value of attribute language.



8
9
10
# File 'lib/kward/prompt_interface/editor/runner_state.rb', line 8

def language
  @language
end

#source_digestObject (readonly)

Returns the value of attribute source_digest.



8
9
10
# File 'lib/kward/prompt_interface/editor/runner_state.rb', line 8

def source_digest
  @source_digest
end

Instance Method Details

#cancelObject



29
30
31
# File 'lib/kward/prompt_interface/editor/runner_state.rb', line 29

def cancel
  @mutex.synchronize { @cancel_requested = true }
end

#cancel_requested?Boolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/kward/prompt_interface/editor/runner_state.rb', line 25

def cancel_requested?
  @mutex.synchronize { @cancel_requested }
end

#complete(result) ⇒ Object



33
34
35
36
37
38
# File 'lib/kward/prompt_interface/editor/runner_state.rb', line 33

def complete(result)
  @mutex.synchronize do
    @result = result
    @running = false
  end
end

#errorObject



51
52
53
# File 'lib/kward/prompt_interface/editor/runner_state.rb', line 51

def error
  @mutex.synchronize { @error }
end

#fail(error) ⇒ Object



40
41
42
43
44
45
# File 'lib/kward/prompt_interface/editor/runner_state.rb', line 40

def fail(error)
  @mutex.synchronize do
    @error = error
    @running = false
  end
end

#output_textObject



55
56
57
58
59
60
61
# File 'lib/kward/prompt_interface/editor/runner_state.rb', line 55

def output_text
  @mutex.synchronize do
    return @error.message.to_s if @error

    @result&.output.to_s
  end
end

#resultObject



47
48
49
# File 'lib/kward/prompt_interface/editor/runner_state.rb', line 47

def result
  @mutex.synchronize { @result }
end

#running?Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/kward/prompt_interface/editor/runner_state.rb', line 21

def running?
  @mutex.synchronize { @running }
end

#scroll_by(delta, maximum:) ⇒ Object



73
74
75
# File 'lib/kward/prompt_interface/editor/runner_state.rb', line 73

def scroll_by(delta, maximum:)
  set_scroll_row(scroll_row + delta.to_i, maximum: maximum)
end

#scroll_rowObject



63
64
65
# File 'lib/kward/prompt_interface/editor/runner_state.rb', line 63

def scroll_row
  @mutex.synchronize { @scroll_row }
end

#set_scroll_row(row, maximum:) ⇒ Object



67
68
69
70
71
# File 'lib/kward/prompt_interface/editor/runner_state.rb', line 67

def set_scroll_row(row, maximum:)
  @mutex.synchronize do
    @scroll_row = [[row.to_i, 0].max, maximum.to_i].min
  end
end