Add the possibility to put the name and the size on the same line in ASCII mode
[pysize.git] / pysize / ui / ascii / ui_ascii.py
blobb3dc2904b3f1910c2b9b1f724c48fb950e508073
1 # This program is free software; you can redistribute it and/or modify
2 # it under the terms of the GNU General Public License as published by
3 # the Free Software Foundation; either version 2 of the License, or
4 # (at your option) any later version.
6 # This program is distributed in the hope that it will be useful,
7 # but WITHOUT ANY WARRANTY; without even the implied warranty of
8 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9 # GNU Library General Public License for more details.
11 # You should have received a copy of the GNU General Public License
12 # along with this program; if not, write to the Free Software
13 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
15 # See the COPYING file for license information.
17 # Copyright (c) 2006 Guillaume Chazarain <guichaz@yahoo.fr>
19 import math
20 import sys
22 from pysize.core.pysize_fs_tree import pysize_tree
23 from pysize.core.compute_size import size_observable
24 from pysize.ui.utils import human_unit, min_size_to_consider, update_progress
25 from pysize.ui.utils import sanitize_string
26 from pysize.ui.char_matrix import CharMatrix, HLINE_START, HLINE, HLINE_END
27 from pysize.ui.char_matrix import VLINE_START, VLINE, VLINE_END
28 from pysize.ui.ascii.terminal_size import terminal_size
30 MATRIX_TO_ASCII = {
31 0: ' ',
32 HLINE_START: '-', HLINE: '-', HLINE_END: '-',
33 VLINE_START: '|', VLINE: '|', VLINE_END: '|'
36 def _transform(char):
37 if isinstance(char, int):
38 return MATRIX_TO_ASCII.get(char, '+')
39 return char
41 def _draw_progress():
42 progress = update_progress()
43 if progress:
44 print '', progress, '\r',
45 sys.stdout.flush()
47 def run(options, args):
48 columns, lines = terminal_size()
49 lines -= 3 # Summary + Prompt + Last line
50 # An entry needs 2 lines
51 min_size = min_size_to_consider(options.min_size, lines / 2)
52 size_observable.add_observer(_draw_progress)
53 args = args or ['.']
54 tree = pysize_tree(args, options.max_depth, min_size, options)
55 if not tree.root:
56 sys.exit(1)
57 # The last entry needs an additional terminating line
58 total_lines = int(math.ceil(2.0 * tree.root.size /
59 tree.root.minimum_node_size())) + 1
60 matrix = CharMatrix(columns, total_lines, tree)
61 print '\n'.join([''.join(map(_transform, line)) for line in matrix.matrix])
62 print sanitize_string(tree.root.get_name()), human_unit(tree.root.size)