Module: Agent::SessionContext::Renderers::HumanDisplay
- Defined in:
- lib/agent/session_context/renderers/human_display.rb
Class Method Summary collapse
- .attribute_value(value, inline_formatter) ⇒ Object
- .attributes(hash, inline_formatter:) ⇒ Object
- .code_span_content(literal) ⇒ Object
- .escape_markdown(value) ⇒ Object
- .longest_backtick_run(value) ⇒ Object
- .markdown_block(value) ⇒ Object
- .markdown_literal(value) ⇒ Object
- .markdown_text(value) ⇒ Object
- .refs(source_refs) ⇒ Object
- .sanitize_string(value, preserve_newlines:) ⇒ Object
- .text_block(value) ⇒ Object
- .text_inline(value) ⇒ Object
- .timestamp(value) ⇒ Object
- .visible_control_escape(codepoint) ⇒ Object
Class Method Details
.attribute_value(value, inline_formatter) ⇒ Object
54 55 56 57 58 59 |
# File 'lib/agent/session_context/renderers/human_display.rb', line 54 def attribute_value(value, inline_formatter) return "{#{attributes(value, inline_formatter:)}}" if value.is_a?(Hash) return value.map { |entry| attribute_value(entry, inline_formatter) }.join(",") if value.is_a?(Array) inline_formatter.call(Serializer.serialize(value).to_s) end |
.attributes(hash, inline_formatter:) ⇒ Object
48 49 50 51 52 |
# File 'lib/agent/session_context/renderers/human_display.rb', line 48 def attributes(hash, inline_formatter:) Serializer.normalized_hash_entries(hash).map do |key, value| "#{inline_formatter.call(key)}=#{attribute_value(value, inline_formatter)}" end.join(", ") end |
.code_span_content(literal) ⇒ Object
85 86 87 88 89 90 |
# File 'lib/agent/session_context/renderers/human_display.rb', line 85 def code_span_content(literal) return " #{literal} " if literal.start_with?("`") || literal.end_with?("`") return " #{literal} " if literal.start_with?(" ") && literal.end_with?(" ") literal end |
.escape_markdown(value) ⇒ Object
78 79 80 81 82 83 |
# File 'lib/agent/session_context/renderers/human_display.rb', line 78 def escape_markdown(value) escaped = value.gsub("&", "&").gsub("<", "<").gsub(">", ">") escaped = escaped.gsub(/([\\`*\[\]#])/, "\\\\\\1") escaped = escaped.gsub(/(^|[^[:alnum:]])_([^_]+)_([^[:alnum:]]|$)/, '\1\\_\2\\_\3') escaped.gsub("-", "\\-") end |
.longest_backtick_run(value) ⇒ Object
92 93 94 |
# File 'lib/agent/session_context/renderers/human_display.rb', line 92 def longest_backtick_run(value) value.scan(/`+/).map(&:length).max || 0 end |
.markdown_block(value) ⇒ Object
30 31 32 33 34 35 |
# File 'lib/agent/session_context/renderers/human_display.rb', line 30 def markdown_block(value) content = sanitize_string(value, preserve_newlines: true) fence = "`" * [longest_backtick_run(content) + 1, 3].max ["#{fence}text", content, fence].join("\n") end |
.markdown_literal(value) ⇒ Object
21 22 23 24 25 26 27 28 |
# File 'lib/agent/session_context/renderers/human_display.rb', line 21 def markdown_literal(value) literal = text_inline(value) return "<code></code>" if literal.empty? return "<code>#{literal}</code>" if literal.match?(/\A +\z/) fence = "`" * [longest_backtick_run(literal) + 1, 1].max "#{fence}#{code_span_content(literal)}#{fence}" end |
.markdown_text(value) ⇒ Object
17 18 19 |
# File 'lib/agent/session_context/renderers/human_display.rb', line 17 def markdown_text(value) escape_markdown(text_inline(value)) end |
.refs(source_refs) ⇒ Object
44 45 46 |
# File 'lib/agent/session_context/renderers/human_display.rb', line 44 def refs(source_refs) Array(source_refs).map { |ref| "#{ref.}:#{ref.part_index}" }.join(", ") end |
.sanitize_string(value, preserve_newlines:) ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/agent/session_context/renderers/human_display.rb', line 61 def sanitize_string(value, preserve_newlines:) scrubbed = Serializer.scrub_string(value.to_s) buffer = String.new(encoding: Encoding::UTF_8) scrubbed.each_codepoint do |codepoint| if preserve_newlines && codepoint == 0x0A buffer << "\n" next end visible = visible_control_escape(codepoint) buffer << (visible || codepoint) end buffer end |
.text_block(value) ⇒ Object
13 14 15 |
# File 'lib/agent/session_context/renderers/human_display.rb', line 13 def text_block(value) sanitize_string(value, preserve_newlines: true).split("\n", -1).map { |line| "| #{line}" }.join("\n") end |
.text_inline(value) ⇒ Object
9 10 11 |
# File 'lib/agent/session_context/renderers/human_display.rb', line 9 def text_inline(value) sanitize_string(value, preserve_newlines: false) end |
.timestamp(value) ⇒ Object
37 38 39 40 41 42 |
# File 'lib/agent/session_context/renderers/human_display.rb', line 37 def (value) serialized = Serializer.(value) return if serialized.nil? serialized.to_s end |
.visible_control_escape(codepoint) ⇒ Object
96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/agent/session_context/renderers/human_display.rb', line 96 def visible_control_escape(codepoint) case codepoint when 0x09 "\\t" when 0x0A "\\n" when 0x0D "\\r" when 0x1B "\\e" when 0x00..0x08, 0x0B..0x0C, 0x0E..0x1A, 0x1C..0x1F, 0x7F, 0x80..0x9F format("\\u%04X", codepoint) end end |