Update e-mail address
[pysize.git] / pysize / ui / ascii / ui_ascii.py
blob975437c03ca31bc9e44db8c67f41cb67b6c248dd
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, 2007, 2008 Guillaume Chazarain <guichaz@gmail.com>
19 import math
20 import os
21 import sys
23 from pysize.core.pysize_fs_tree import pysize_tree
24 from pysize.core.compute_size import size_observable
25 from pysize.ui.utils import human_unit, update_progress
26 from pysize.ui.utils import sanitize_string
27 from pysize.ui.char_matrix import CharMatrix, HLINE_START, HLINE, HLINE_END
28 from pysize.ui.char_matrix import VLINE_START, VLINE, VLINE_END
29 from pysize.ui.ascii.terminal_size import terminal_size
31 MATRIX_TO_ASCII = {
32 0: ' ',
33 HLINE_START: '-', HLINE: '-', HLINE_END: '-',
34 VLINE_START: '|', VLINE: '|', VLINE_END: '|'
37 def _transform(char):
38 if isinstance(char, int):
39 return MATRIX_TO_ASCII.get(char, '+')
40 return char
42 def _draw_progress():
43 progress = update_progress()
44 if progress:
45 print '', progress, '\r',
46 sys.stdout.flush()
48 def run(options, args):
49 columns, lines = terminal_size()
50 lines -= 3 # Summary + Prompt + Last line
51 size_observable.add_observer(_draw_progress)
52 args = args or [os.getcwd()]
53 # An entry needs 2 lines
54 tree = pysize_tree(args, options.max_depth, 2.0 / lines, 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 max(1, 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)