foobara-util 0.0.7 → 0.0.9
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +8 -0
- data/lib/foobara/util/tree.rb +144 -0
- metadata +5 -8
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6b4070d73b35f4d78f9eed62ed1aa1e717beed8049881885c92291f1ae01c055
|
4
|
+
data.tar.gz: a9a7185f19e1e64655a1460444229de4ba94c2e1f15050f59063aa01d4b9f47e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: bfdd2d199b0c13951d122851d7a2dcd0820de3ab75e152bf47f1cc663fe85677c4f9fa82b01531a9dd84210b1ce9ef19561a0a7bcc365406223c0b7a74b147d0
|
7
|
+
data.tar.gz: 14ec8a7ac063c58018920029fce654ecadc4902fcb2844fcfae4a846ddd7f218c5d2d9f43e8c5fb75869c43c0cc6d91e882c4cb011bc0cbbf8414e337a7e6f19
|
data/CHANGELOG.md
CHANGED
@@ -0,0 +1,144 @@
|
|
1
|
+
module Foobara
|
2
|
+
module Util
|
3
|
+
class SubTree
|
4
|
+
attr_accessor :value, :parent_tree, :to_parent
|
5
|
+
|
6
|
+
def initialize(value, to_parent = nil, &block)
|
7
|
+
self.value = value
|
8
|
+
self.to_parent = to_parent.nil? ? block : to_parent.to_proc
|
9
|
+
end
|
10
|
+
|
11
|
+
def root?
|
12
|
+
parent_tree.is_a?(Tree)
|
13
|
+
end
|
14
|
+
|
15
|
+
def children_trees
|
16
|
+
@children_trees ||= []
|
17
|
+
end
|
18
|
+
|
19
|
+
def add(object)
|
20
|
+
return if include?(object)
|
21
|
+
|
22
|
+
parent = to_parent.call(object)
|
23
|
+
|
24
|
+
parent_node = if parent
|
25
|
+
node_for(parent) || add(parent)
|
26
|
+
else
|
27
|
+
self
|
28
|
+
end
|
29
|
+
|
30
|
+
child_tree = SubTree.new(object, to_parent)
|
31
|
+
child_tree.parent_tree = parent_node
|
32
|
+
parent_node.children_trees << child_tree
|
33
|
+
|
34
|
+
child_tree
|
35
|
+
end
|
36
|
+
|
37
|
+
def node_for(value)
|
38
|
+
return self if value == self.value
|
39
|
+
|
40
|
+
children_trees.each do |child|
|
41
|
+
node = child.node_for(value)
|
42
|
+
return node if node
|
43
|
+
end
|
44
|
+
|
45
|
+
nil
|
46
|
+
end
|
47
|
+
|
48
|
+
def include?(value)
|
49
|
+
!!node_for(value)
|
50
|
+
end
|
51
|
+
|
52
|
+
# TODO: try to break this up a bit
|
53
|
+
def to_s(to_name, padding: "", last_child: true)
|
54
|
+
output = StringIO.new
|
55
|
+
name = to_name.call(value)
|
56
|
+
|
57
|
+
padding = puts_box(output, name, padding, last_child)
|
58
|
+
|
59
|
+
children = children_trees.sort_by { |child_tree| to_name.call(child_tree.value) }
|
60
|
+
last = children.last
|
61
|
+
|
62
|
+
children.each do |child_tree|
|
63
|
+
output.puts child_tree.to_s(to_name, padding:, last_child: child_tree == last)
|
64
|
+
end
|
65
|
+
|
66
|
+
output.string
|
67
|
+
end
|
68
|
+
|
69
|
+
private
|
70
|
+
|
71
|
+
def puts_box(output, name, padding, last_child)
|
72
|
+
name_size = name.size + 1
|
73
|
+
name_size_left = name_size / 2
|
74
|
+
name_size_right = name_size - name_size_left
|
75
|
+
|
76
|
+
bottom_connector = children_trees.any? ? "┬" : "─"
|
77
|
+
|
78
|
+
if root?
|
79
|
+
output.puts "╭#{"─" * (name.size + 2)}╮"
|
80
|
+
output.puts "│ #{name} │"
|
81
|
+
output.puts "╰#{"─" * name_size_left}#{bottom_connector}#{"─" * name_size_right}╯"
|
82
|
+
else
|
83
|
+
padding += " "
|
84
|
+
|
85
|
+
connector = last_child ? "└" : "├"
|
86
|
+
padding_end = last_child ? " " : "│ "
|
87
|
+
|
88
|
+
output.print padding
|
89
|
+
output.print "│ "
|
90
|
+
|
91
|
+
output.puts "╭#{"─" * (name.size + 2)}╮"
|
92
|
+
output.print "#{padding}#{connector}"
|
93
|
+
output.puts "─┤ #{name} │"
|
94
|
+
|
95
|
+
padding += padding_end
|
96
|
+
|
97
|
+
output.puts "#{padding}╰#{"─" * name_size_left}#{bottom_connector}#{"─" * name_size_right}╯"
|
98
|
+
end
|
99
|
+
|
100
|
+
padding + (" " * (name_size / 2))
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
104
|
+
class Tree < SubTree
|
105
|
+
def initialize(data, to_parent = nil, &)
|
106
|
+
super(nil, to_parent, &)
|
107
|
+
|
108
|
+
data.each do |object|
|
109
|
+
add(object)
|
110
|
+
end
|
111
|
+
end
|
112
|
+
|
113
|
+
def to_s(to_name = nil, &block)
|
114
|
+
to_name = if to_name
|
115
|
+
to_name.to_proc
|
116
|
+
elsif block_given?
|
117
|
+
block
|
118
|
+
else
|
119
|
+
:to_s.to_proc
|
120
|
+
end
|
121
|
+
|
122
|
+
children_trees.map { |child| child.to_s(to_name) }.join("\n")
|
123
|
+
end
|
124
|
+
end
|
125
|
+
|
126
|
+
module_function
|
127
|
+
|
128
|
+
def print_tree(data, io: $stdout, to_name: nil, to_parent: nil)
|
129
|
+
data = data.to_a if data.is_a?(::Hash)
|
130
|
+
|
131
|
+
if to_name.nil? && to_parent.nil?
|
132
|
+
to_name = :first.to_proc
|
133
|
+
to_parent = proc do |object|
|
134
|
+
parent_name = object.last
|
135
|
+
data.find { |pair| pair.first == parent_name }
|
136
|
+
end
|
137
|
+
end
|
138
|
+
|
139
|
+
tree = Util::Tree.new(data, to_parent)
|
140
|
+
|
141
|
+
io.puts tree.to_s(to_name)
|
142
|
+
end
|
143
|
+
end
|
144
|
+
end
|
metadata
CHANGED
@@ -1,16 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: foobara-util
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.9
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Miles Georgi
|
8
|
-
autorequire:
|
9
8
|
bindir: bin
|
10
9
|
cert_chain: []
|
11
|
-
date:
|
10
|
+
date: 2025-01-07 00:00:00.000000000 Z
|
12
11
|
dependencies: []
|
13
|
-
description:
|
14
12
|
email:
|
15
13
|
- azimux@gmail.com
|
16
14
|
executables: []
|
@@ -33,6 +31,7 @@ files:
|
|
33
31
|
- lib/foobara/util/require.rb
|
34
32
|
- lib/foobara/util/string.rb
|
35
33
|
- lib/foobara/util/structured.rb
|
34
|
+
- lib/foobara/util/tree.rb
|
36
35
|
homepage: https://github.com/foobara/util
|
37
36
|
licenses:
|
38
37
|
- Apache-2.0
|
@@ -42,7 +41,6 @@ metadata:
|
|
42
41
|
source_code_uri: https://github.com/foobara/util
|
43
42
|
changelog_uri: https://github.com/foobara/util/blob/main/CHANGELOG.md
|
44
43
|
rubygems_mfa_required: 'true'
|
45
|
-
post_install_message:
|
46
44
|
rdoc_options: []
|
47
45
|
require_paths:
|
48
46
|
- lib
|
@@ -50,15 +48,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
50
48
|
requirements:
|
51
49
|
- - ">="
|
52
50
|
- !ruby/object:Gem::Version
|
53
|
-
version: 3.
|
51
|
+
version: 3.4.0
|
54
52
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
55
53
|
requirements:
|
56
54
|
- - ">="
|
57
55
|
- !ruby/object:Gem::Version
|
58
56
|
version: '0'
|
59
57
|
requirements: []
|
60
|
-
rubygems_version: 3.
|
61
|
-
signing_key:
|
58
|
+
rubygems_version: 3.6.2
|
62
59
|
specification_version: 4
|
63
60
|
summary: Utility functions used across various Foobara projects
|
64
61
|
test_files: []
|