tree.rb 0.3.8 → 0.3.9

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,179 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
5
+
6
+ <link type="text/css" rel="stylesheet" href="style.css">
7
+
8
+ <script src="http://d3js.org/d3.v2.js"></script>
9
+ <script src="data.js"></script>
10
+
11
+ <style type="text/css">
12
+
13
+ .chart {
14
+ display: block;
15
+ margin: auto;
16
+ margin-top: 40px;
17
+ }
18
+
19
+ text {
20
+ font-size: 11px;
21
+ }
22
+
23
+ rect {
24
+ fill: none;
25
+ }
26
+
27
+ </style>
28
+ </head>
29
+
30
+ <body>
31
+ <div id="body">
32
+
33
+
34
+ <div id="footer">
35
+ d3.layout.treemap
36
+ <div class="hint">click or option-click to descend or ascend</div>
37
+ <div><select>
38
+ <option value="size">Size</option>
39
+ <option value="count">Count</option>
40
+ </select></div>
41
+ </div>
42
+
43
+
44
+ </div>
45
+
46
+ <script type="text/javascript">
47
+
48
+
49
+ var w = 1280 - 80,
50
+ h = 800 - 180,
51
+ x = d3.scale.linear().range([0, w]),
52
+ y = d3.scale.linear().range([0, h]),
53
+ color = d3.scale.category20c(),
54
+ root,
55
+ node;
56
+
57
+ var treemap = d3.layout.treemap()
58
+ .round(false)
59
+ .size([w, h])
60
+ .sticky(true)
61
+ .value(function (d) {
62
+ return d.size;
63
+ });
64
+
65
+ var svg = d3
66
+ .select("#body")
67
+ .append("div")
68
+ .attr("class", "chart")
69
+ .style("width", w + "px")
70
+ .style("height", h + "px")
71
+ .append("svg:svg")
72
+ .attr("width", w)
73
+ .attr("height", h)
74
+ .append("svg:g")
75
+ .attr("transform", "translate(.5,.5)");
76
+
77
+ function size(d) {
78
+ return d.size;
79
+ }
80
+
81
+ function count(d) {
82
+ return 1;
83
+ }
84
+
85
+ function zoom(d) {
86
+ var kx = w / d.dx, ky = h / d.dy;
87
+ x.domain([d.x, d.x + d.dx]);
88
+ y.domain([d.y, d.y + d.dy]);
89
+
90
+ var t = svg.selectAll("g.cell").transition()
91
+ .duration(d3.event.altKey ? 7500 : 750)
92
+ .attr("transform", function (d) {
93
+ return "translate(" + x(d.x) + "," + y(d.y) + ")";
94
+ });
95
+
96
+ t.select("rect")
97
+ .attr("width", function (d) {
98
+ return kx * d.dx - 1;
99
+ })
100
+ .attr("height", function (d) {
101
+ return ky * d.dy - 1;
102
+ })
103
+
104
+ t.select("text")
105
+ .attr("x", function (d) {
106
+ return kx * d.dx / 2;
107
+ })
108
+ .attr("y", function (d) {
109
+ return ky * d.dy / 2;
110
+ })
111
+ .style("opacity", function (d) {
112
+ return kx * d.dx > d.w ? 1 : 0;
113
+ });
114
+
115
+ node = d;
116
+ d3.event.stopPropagation();
117
+ }
118
+
119
+
120
+ node = root = data;
121
+
122
+ var nodes = treemap.nodes(root)
123
+ .filter(function (d) {
124
+ return !d.children;
125
+ });
126
+
127
+ var cell = svg.selectAll("g")
128
+ .data(nodes)
129
+ .enter().append("svg:g")
130
+ .attr("class", "cell")
131
+ .attr("transform", function (d) {
132
+ return "translate(" + d.x + "," + d.y + ")";
133
+ })
134
+ .on("click", function (d) {
135
+ return zoom(node == d.parent ? root : d.parent);
136
+ });
137
+
138
+ cell.append("svg:rect")
139
+ .attr("width", function (d) {
140
+ return d.dx - 1;
141
+ })
142
+ .attr("height", function (d) {
143
+ return d.dy - 1;
144
+ })
145
+ .style("fill", function (d) {
146
+ return color(d.parent.name);
147
+ });
148
+
149
+ cell.append("svg:text")
150
+ .attr("x", function (d) {
151
+ return d.dx / 2;
152
+ })
153
+ .attr("y", function (d) {
154
+ return d.dy / 2;
155
+ })
156
+ .attr("dy", ".35em")
157
+ .attr("text-anchor", "middle")
158
+ .text(function (d) {
159
+ return d.name;
160
+ })
161
+ .style("opacity", function (d) {
162
+ d.w = this.getComputedTextLength();
163
+ return d.dx > d.w ? 1 : 0;
164
+ });
165
+
166
+ d3.select(window).on("click", function () {
167
+ zoom(root);
168
+ });
169
+
170
+ d3.select("select").on("change", function () {
171
+ d3_treemap.value(this.value == "size" ? size : count).nodes(root);
172
+ zoom(node);
173
+ });
174
+
175
+
176
+ </script>
177
+
178
+ </body>
179
+ </html>
@@ -0,0 +1,2 @@
1
+ #!/bin/bash
2
+ ../../bin/tree.rb ../.. --format js_treemap --force -o data.js
@@ -0,0 +1,89 @@
1
+ body {
2
+ background: url(texture-noise.png);
3
+ overflow: hidden;
4
+ margin: 0;
5
+ font-size: 14px;
6
+ font-family: "Helvetica Neue", Helvetica;
7
+ }
8
+
9
+ #chart, #header, #footer {
10
+ position: absolute;
11
+ top: 0;
12
+ }
13
+
14
+ #header, #footer {
15
+ z-index: 1;
16
+ display: block;
17
+ font-size: 36px;
18
+ font-weight: 300;
19
+ text-shadow: 0 1px 0 #fff;
20
+ }
21
+
22
+ #header.inverted, #footer.inverted {
23
+ color: #fff;
24
+ text-shadow: 0 1px 4px #000;
25
+ }
26
+
27
+ #header {
28
+ top: 80px;
29
+ left: 140px;
30
+ width: 1000px;
31
+ }
32
+
33
+ #footer {
34
+ top: 680px;
35
+ right: 140px;
36
+ text-align: right;
37
+ }
38
+
39
+ rect {
40
+ fill: none;
41
+ pointer-events: all;
42
+ }
43
+
44
+ pre {
45
+ font-size: 18px;
46
+ }
47
+
48
+ line {
49
+ stroke: #000;
50
+ stroke-width: 1.5px;
51
+ }
52
+
53
+ .string, .regexp {
54
+ color: #f39;
55
+ }
56
+
57
+ .keyword {
58
+ color: #00c;
59
+ }
60
+
61
+ .comment {
62
+ color: #777;
63
+ font-style: oblique;
64
+ }
65
+
66
+ .number {
67
+ color: #369;
68
+ }
69
+
70
+ .class, .special {
71
+ color: #1181B8;
72
+ }
73
+
74
+ a:link, a:visited {
75
+ color: #000;
76
+ text-decoration: none;
77
+ }
78
+
79
+ a:hover {
80
+ color: #666;
81
+ }
82
+
83
+ .hint {
84
+ position: absolute;
85
+ right: 0;
86
+ width: 1280px;
87
+ font-size: 12px;
88
+ color: #999;
89
+ }
@@ -3,7 +3,7 @@ require 'ostruct'
3
3
 
