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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 41efefad546a256ec0cbd3bf06ed9c7b4d2e60106a6afc661e6866d9de8fadc2
4
+ data.tar.gz: 1d7f3ac2d2503bd3bfa1d113fe5c12be99e45e4f0fa501841e3173cf67a60426
5
+ SHA512:
6
+ metadata.gz: 0d44d838641769aa9a3ef4eea3ede06137522ae17493f13fb050e5d0eca02873828da6f671ef76247108f9ca63bd6a41e8805ef50a450fcb5307b9ecdcc23893
7
+ data.tar.gz: 96abad194832543b77954753543103924c1390b45f682d278e8cd8f3389cf885326cf0acd2f30d523a853c94d7deb64f43802a5d1fd5f69094b589e28ff23b9a
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,8 @@
1
+ AllCops:
2
+ TargetRubyVersion: 3.1
3
+
4
+ Style/StringLiterals:
5
+ EnforcedStyle: double_quotes
6
+
7
+ Style/StringLiteralsInInterpolation:
8
+ EnforcedStyle: double_quotes
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2025-06-15
4
+
5
+ - Initial release
@@ -0,0 +1,132 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our
6
+ community a harassment-free experience for everyone, regardless of age, body
7
+ size, visible or invisible disability, ethnicity, sex characteristics, gender
8
+ identity and expression, level of experience, education, socio-economic status,
9
+ nationality, personal appearance, race, caste, color, religion, or sexual
10
+ identity and orientation.
11
+
12
+ We pledge to act and interact in ways that contribute to an open, welcoming,
13
+ diverse, inclusive, and healthy community.
14
+
15
+ ## Our Standards
16
+
17
+ Examples of behavior that contributes to a positive environment for our
18
+ community include:
19
+
20
+ * Demonstrating empathy and kindness toward other people
21
+ * Being respectful of differing opinions, viewpoints, and experiences
22
+ * Giving and gracefully accepting constructive feedback
23
+ * Accepting responsibility and apologizing to those affected by our mistakes,
24
+ and learning from the experience
25
+ * Focusing on what is best not just for us as individuals, but for the overall
26
+ community
27
+
28
+ Examples of unacceptable behavior include:
29
+
30
+ * The use of sexualized language or imagery, and sexual attention or advances of
31
+ any kind
32
+ * Trolling, insulting or derogatory comments, and personal or political attacks
33
+ * Public or private harassment
34
+ * Publishing others' private information, such as a physical or email address,
35
+ without their explicit permission
36
+ * Other conduct which could reasonably be considered inappropriate in a
37
+ professional setting
38
+
39
+ ## Enforcement Responsibilities
40
+
41
+ Community leaders are responsible for clarifying and enforcing our standards of
42
+ acceptable behavior and will take appropriate and fair corrective action in
43
+ response to any behavior that they deem inappropriate, threatening, offensive,
44
+ or harmful.
45
+
46
+ Community leaders have the right and responsibility to remove, edit, or reject
47
+ comments, commits, code, wiki edits, issues, and other contributions that are
48
+ not aligned to this Code of Conduct, and will communicate reasons for moderation
49
+ decisions when appropriate.
50
+
51
+ ## Scope
52
+
53
+ This Code of Conduct applies within all community spaces, and also applies when
54
+ an individual is officially representing the community in public spaces.
55
+ Examples of representing our community include using an official email address,
56
+ posting via an official social media account, or acting as an appointed
57
+ representative at an online or offline event.
58
+
59
+ ## Enforcement
60
+
61
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
62
+ reported to the community leaders responsible for enforcement at
63
+ [INSERT CONTACT METHOD].
64
+ All complaints will be reviewed and investigated promptly and fairly.
65
+
66
+ All community leaders are obligated to respect the privacy and security of the
67
+ reporter of any incident.
68
+
69
+ ## Enforcement Guidelines
70
+
71
+ Community leaders will follow these Community Impact Guidelines in determining
72
+ the consequences for any action they deem in violation of this Code of Conduct:
73
+
74
+ ### 1. Correction
75
+
76
+ **Community Impact**: Use of inappropriate language or other behavior deemed
77
+ unprofessional or unwelcome in the community.
78
+
79
+ **Consequence**: A private, written warning from community leaders, providing
80
+ clarity around the nature of the violation and an explanation of why the
81
+ behavior was inappropriate. A public apology may be requested.
82
+
83
+ ### 2. Warning
84
+
85
+ **Community Impact**: A violation through a single incident or series of
86
+ actions.
87
+
88
+ **Consequence**: A warning with consequences for continued behavior. No
89
+ interaction with the people involved, including unsolicited interaction with
90
+ those enforcing the Code of Conduct, for a specified period of time. This
91
+ includes avoiding interactions in community spaces as well as external channels
92
+ like social media. Violating these terms may lead to a temporary or permanent
93
+ ban.
94
+
95
+ ### 3. Temporary Ban
96
+
97
+ **Community Impact**: A serious violation of community standards, including
98
+ sustained inappropriate behavior.
99
+
100
+ **Consequence**: A temporary ban from any sort of interaction or public
101
+ communication with the community for a specified period of time. No public or
102
+ private interaction with the people involved, including unsolicited interaction
103
+ with those enforcing the Code of Conduct, is allowed during this period.
104
+ Violating these terms may lead to a permanent ban.
105
+
106
+ ### 4. Permanent Ban
107
+
108
+ **Community Impact**: Demonstrating a pattern of violation of community
109
+ standards, including sustained inappropriate behavior, harassment of an
110
+ individual, or aggression toward or disparagement of classes of individuals.
111
+
112
+ **Consequence**: A permanent ban from any sort of public interaction within the
113
+ community.
114
+
115
+ ## Attribution
116
+
117
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage],
118
+ version 2.1, available at
119
+ [https://www.contributor-covenant.org/version/2/1/code_of_conduct.html][v2.1].
120
+
121
+ Community Impact Guidelines were inspired by
122
+ [Mozilla's code of conduct enforcement ladder][Mozilla CoC].
123
+
124
+ For answers to common questions about this code of conduct, see the FAQ at
125
+ [https://www.contributor-covenant.org/faq][FAQ]. Translations are available at
126
+ [https://www.contributor-covenant.org/translations][translations].
127
+
128
+ [homepage]: https://www.contributor-covenant.org
129
+ [v2.1]: https://www.contributor-covenant.org/version/2/1/code_of_conduct.html
130
+ [Mozilla CoC]: https://github.com/mozilla/diversity
131
+ [FAQ]: https://www.contributor-covenant.org/faq
132
+ [translations]: https://www.contributor-covenant.org/translations
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2025 Dmytro Koval
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,145 @@
1
+ # TpTree
2
+
3
+ A Ruby gem for visualizing method call traces with timing information. TPTree uses Ruby's TracePoint to capture method calls and presents them in a beautiful tree format with execution times, parameters, and return values.
4
+
5
+ ## Features
6
+
7
+ - 🌳 **Tree visualization** of method calls with proper indentation
8
+ - ⏱️ **Timing information** for performance analysis
9
+ - 🎯 **Method filtering** to focus on specific methods or classes
10
+ - 📊 **JSON export** for integration with external tools
11
+ - 🎨 **Colorized output** for better readability
12
+ - 🔧 **Multiple output formats** (ANSI and XML)
13
+
14
+ ## Installation
15
+
16
+ Install the gem by executing:
17
+
18
+ ```bash
19
+ gem install tp_tree
20
+ ```
21
+
22
+ Or add it to your Gemfile:
23
+
24
+ ```ruby
25
+ gem 'tp_tree'
26
+ ```
27
+
28
+ Then execute:
29
+
30
+ ```bash
31
+ bundle install
32
+ ```
33
+
34
+ ## Usage
35
+
36
+ ### Basic Usage
37
+
38
+ Wrap any code block with `TPTree.catch` to trace method calls:
39
+
40
+ ```ruby
41
+ require 'tp_tree'
42
+
43
+ def slow_method(n)
44
+ sleep(0.1)
45
+ fast_method(n)
46
+ end
47
+
48
+ def fast_method(n)
49
+ sleep(0.01)
50
+ n * 2
51
+ end
52
+
53
+ TPTree.catch do
54
+ slow_method(5)
55
+ end
56
+ ```
57
+
58
+ Output:
59
+ ```
60
+ slow_method(n = 5) [112.0ms]
61
+ │ fast_method(n = 5) → 10 [11.1ms]
62
+ └→ 10 [112.0ms]
63
+ ```
64
+
65
+ ### Method Filtering
66
+
67
+ Filter methods by name, class, or custom criteria:
68
+
69
+ ```ruby
70
+ # Filter by method name (string or regex)
71
+ TPTree.catch(filter: 'slow_method') do
72
+ # your code
73
+ end
74
+
75
+ # Filter by multiple criteria
76
+ TPTree.catch(filter: ['method1', /^User/, SomeClass]) do
77
+ # your code
78
+ end
79
+
80
+ # Exclude specific methods
81
+ TPTree.catch(exclude: 'fast_method') do
82
+ # your code
83
+ end
84
+
85
+ # Custom filtering with block
86
+ TPTree.catch(filter: ->(call_info) { call_info.method_name.start_with?('api_') }) do
87
+ # your code
88
+ end
89
+ ```
90
+
91
+ ### JSON Export
92
+
93
+ Export trace data for external analysis:
94
+
95
+ ```ruby
96
+ TPTree.catch(json_file: 'trace.json') do
97
+ # your code
98
+ end
99
+ ```
100
+
101
+ The JSON file contains structured data with timing, parameters, return values, and call hierarchy.
102
+
103
+ ### Advanced Options
104
+
105
+ ```ruby
106
+ TPTree.catch(
107
+ filter: 'important_method', # Method filtering
108
+ exclude: 'noise_method', # Method exclusion
109
+ json_file: 'trace.json', # JSON export
110
+ interactive: true # Interactive viewer (if available)
111
+ ) do
112
+ # your code
113
+ end
114
+ ```
115
+
116
+ ## Examples
117
+
118
+ See the `examples/` directory for complete demonstrations:
119
+
120
+ - `timing_demo.rb` - Basic timing visualization
121
+ - `interactive_timing_demo.rb` - Interactive viewer demo
122
+ - `semi_empty_nodes_demo.rb` - Complex filtering examples
123
+
124
+ Run them with:
125
+ ```bash
126
+ ruby examples/timing_demo.rb
127
+ ```
128
+
129
+ ## Development
130
+
131
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
132
+
133
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
134
+
135
+ ## Contributing
136
+
137
+ Bug reports and pull requests are welcome on GitHub at https://github.com/dmk/tp_tree. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/dmk/tp_tree/blob/main/CODE_OF_CONDUCT.md).
138
+
139
+ ## License
140
+
141
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
142
+
143
+ ## Code of Conduct
144
+
145
+ Everyone interacting in the TpTree project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/dmk/tp_tree/blob/main/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ require "rubocop/rake_task"
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: %i[spec rubocop]
@@ -0,0 +1,35 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require_relative '../lib/tp_tree'
4
+
5
+ def recursive_fibonacci(n)
6
+ return n if n <= 1
7
+ sleep(0.001) # Small delay to make timing visible
8
+ recursive_fibonacci(n - 1) + recursive_fibonacci(n - 2)
9
+ end
10
+
11
+ def optimized_fibonacci(n)
12
+ return n if n <= 1
13
+ sleep(0.001)
14
+
15
+ a, b = 0, 1
16
+ 2.upto(n) do
17
+ sleep(0.0001)
18
+ a, b = b, a + b
19
+ end
20
+ b
21
+ end
22
+
23
+ puts "=== Interactive Method Timing Demo ==="
24
+ puts "This demo shows method execution times in interactive mode"
25
+ puts "Use arrow keys to navigate, Enter to expand/collapse, 'q' to quit"
26
+ puts "Press any key to start..."
27
+ gets
28
+
29
+ TPTree.catch(interactive: true) do
30
+ puts "Computing fibonacci(5) recursively..."
31
+ recursive_fibonacci(5)
32
+
33
+ puts "Computing fibonacci(5) iteratively..."
34
+ optimized_fibonacci(5)
35
+ end
@@ -0,0 +1,47 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require_relative '../lib/tp_tree'
5
+
6
+ # Example methods to demonstrate semi-empty nodes
7
+ class Demo
8
+ def public_method
9
+ private_helper_method("hello")
10
+ end
11
+
12
+ private
13
+
14
+ def private_helper_method(message)
15
+ internal_processing(message)
16
+ end
17
+
18
+ def internal_processing(data)
19
+ final_operation(data.upcase)
20
+ end
21
+
22
+ def final_operation(processed_data)
23
+ "Result: #{processed_data}"
24
+ end
25
+ end
26
+
27
+ puts "=== Without filtering (full call tree) ==="
28
+ TPTree.catch do
29
+ demo = Demo.new
30
+ result = demo.public_method
31
+ puts "Final: #{result}"
32
+ end
33
+
34
+ puts "\n=== With filtering (semi-empty nodes for filtered methods) ==="
35
+ puts "Filtering out private helper methods but keeping them visible"
36
+ TPTree.catch(exclude: [/private_helper_method/, /internal_processing/]) do
37
+ demo = Demo.new
38
+ result = demo.public_method
39
+ puts "Final: #{result}"
40
+ end
41
+
42
+ puts "\n=== Heavy filtering (multiple methods filtered) ==="
43
+ TPTree.catch(exclude: [/helper/, /processing/, /operation/]) do
44
+ demo = Demo.new
45
+ result = demo.public_method
46
+ puts "Final: #{result}"
47
+ end
@@ -0,0 +1,28 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require_relative '../lib/tp_tree'
4
+
5
+ def slow_method(n)
6
+ sleep(0.1)
7
+ fast_method(n)
8
+ end
9
+
10
+ def fast_method(n)
11
+ sleep(0.01)
12
+ n * 2
13
+ end
14
+
15
+ def complex_calculation(arr)
16
+ sleep(0.05)
17
+ arr.map { |x| slow_method(x) }.sum
18
+ end
19
+
20
+ puts "=== Method Timing Demo ==="
21
+ puts "This demo shows method execution times in the call tree"
22
+ puts
23
+
24
+ result = TPTree.catch do
25
+ complex_calculation([1, 2, 3])
26
+ end
27
+
28
+ puts "\nDemo completed!"
data/exe/tp_tree ADDED
@@ -0,0 +1,23 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ $LOAD_PATH.unshift(File.expand_path('../lib', __dir__))
5
+ require 'tp_tree'
6
+
7
+ if ARGV.empty?
8
+ puts "Usage: tp_tree [--interactive] <ruby_file>"
9
+ exit 1
10
+ end
11
+
12
+ interactive = ARGV.delete('--interactive')
13
+ file = ARGV.first
14
+
15
+ unless File.exist?(file)
16
+ puts "Error: File '#{file}' not found"
17
+ exit 1
18
+ end
19
+
20
+ code = File.read(file)
21
+ TPTree.catch(interactive: interactive) do
22
+ eval(code, TOPLEVEL_BINDING, file)
23
+ end
@@ -0,0 +1,72 @@
1
+ # frozen_string_literal: true
2
+
3
+ module TPTree
4
+ # CallStack manages the state of method calls during tracing
5
+ class CallStack
6
+ def initialize
7
+ @call_stack = []
8
+ @call_depth = 0
9
+ @events = []
10
+ end
11
+
12
+ def start_call(method_name, parameters, defined_class, path, lineno, start_time)
13
+ call_info = {
14
+ method_name: method_name,
15
+ parameters: parameters,
16
+ depth: @call_depth,
17
+ event_index: @events.length,
18
+ defined_class: defined_class,
19
+ path: path,
20
+ lineno: lineno,
21
+ start_time: start_time
22
+ }
23
+
24
+ @call_stack.push(call_info)
25
+ @events << nil # Placeholder for the actual TreeNode
26
+ @call_depth += 1
27
+
28
+ call_info
29
+ end
30
+
31
+ def finish_call(method_name, return_value, end_time)
32
+ return nil if @call_stack.empty?
33
+
34
+ # Find matching call on stack (handles filtered nested calls)
35
+ call_info = @call_stack.last
36
+ return nil unless call_info && call_info[:method_name] == method_name
37
+
38
+ @call_depth -= 1
39
+ @call_stack.pop
40
+
41
+ call_info.merge(
42
+ return_value: return_value,
43
+ end_time: end_time,
44
+ has_children: @events.length > call_info[:event_index] + 1
45
+ )
46
+ end
47
+
48
+ def add_event(event)
49
+ @events << event
50
+ end
51
+
52
+ def set_event_at_index(index, event)
53
+ @events[index] = event
54
+ end
55
+
56
+ def events
57
+ @events.compact
58
+ end
59
+
60
+ def events_array
61
+ @events
62
+ end
63
+
64
+ def current_depth
65
+ @call_depth
66
+ end
67
+
68
+ def empty?
69
+ @call_stack.empty?
70
+ end
71
+ end
72
+ end
@@ -0,0 +1,47 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'formatters/ansi_formatter'
4
+
5
+ module TPTree
6
+ # Formatter provides methods for colorizing and formatting output.
7
+ # This module acts as a compatibility layer for the old Formatter module.
8
+ module Formatter
9
+ def self.included(base)
10
+ base.extend(FormatterMethods)
11
+ base.include(FormatterMethods)
12
+ end
13
+
14
+ module FormatterMethods
15
+ def formatter
16
+ @formatter ||= Formatters::AnsiFormatter.new
17
+ end
18
+
19
+ def colorize(text, color)
20
+ formatter.colorize(text, color)
21
+ end
22
+
23
+ def format_timing(duration)
24
+ formatter.format_timing(duration)
25
+ end
26
+
27
+ def format_parameters(parameters)
28
+ formatter.format_parameters(parameters)
29
+ end
30
+
31
+ def format_value(value)
32
+ formatter.format_value(value)
33
+ end
34
+
35
+ def format_return_value(return_value)
36
+ formatter.format_return_value(return_value)
37
+ end
38
+
39
+ def color_for_depth(depth)
40
+ formatter.color_for_depth(depth)
41
+ end
42
+ end
43
+
44
+ # Expose constants for backward compatibility
45
+ DEPTH_COLORS = Formatters::BaseFormatter::DEPTH_COLORS
46
+ end
47
+ end
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'base_formatter'
4
+
5
+ module TPTree
6
+ module Formatters
7
+ # AnsiFormatter provides ANSI color code formatting for terminal output
8
+ class AnsiFormatter < BaseFormatter
9
+ def colorize(text, color)
10
+ color_codes = {
11
+ black: 30, red: 31, green: 32, yellow: 33,
12
+ blue: 34, magenta: 35, cyan: 36, white: 37
13
+ }
14
+
15
+ code = color_codes[color] || 37
16
+ "\e[#{code}m#{text}\e[0m"
17
+ end
18
+ end
19
+ end
20
+ end