tree_print 0.0.1 → 0.1.1
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 +4 -4
- data/lib/tree_print.rb +6 -4
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 93f5cbdb0a954a18bb113b32dea3c8b2dda01701
|
4
|
+
data.tar.gz: 4f3a1e0421e1c3cfc8fc713aede5fa289f37dbc8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 641e3371a9ab8e9545757c3c771fabb120560bf65fced820c46051ddf6bb449c261bc692aa1e56ca2123b2cfa84c11b5553e6157e6bea8e753ed08e5af4b2f89
|
7
|
+
data.tar.gz: bb3cba9828350c4d46ed6138c364cb66f53354c56037357c428e1fbf2b1a2e90e8117d733533543eb041e609fe02625c2d38e0a1d20d26c2330710dec9ca56de
|
data/lib/tree_print.rb
CHANGED
@@ -4,22 +4,23 @@ module TreePrint
|
|
4
4
|
height = hosts.compact.map(&:height).max
|
5
5
|
buffer = indent(height)
|
6
6
|
max_length = hosts.compact.map { |node| node.val.to_s.length }.max
|
7
|
-
max_length = [8, max_length].min
|
7
|
+
max_length = [8, max_length].min #arbitrary cut-off for super-long strings
|
8
8
|
str = ""
|
9
9
|
row = ""
|
10
10
|
max_length.times do |i|
|
11
11
|
row = hosts.inject("") do |sum, node|
|
12
|
-
sum + buffer + node.val.to_s[i] + buffer + " " rescue sum + buffer + "
|
12
|
+
sum + buffer + (node.val.to_s[i] || '.') + buffer + " " rescue sum + buffer + " " + buffer + " " #in case node is nil
|
13
13
|
end + " \n"
|
14
14
|
str << row
|
15
15
|
end
|
16
16
|
if height > 1
|
17
17
|
(2**(height-2)).times do
|
18
|
-
row = row.gsub(/ \//, '/ ').gsub(/\\ /, " \\")
|
18
|
+
row = row.gsub(/ \//, '/ ').gsub(/\\ /, " \\") #left and right slashes
|
19
|
+
.gsub(/ [\w|.] /, '/ \\') #exchange values for slashes
|
19
20
|
str << row
|
20
21
|
end
|
21
22
|
end
|
22
|
-
return str + tree_to_s(*hosts.flat_map{ |host|
|
23
|
+
return str + tree_to_s(*hosts.flat_map{ |host| #recursion, breadth-first
|
23
24
|
if !host
|
24
25
|
[nil, nil]
|
25
26
|
else
|
@@ -28,6 +29,7 @@ module TreePrint
|
|
28
29
|
})
|
29
30
|
end
|
30
31
|
|
32
|
+
#determine proper number of spaces
|
31
33
|
def indent(height)
|
32
34
|
" " * (1...height).inject { |sum, n| sum + 2 ** (n-1) } rescue ""
|
33
35
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: tree_print
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Andrew Carlson
|
@@ -18,7 +18,7 @@ extensions: []
|
|
18
18
|
extra_rdoc_files: []
|
19
19
|
files:
|
20
20
|
- lib/tree_print.rb
|
21
|
-
homepage:
|
21
|
+
homepage: https://github.com/acarl005/tree_print
|
22
22
|
licenses:
|
23
23
|
- MIT
|
24
24
|
metadata: {}
|