4
4
  cwd = File.expand_path( File.join( File.dirname(__FILE__), "..", "..", "lib" ) )
5
5
  $:.unshift(cwd) unless $:.include?(cwd)
6
- require 'treevisitor'
6
+ require 'tree_rb'
7
7
 
8
8
  #
9
9
  # Find directories without subdirectories
@@ -31,7 +31,7 @@ class DirWithoutSubDir < TreeRb::BasicTreeNodeVisitor
31
31
 
32
32
  end
33
33
 
34
- dtw = TreeRb::DirTreeWalker.new( ".." )
34
+ dtw = TreeRb::DirTreeWalker.new( File.join("..", ".." ))
35
35
  dtw.ignore /^\./
36
36
  dtw.visit_file=false
37
37
  dtw.run( DirWithoutSubDir.new )
@@ -2,12 +2,12 @@
2
2
 
3
3
  cwd = File.expand_path( File.join( File.dirname(__FILE__), "..", "..", "lib" ) )
4
4
  $:.unshift(cwd) unless $:.include?(cwd)
5
- require 'treevisitor'
5
+ require 'tree_rb'
6
6
  include TreeRb
7
7
 
8
8
  class MyVisitor < BasicTreeNodeVisitor
9
9
  def visit_leaf( pathname )
10
- puts pathname
10
+ puts "found: #{pathname}"
11
11
  end
