Pretty print a list of lines for realtime
authorThomas Perl <m@thp.io>
Wed, 28 Sep 2011 19:58:30 +0000 (21:58 +0200)
committerThomas Perl <m@thp.io>
Wed, 28 Sep 2011 19:58:30 +0000 (21:58 +0200)
gotovienna/realtime.py
itip

index fc1730d..a5b3f51 100644 (file)
@@ -5,6 +5,7 @@ from urllib2 import urlopen
 from datetime import time
 import argparse
 import re
+import collections
 
 from gotovienna import defaults
 
@@ -108,3 +109,63 @@ class ITipParser:
 
         return dep
 
+
+UBAHN, TRAM, BUS, NIGHTLINE, OTHER = range(5)
+LINE_TYPE_NAMES = ['U-Bahn', 'Strassenbahn', 'Bus', 'Nightline', 'Andere']
+
+def get_line_sort_key(name):
+    """Return a sort key for a line name
+
+    >>> get_line_sort_key('U6')
+    ('U', 6)
+
+    >>> get_line_sort_key('D')
+    ('D', 0)
+
+    >>> get_line_sort_key('59A')
+    ('A', 59)
+    """
+    txt = ''.join(x for x in name if not x.isdigit())
+    num = ''.join(x for x in name if x.isdigit()) or '0'
+
+    return (txt, int(num))
+
+def get_line_type(name):
+    """Get the type of line for the given name
+
+    >>> get_line_type('U1')
+    UBAHN
+    >>> get_line_type('59A')
+    BUS
+    """
+    if name.isdigit():
+        return TRAM
+    elif name.endswith('A') or name.endswith('B') and name[1].isdigit():
+        return BUS
+    elif name.startswith('U'):
+        return UBAHN
+    elif name.startswith('N'):
+        return NIGHTLINE
+    elif name in ('D', 'O', 'VRT', 'WLB'):
+        return TRAM
+
+    return OTHER
+
+def categorize_lines(lines):
+    """Return a categorized version of a list of line names
+
+    >>> categorize_lines(['U4', 'U3', '59A'])
+    [('U-Bahn', ['U3', 'U4']), ('Bus', ['59A'])]
+    """
+    categorized_lines = collections.defaultdict(list)
+
+    for line in sorted(lines):
+        line_type = get_line_type(line)
+        categorized_lines[line_type].append(line)
+
+    for lines in categorized_lines.values():
+        lines.sort(key=get_line_sort_key)
+
+    return [(LINE_TYPE_NAMES[key], categorized_lines[key])
+            for key in sorted(categorized_lines)]
+
diff --git a/itip b/itip
index 5951cea..54ed263 100755 (executable)
--- a/itip
+++ b/itip
@@ -35,23 +35,21 @@ if l and l in lines:
                     print '* %s\n  %s .....' % (key, station[0]), itip.get_departures(station[1])
             else:
                 print '    %s' % station[0]
-
 elif not l:
-    line = {'U-Bahn': '|', 'Strassenbahn': '|', 'Bus': '|', 'Andere': '|', 'Nightline': '|'}
-    lines_sorted = lines.keys()
-    lines_sorted.sort()
-    for li in lines_sorted:
-        if li.isdigit():
-            type = 'Strassenbahn'
-        elif li.endswith('A') or li.endswith('B') and li[1].isdigit():
-            type = 'Bus'
-        elif li.startswith('U'):
-            type = 'U-Bahn'
-        elif li.startswith('N'):
-            type = 'Nightline'
-        else:
-            type = 'Andere'
-
-        line[type] += ' %s |' % li
-    for kv in line.items():
-        print "%s:\n%s" % kv
+    ITEMS_PER_LINE = 12
+    ITEM_WIDTH = 5
+    LINE_WIDTH = (ITEMS_PER_LINE*ITEM_WIDTH + ITEMS_PER_LINE)
+
+    print
+    for label, remaining in categorize_lines(lines.keys()):
+        prefix, fill, postfix = '|== ', '=', '==- -'
+        before, after = prefix+label+' ', postfix
+        padding = LINE_WIDTH - len(before+after)
+        print ''.join((before, fill*padding, after))
+
+        while remaining:
+            this_row = [remaining.pop(0) for _ in
+                    range(min(len(remaining), ITEMS_PER_LINE))]
+            print ' '.join(('%%%ds' % ITEM_WIDTH) % x for x in this_row)
+
+        print