-lines = itip.lines
-if args.l:
- l = args.l.upper()
-else:
- l = None
-if args.s:
- s = args.s.decode('UTF-8')
-else:
- s = ''
-
-if l and l in lines:
- stations = itip.get_stations(l)
- for key in stations.keys():
- if not s:
- print '* %s:' % key
- for station in stations[key]:
- if s:
- if s.startswith(station[0]) or station[0].startswith(s):
- # FIXME
- 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'
+
+if args.line:
+ # Convert line name to uppercase (e.g. 'u4' -> 'U4')
+ args.line = args.line.upper()
+
+if args.station:
+ args.station = args.station.decode('utf-8')
+
+if args.line in itip.lines:
+ ITEM_WIDTH = 33
+ ITEM_SPACING = 4
+
+ # FIXME: change get_stations() to return (headers, stations) directly
+ stations = sorted(itip.get_stations(args.line).items())
+ headers, stations = zip(*stations)
+
+ maxlength = max(len(stops) for stops in stations)
+ for stops in stations:
+ # Pad station list with empty items for printing, so that
+ # different-sized lists aren't truncated (with zip below)
+ stops.extend([('', '')] * (maxlength - len(stops)))
+
+ stations_table = zip(*stations)
+ fmt = '%%-%ds' % ITEM_WIDTH
+ spacer = ' ' * ITEM_SPACING
+
+ print
+ print spacer, spacer.join(inblue(fmt % ('Richtung %s' % name))
+ for name in headers)
+ print spacer, spacer.join('-' * ITEM_WIDTH for name in headers)
+
+ def match_station(query, station):
+ return query and station and (query.lower() in station.lower())
+
+ for row in stations_table:
+ print spacer, spacer.join(ingreen(fmt % name)
+ if match_station(args.station, name)
+ else fmt % name
+ for name, url in row)
+ print
+
+ # Get matching stations
+ stations = zip(headers, stations)
+ details = [(direction, name, url) for direction, stops in stations
+ for name, url in stops if match_station(args.station, name)]
+
+ # User entered a station, but no matches were found
+ if args.station and not details:
+ print inred('No station matched your query.')
+ print
+
+ # Format a departure time (in minutes from now) for display
+ def format_departure(departure):
+ minutes = departure['time']
+ if type(minutes) == time:
+ return inblue(minutes.strftime('%H:%M'))
+ elif minutes == 0:
+ return inred('now')
+ elif minutes == 1:
+ return inblue('1') + ' min'