12
12
  end
13
13
 
@@ -1,12 +1,12 @@
1
1
  # -*- coding: utf-8 -*-
2
2
  cwd = File.expand_path( File.join( File.dirname(__FILE__), "..", "..", "lib" ) )
3
3
  $:.unshift(cwd) unless $:.include?(cwd)
4
- require 'treevisitor'
4
+ require 'tree_rb'
5
5
  include TreeRb
6
6
 
7
7
  dtw = DirTreeWalker.new( :ignore => ".git" )
8
8
  dtw.run ".." do
9
9
  on_leaf do |pathname|
10
- puts pathname
10
+ puts "- #{pathname}"
11
11
  end
12
12
  end
@@ -23,12 +23,12 @@ module TreeRb
23
23
  parser.separator ""
24
24
  parser.separator "Generic options: "
25
25
 
26
- parser.on_tail("--help", "Show this message") do
26
+ parser.on("--help", "Show this message") do
27
27
  puts parser
28
28
  options[:exit] = 1
29
29
  end
30
30
 
31
- parser.on_tail("--version", "Show the version") do
31
+ parser.on("--version", "Show the version") do
32
32
  puts "tree.rb version #{TreeRb::VERSION}"
33
33
  options[:exit] = 1
34
34
  end
@@ -74,11 +74,10 @@ module TreeRb
74
74
  options[:show_indentation] = false
75
75
  end
76
76
 
77
- algos = %w[build-dir print-dir json json2 yaml sqlite]
78
- algo_aliases = { "b" => "build-dir", "v" => "print-dir", "j" => "json", "y" => "yaml", "s" => "sqlite" }
79
-
80
- algo_list = (algo_aliases.keys + algos).join(',')
81
- parser.on("--format ALGO", algos, algo_aliases, "select an algo", " (#{algo_list})") do |algo|
77
+ algos = %w[build-dir print-dir json d3js html_partition yaml sqlite]
78
+ # algo_aliases = { "b" => "build-dir", "v" => "print-dir", "j" => "json", "y" => "yaml", "s" => "sqlite" }
79
+ # algo_list = (algo_aliases.keys + algos).join(',')
80
+ parser.on("--format ALGO", algos, "select an algo", " (#{algos})") do |algo|
82
81
  options[:algo] = algo
83
82
  end
84
83
 
@@ -129,7 +128,7 @@ module TreeRb
129
128
  parser.on("-h",
130
129
  "Print the size of each file but in a more human readable way,",
131
130
  " e.g. appending a size letter for kilobytes (K), megabytes (M),",
132
- " gigabytes (G), terrabytes (T), petabytes (P) and exabytes (E).") do
131
+ " gigabytes (G), terabytes (T), petabytes (P) and exabytes (E).") do
133
132
  options[:show_size_human] = true
134
133
  end
135
134
 
@@ -151,7 +150,7 @@ module TreeRb
151
150
  end
152
151
 
153
152
  options[:show_report] = true
154
- parser.on("--noreport", "Omits printing of the file and directory report at the end of the tree listing.") do
153
+ parser.on("--no-report", "Omits printing of the file and directory report at the end of the tree listing.") do
155
154
  options[:show_report] = false
156
155
  end
157
156
 
@@ -265,11 +264,26 @@ module TreeRb
265
264
  $stderr.puts "#{File.basename(__FILE__)}:#{__LINE__} #{e.to_s}"
266
265
  end
267
266
 
268
- when 'json2'
267
+ when 'd3js'
269
268
  visitor = DirectoryToHash2Visitor.new(dirname)
270
269
  root = dtw.run(visitor).root
271
270
  begin
272
- output.puts JSON.pretty_generate(root)
271
+ str_json = JSON.pretty_generate(root)
272
+ str_json = "var data = " + str_json
273
+ output.puts str_json
274
+ rescue JSON::NestingError => e
275
+ $stderr.puts "#{File.basename(__FILE__)}:#{__LINE__} #{e.to_s}"
276
+ end
277
+
278
+ when 'html_partition'
279
+ visitor = DirectoryToHash2Visitor.new(dirname)
280
+ root = dtw.run(visitor).root
281
+ begin
282
+ str_json = JSON.pretty_generate(root)
283
+ str_json = "var data = " + str_json
284
+
285
+ render = ErbRender.new("d3js_layout_partition.erb", str_json)
286
+ output.puts render.render
273
287
  rescue JSON::NestingError => e
