Class: Aiko::UI

Inherits:
Object
  • Object
show all
Defined in:
lib/aiko/ui.rb

Constant Summary collapse

TOOL_ARGS_WIDTH =
120
RESULT_PREVIEW_LINES =
5

Instance Method Summary collapse

Constructor Details

#initialize(input: $stdin, output: $stdout, error: $stderr, auto_approve: false) ⇒ UI

Returns a new instance of UI.



8
9
10
11
12
13
# File 'lib/aiko/ui.rb', line 8

def initialize(input: $stdin, output: $stdout, error: $stderr, auto_approve: false)
  @input = input
  @output = output
  @error = error
  @auto_approve = auto_approve
end

Instance Method Details

#confirm?(message) ⇒ Boolean

Returns:

  • (Boolean)


45
46
47
48
49
50
51
52
53
# File 'lib/aiko/ui.rb', line 45

def confirm?(message)
  return true if @auto_approve
  return false unless @input.tty?

  @output.puts(message)
  @output.print("実行しますか? [y/N] ")
  answer = @input.gets
  !answer.nil? && %w[y Y].include?(answer.strip)
end

#on_assistant_text(text) ⇒ Object



15
16
17
# File 'lib/aiko/ui.rb', line 15

def on_assistant_text(text)
  @output.puts(text)
end

#on_request_endObject



26
27
28
29
30
31
# File 'lib/aiko/ui.rb', line 26

def on_request_end
  return unless @output.tty?

  @output.print("\r\e[K")
  @output.flush
end

#on_request_startObject



19
20
21
22
23
24
# File 'lib/aiko/ui.rb', line 19

def on_request_start
  return unless @output.tty?

  @output.print(dim("考え中…"))
  @output.flush
end

#on_tool_result(_name, result) ⇒ Object



40
41
42
43
# File 'lib/aiko/ui.rb', line 40

def on_tool_result(_name, result)
  lines = result.lines.first(RESULT_PREVIEW_LINES)
  lines.each { |line| @output.puts(dim("  #{line.chomp}")) }
end

#on_tool_start(name, arguments) ⇒ Object



33
34
35
36
37
38
# File 'lib/aiko/ui.rb', line 33

def on_tool_start(name, arguments)
  args = arguments.map { |k, v| "#{k}: #{v.inspect}" }.join(", ")
  line = "#{bold(name)}(#{args})"
  line = "#{line[0, TOOL_ARGS_WIDTH]}" if line.length > TOOL_ARGS_WIDTH
  @output.puts(line)
end

#puts(text = "") ⇒ Object



59
60
61
# File 'lib/aiko/ui.rb', line 59

def puts(text = "")
  @output.puts(text)
end

#show_error(message) ⇒ Object



55
56
57
# File 'lib/aiko/ui.rb', line 55

def show_error(message)
  @error.puts("Error: #{message}")
end