Class: TUI::Decorators::BaseDecorator

Inherits:
Object
  • Object
show all
Includes:
Formatting
Defined in:
lib/tui/decorators/base_decorator.rb

Constant Summary collapse

ICON =

wrench

"\u{1F527}"
CHECKMARK =
"\u2713"
RETURN_ARROW =
"\u21A9"
ERROR_ICON =
"\u274C"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Formatting

#format_ns_timestamp, #format_token_label, #preserve_indentation, #token_count_color

Constructor Details

#initialize(data) ⇒ BaseDecorator

Returns a new instance of BaseDecorator.



52
53
54
# File 'lib/tui/decorators/base_decorator.rb', line 52

def initialize(data)
  @data = data
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



50
51
52
# File 'lib/tui/decorators/base_decorator.rb', line 50

def data
  @data
end

Class Method Details

.for(data) ⇒ BaseDecorator

Factory returning the per-tool decorator for the given data hash.

Parameters:

  • data (Hash)

    structured event data with string keys (“role”, “tool”, “content”, etc.)

Returns:



61
62
63
64
# File 'lib/tui/decorators/base_decorator.rb', line 61

def self.for(data)
  tool = resolve_tool(data)
  decorator_for(tool).new(data)
end

Instance Method Details

#colorString

Unified color for all tool call headers. Keeps tool invocations visually distinct from conversation messages (user/assistant/thought).

Returns:

  • (String)


142
143
144
# File 'lib/tui/decorators/base_decorator.rb', line 142

def color
  Settings.theme_color_accent
end

#iconString

Icon for this tool type. Subclasses override with tool-specific icons.

Returns:

  • (String)


135
136
137
# File 'lib/tui/decorators/base_decorator.rb', line 135

def icon
  ICON
end

#render(tui) ⇒ Array<RatatuiRuby::Widgets::Line>

Renders the event, dispatching by role to the appropriate method.

Parameters:

  • tui (RatatuiRuby)

    TUI rendering API

Returns:

  • (Array<RatatuiRuby::Widgets::Line>)


70
71
72
73
74
75
76
# File 'lib/tui/decorators/base_decorator.rb', line 70

def render(tui)
  case data["role"].to_s
  when "tool_call" then render_call(tui)
  when "tool_response" then render_response(tui)
  when "think" then render_think(tui)
  end
end

#render_call(tui) ⇒ Array<RatatuiRuby::Widgets::Line>

Generic tool call rendering — icon, tool name, and indented input. Subclasses override for tool-specific presentation.

Parameters:

  • tui (RatatuiRuby)

    TUI rendering API

Returns:

  • (Array<RatatuiRuby::Widgets::Line>)


83
84
85
86
87
88
89
90
91
# File 'lib/tui/decorators/base_decorator.rb', line 83

def render_call(tui)
  style = tui.style(fg: color)
  header = build_call_header
  lines = [tui.line(spans: [tui.span(content: header, style: style)])]
  data["input"].to_s.split("\n", -1).each do |line|
    lines << tui.line(spans: [tui.span(content: preserve_indentation("  #{line}"), style: style)])
  end
  lines
end

#render_response(tui) ⇒ Array<RatatuiRuby::Widgets::Line>

Generic tool response rendering — success/failure indicator and content. Token counts get their own color-coded span so expensive responses visually jump out in debug mode. Subclasses override for tool-specific presentation.

Parameters:

  • tui (RatatuiRuby)

    TUI rendering API

Returns:

  • (Array<RatatuiRuby::Widgets::Line>)


100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/tui/decorators/base_decorator.rb', line 100

def render_response(tui)
  indicator = (data["success"] == false) ? ERROR_ICON : CHECKMARK
  tool_id = data["tool_use_id"]
  tokens = data["tokens"]
  style = tui.style(fg: response_color)

  meta_parts = []
  meta_parts << "[#{tool_id}]" if tool_id
  meta_parts << indicator
  prefix = "  #{RETURN_ARROW} #{meta_parts.join(" ")} "

  content_lines = data["content"].to_s.split("\n", -1)
  first_line_spans = [tui.span(content: prefix, style: style)]
  if tokens
    tok_label = format_token_label(tokens, data["estimated"])
    first_line_spans << tui.span(content: "#{tok_label} ", style: tui.style(fg: token_count_color(tokens)))
  end
  first_line_spans << tui.span(content: content_lines.first.to_s, style: style)

  lines = [tui.line(spans: first_line_spans)]
  content_lines.drop(1).each { |line| lines << tui.line(spans: [tui.span(content: preserve_indentation("    #{line}"), style: style)]) }
  lines
end

#render_think(tui) ⇒ Array<RatatuiRuby::Widgets::Line>

Think rendering — delegated to ThinkDecorator, but base provides a fallback that renders as a generic tool call.

Parameters:

  • tui (RatatuiRuby)

    TUI rendering API

Returns:

  • (Array<RatatuiRuby::Widgets::Line>)


129
130
131
# File 'lib/tui/decorators/base_decorator.rb', line 129

def render_think(tui)
  render_call(tui)
end

#response_colorString

Color for tool response content. Subclasses override for tool-specific colors.

Returns:

  • (String)


148
149
150
# File 'lib/tui/decorators/base_decorator.rb', line 148

def response_color
  Settings.theme_color_text
end