]> arthur.barton.de Git - bup.git/commitdiff
cmd/ftp: 'ls' command should print filenames in columns.
authorAvery Pennarun <apenwarr@gmail.com>
Fri, 23 Apr 2010 20:33:10 +0000 (16:33 -0400)
committerAvery Pennarun <apenwarr@gmail.com>
Fri, 23 Apr 2010 20:33:42 +0000 (16:33 -0400)
We use the columnate() function from main.py for this, now moved into
helpers.py.

cmd/ftp-cmd.py
lib/bup/helpers.py
main.py

index f7b0492a31ace5686991ad1851dd1b2efd071a00..52391c6026b91ea28b4accc5e26437efe4aec7ed 100755 (executable)
@@ -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):
index 156486a0b960cc94243a97c87070bb5f2edc06ae..c92c09d813d5594e63e9f86244115921ab43cb54 100644 (file)
@@ -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 0ac7abc64857133117028602184e6fa17f4ea8f6..5904018c30854b80f84d87e00e688724d6038d4e 100755 (executable)
--- 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')