tp_tree 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,87 @@
1
+ # frozen_string_literal: true
2
+
3
+ module TPTree
4
+ module Formatters
5
+ # BaseFormatter provides common formatting methods for parameters, values, and timing.
6
+ # Subclasses must implement the colorize method for their specific output format.
7
+ class BaseFormatter
8
+ # Color codes for different depth levels in the call tree
9
+ DEPTH_COLORS = [:green, :blue, :yellow, :magenta, :cyan, :red].freeze
10
+
11
+ # Abstract method - must be implemented by subclasses
12
+ def colorize(text, color)
13
+ raise NotImplementedError, "Subclasses must implement colorize method"
14
+ end
15
+
16
+ def format_timing(duration)
17
+ return '' if duration.nil?
18
+
19
+ formatted_time = if duration < 0.001
20
+ "#{(duration * 1_000_000).round(1)}μs"
21
+ elsif duration < 1.0
22
+ "#{(duration * 1000).round(1)}ms"
23
+ else
24
+ "#{duration.round(3)}s"
25
+ end
26
+
27
+ colorize(" [#{formatted_time}]", :cyan)
28
+ end
29
+
30
+ def format_parameters(parameters)
31
+ return '' if parameters.nil? || parameters.empty?
32
+
33
+ parameters.map { |param_type, param_name, param_value|
34
+ case param_type
35
+ when :req, :opt
36
+ "#{param_name} = #{format_value(param_value)}"
37
+ when :keyreq, :key
38
+ if param_value.nil?
39
+ "#{param_name}:"
40
+ else
41
+ "#{param_name} = #{format_value(param_value)}"
42
+ end
43
+ when :rest
44
+ "*#{param_name}"
45
+ when :keyrest
46
+ "**#{param_name}"
47
+ when :block
48
+ "&#{param_name}"
49
+ else
50
+ param_name.to_s
51
+ end
52
+ }.join(', ')
53
+ end
54
+
55
+ def format_value(value)
56
+ case value
57
+ when String
58
+ value.inspect
59
+ when Symbol
60
+ ":#{value}"
61
+ when NilClass
62
+ 'nil'
63
+ when TrueClass, FalseClass
64
+ value.to_s
65
+ when Numeric
66
+ value.to_s
67
+ when Array
68
+ "[#{value.map { |v| format_value(v) }.join(', ')}]"
69
+ when Hash
70
+ "{#{value.map { |k, v| "#{format_value(k)} => #{format_value(v)}" }.join(', ')}}"
71
+ when Proc
72
+ 'Proc'
73
+ else
74
+ value.inspect
75
+ end
76
+ end
77
+
78
+ def format_return_value(return_value)
79
+ format_value(return_value)
80
+ end
81
+
82
+ def color_for_depth(depth)
83
+ DEPTH_COLORS[depth % DEPTH_COLORS.length]
84
+ end
85
+ end
86
+ end
87
+ end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'base_formatter'
4
+
5
+ module TPTree
6
+ module Formatters
7
+ # XmlFormatter provides XML tag formatting for structured output
8
+ class XmlFormatter < BaseFormatter
9
+ def colorize(text, color)
10
+ "<#{color}>#{text}</#{color}>"
11
+ end
12
+ end
13
+ end
14
+ end