274
288
  $stderr.puts "#{File.basename(__FILE__)}:#{__LINE__} #{e.to_s}"
275
289
  end
@@ -0,0 +1,21 @@
1
+ # -*- coding: utf-8 -*-
2
+ module TreeRb
3
+
4
+ class ErbRender
5
+ include ERB::Util
6
+ attr_accessor :json_str
7
+
8
+ def initialize(template_name, json_str)
9
+ @template_name = template_name
10
+ @json_str = json_str
11
+ end
12
+
13
+ def render()
14
+ template_file_name = File.join(File.dirname(__FILE__), "templates", @template_name)
15
+ template = File.read(template_file_name)
16
+ ERB.new(template).result(binding)
17
+ end
18
+
19
+ end
20
+
21
+ end # end module
@@ -1,4 +1,4 @@
1
1
  # -*- coding: utf-8 -*-
2
2
  module TreeRb
3
- VERSION="0.3.8"
3
+ VERSION="0.3.9"
4
4
  end
@@ -4,6 +4,7 @@
4
4
  # stdlib
5
5
  #
6
6
  require 'optparse'
7
+ require 'erb'
7
8
 
8
9
  #
9
10
  # gem
@@ -13,5 +14,6 @@ require 'optparse'
13
14
  # tree_rb cli
14
15
  #
15
16
  require 'tree_rb'
17
+ require 'tree_rb/erb_render'
16
18
  require 'tree_rb/cli/cli_tree'
17
19
  require 'tree_rb/cli/cli_json'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tree.rb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.8
4
+ version: 0.3.9
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-09-15 00:00:00.000000000 Z
12
+ date: 2012-09-16 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: json
@@ -169,6 +169,7 @@ files:
169
169
  - lib/tree_rb/tree_node_visitor.rb
170
170
  - lib/tree_rb/tree_node.rb
171
171
  - lib/tree_rb/basic_tree_node_visitor.rb
172
+ - lib/tree_rb/erb_render.rb
172
173
  - lib/tree_rb/version.rb
173
174
  - lib/treevisitor.rb
174
175
  - lib/tree_rb.rb
@@ -176,23 +177,22 @@ files:
176
177
  - tasks/tree_rb.rake
177
178
  - tasks/rspec.rake
178
179
  - tasks/yard.rake
179
- - examples/prova/prova.html
180
- - examples/prova/flare.js
181
- - examples/directory_walker/print_files.rb
182
- - examples/directory_walker/directory_without_subdirectory.rb
183
- - examples/directory_walker/find_files.rb
184
- - examples/d3_treemap/d3.js
185
- - examples/d3_treemap/style.css
186
- - examples/d3_treemap/d3_treemap.html
187
- - examples/d3_treemap/prova.html
188
- - examples/d3_treemap/d3_tree_rb_output.json
189
- - examples/d3_treemap/d3.layout.js
190
- - examples/prova1/prova1.html
180
+ - examples/d3js_layout_partition/data.js
181
+ - examples/d3js_layout_partition/style.css
182
+ - examples/d3js_layout_partition/index.html
183
+ - examples/d3js_layout_partition/run.sh
184
+ - examples/ruby_examples/print_files.rb
185
+ - examples/ruby_examples/find_directory_without_subdirectory.rb
186
+ - examples/ruby_examples/find_files.rb
191
187
  - examples/protovis/treevisitor.png
192
188
  - examples/protovis/protovis-r3.2.js
193
189
  - examples/protovis/index.html
194
190
  - examples/protovis/treevisitor.js
195
191
  - examples/protovis/directory_to_json_visitor.rb
192
+ - examples/d3js_layout_treemap/data.js
193
+ - examples/d3js_layout_treemap/style.css
194
+ - examples/d3js_layout_treemap/index.html
195
+ - examples/d3js_layout_treemap/run.sh
196
196
  - spec/spec_helper.rb
197
197
  - spec/tree_rb/cli/cli_tree_sqlite_spec.rb
198
198
  - spec/tree_rb/cli/cli_tree_digest_spec.rb