Class: TextOutput
- Inherits:
-
Object
- Object
- TextOutput
- Defined in:
- lib/text_output.rb
Overview
Copyright © 2009 Matteo Sasso
This file is part of durb.
durb is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
durb is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with durb. If not, see <www.gnu.org/licenses/>.
Constant Summary collapse
- DEFAULT_WIDTH =
78
Instance Method Summary collapse
- #align(left, right) ⇒ Object
- #calc_width(tree, indentation = 0) ⇒ Object
-
#initialize(trees, output) ⇒ TextOutput
constructor
A new instance of TextOutput.
- #print_all(size) ⇒ Object
- #print_node(node, size, indentation) ⇒ Object
- #print_tree(tree, size, indentation = 0) ⇒ Object
Constructor Details
#initialize(trees, output) ⇒ TextOutput
Returns a new instance of TextOutput.
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/text_output.rb', line 21 def initialize(trees, output) trees_width = trees.map{|t| calc_width(t)}.max @width = trees_width + ($options[:verbose] ? 47 : 11) @output = output if output.tty? begin tput = open("|tput cols") max_width = tput.gets.to_i rescue max_width = ENV["COLUMNS"] max_width = max_width ? max_width.to_i : DEFAULT_WIDTH end @width = [max_width, @width].min end @trees = trees end |
Instance Method Details
#align(left, right) ⇒ Object
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/text_output.rb', line 117 def align(left, right) right = "" if not right size = @width - (left.size + right.size + 4) if size <= 0 left = left[0..(size-4)] + '...' size = 0 end @output.print left, ' ' if size < 4 @output.print " " * size size = 0 end if not right.empty? y = 0 while size > 0 @output.print(((y + left.size) % 2 == 0) ? '.' : ' ') size -= 1 y += 1 end @output.print ' ', right end @output.print "\n" end |
#calc_width(tree, indentation = 0) ⇒ Object
112 113 114 115 |
# File 'lib/text_output.rb', line 112 def calc_width(tree, indentation = 0) (tree.shown_subdirs.map{|n| calc_width(n, indentation + 2)} + [(indentation == 0) ? tree.path_string.size : indentation + tree.name.size]).max end |
#print_all(size) ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/text_output.rb', line 38 def print_all(size) if $options[:verbose] # and not $options[:no_header] # The commented-out code is unneeded right now but may be necessary in the future heading1 = " SMALL DIRS AGGREG BIG DIRS " heading2 = "SIZE SIZE/# SIZE SIZE/# TOTAL" puts((" " * (@width - heading1.size - 2)) + heading1) puts((" " * (@width - heading2.size - 2)) + heading2) end @trees.each do |t| print_tree(t, size) end end |
#print_node(node, size, indentation) ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/text_output.rb', line 58 def print_node(node, size, indentation) if $options[:parsable] @output.puts($options[:output_fields].map do |f| case f when :path if $options[:output_fields].size == 1 node.path_string else node.path_string.gsub(":", "\\:") end when :self_size node.size when :small_dirs_size node.i_subsize when :aggregate_size node.i_subsize + node.size when :big_dirs_size node.s_subsize when :total_size node.s_subsize + node.i_subsize + node.size end end.join(':')) else if $options[:verbose] size_and_isize = (node.size > 0 and node.i_subsize > 0) tsize_and_ssize = (node.size + node.i_subsize > 0 and node.s_subsize > 0) size_text = "%4s %1s %9s %1s %4s %1s %9s %1s %4s %1s" % \ [ (node.size > 0) ? size_to_text(node.size) : nil, size_and_isize ? "+" : nil, (node.i_subsize > 0) ? "%4s/%-4s" % \ [size_to_text(node.i_subsize), size_to_text(node.i_dirs)] : nil, size_and_isize ? "=" : nil, (node.size + node.i_subsize > 0) ? size_to_text(node.size + node.i_subsize) : nil, tsize_and_ssize ? "+" : nil, (node.s_subsize > 0) ? "%4s/%-4s" % \ [size_to_text(node.s_subsize), size_to_text(node.s_dirs)] : nil, tsize_and_ssize ? "=" : nil, (node.size + node.i_subsize + node.s_subsize > 0) ? size_to_text(node.size + node.i_subsize + node.s_subsize) : nil, node.precise? ? nil : "*", ] else total_size = node.size + node.i_subsize size_text = "%4s %1s" % \ [ total_size < size ? nil : size_to_text(total_size), node.precise? ? nil : "*", ] end align((' ' * indentation) + (indentation == 0 ? node.path_string : node.name), size_text) end end |
#print_tree(tree, size, indentation = 0) ⇒ Object
51 52 53 54 55 56 |
# File 'lib/text_output.rb', line 51 def print_tree(tree, size, indentation = 0) print_node(tree, size, indentation) tree.shown_subdirs.each do |n| print_tree(n, size, indentation + 2) end end |