From: Avery Pennarun Date: Fri, 23 Apr 2010 20:33:10 +0000 (-0400) Subject: cmd/ftp: 'ls' command should print filenames in columns. X-Git-Tag: bup-0.14~2 X-Git-Url: https://arthur.barton.de/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=46880edf5b8bd47e8ae635a213977c0d40e060a9;p=bup.git cmd/ftp: 'ls' command should print filenames in columns. We use the columnate() function from main.py for this, now moved into helpers.py. --- diff --git a/cmd/ftp-cmd.py b/cmd/ftp-cmd.py index f7b0492..52391c6 100755 --- a/cmd/ftp-cmd.py +++ b/cmd/ftp-cmd.py @@ -3,22 +3,24 @@ import sys, os, re, stat, readline, fnmatch from bup import options, git, shquote, vfs from bup.helpers import * -def print_node(text, n): +def node_name(text, n): if stat.S_ISDIR(n.mode): - print '%s/' % text + return '%s/' % text elif stat.S_ISLNK(n.mode): - print '%s@' % text + return '%s@' % text else: - print '%s' % text + return '%s' % text def do_ls(path, n): + l = [] if stat.S_ISDIR(n.mode): for sub in n: - print_node(sub.name, sub) + l.append(node_name(sub.name, sub)) else: - print_node(path, n) - + l.append(node_name(path, n)) + print columnate(l, '') + def write_to_file(inf, outf): for blob in chunkyreader(inf): diff --git a/lib/bup/helpers.py b/lib/bup/helpers.py index 156486a..c92c09d 100644 --- a/lib/bup/helpers.py +++ b/lib/bup/helpers.py @@ -297,6 +297,25 @@ def handle_ctrl_c(): sys.excepthook = newhook +def columnate(l, prefix): + l = l[:] + clen = max(len(s) for s in l) + ncols = (78 - len(prefix)) / (clen + 2) + if ncols <= 1: + ncols = 1 + clen = 0 + cols = [] + while len(l) % ncols: + l.append('') + rows = len(l)/ncols + for s in range(0, len(l), rows): + cols.append(l[s:s+rows]) + out = '' + for row in zip(*cols): + out += prefix + ''.join(('%-*s' % (clen+2, s)) for s in row) + '\n' + return out + + # hashlib is only available in python 2.5 or higher, but the 'sha' module # produces a DeprecationWarning in python 2.6 or higher. We want to support # python 2.4 and above without any stupid warnings, so let's try using hashlib diff --git a/main.py b/main.py index 0ac7abc..5904018 100755 --- a/main.py +++ b/main.py @@ -24,25 +24,6 @@ os.environ['BUP_MAIN_EXE'] = os.path.abspath(exe) from bup.helpers import * -def columnate(l, prefix): - l = l[:] - clen = max(len(s) for s in l) - ncols = (78 - len(prefix)) / (clen + 2) - if ncols <= 1: - ncols = 1 - clen = 0 - cols = [] - while len(l) % ncols: - l.append('') - rows = len(l)/ncols - for s in range(0, len(l), rows): - cols.append(l[s:s+rows]) - out = '' - for row in zip(*cols): - out += prefix + ''.join(('%-*s' % (clen+2, s)) for s in row) + '\n' - return out - - def usage(): log('Usage: bup [-?|--help] [-d=BUP_DIR|--bup-dir=BUP_DIR] COMMAND [ARGS]' + '\n\n')