import curses
import termios
from optparse import OptionParser
+from StringIO import StringIO
from BeautifulSoup import BeautifulSoup
#---------- pretty-printer for tables of pirate puzzle standings ----------
class StandingsTable:
- def __init__(self, use_puzzles=None, col_width=6, gap_every=5):
+ def __init__(self, f, use_puzzles=None, col_width=6, gap_every=5):
if use_puzzles is None:
if opts.ship_duty:
use_puzzles=duty_puzzles
else:
use_puzzles=puzzles
self._puzzles = use_puzzles
- self.s = ''
+ self.f = f
self._cw = col_width-1
self._gap_every = gap_every
self._linecount = 0
+ self._o = f.write
+
+ def _nl(self): self._o('\n')
def _pline(self, pirate, puzstrs, extra):
if (self._linecount > 0
and self._gap_every is not None
and not (self._linecount % self._gap_every)):
- self.s += '\n'
- self.s += ' %-*s' % (max(max_pirate_namelen, 14), pirate)
+ self._nl()
+ self._o(' %-*s' % (max(max_pirate_namelen, 14), pirate))
for v in puzstrs:
- self.s += ' %-*.*s' % (self._cw,self._cw, v)
+ self._o(' %-*.*s' % (self._cw,self._cw, v))
if extra:
- self.s += ' ' + extra
- self.s += '\n'
+ self._o(' ' + extra)
+ self._nl()
self._linecount += 1
def _puzstr(self, pi, puzzle):
self._pline(lhs, map(puzn_redact, self._puzzles), rhs)
self._linecount = 0
def literalline(self, line):
- self.s += line + '\n'
+ self._o(line)
+ self._nl()
self._linecount = 0
def pirate_dummy(self, name, standingstring, extra=None):
self._pline(name, standingstring * len(self._puzzles), extra)
puzstrs = [self._puzstr(pi,puz) for puz in self._puzzles]
self._pline(pi.name, puzstrs, extra)
- def results(self):
- return self.s
#---------- chat log parser ----------
def do_standings_crew_of(args, bu):
ci = prep_crew_of(args, bu, 60)
- tab = StandingsTable()
+ tab = StandingsTable(sys.stdout)
tab.headings()
for (rank, members) in ci.crew:
if not members: continue
for p in members:
pi = PirateInfo(p, random.randint(900,1800))
tab.pirate(pi)
- print tab.results()
class ProgressPrintPercentage:
def __init__(self, f=sys.stdout):
aboard = track.aboard(vn)
sort.lsort_pa(aboard)
- tbl = StandingsTable()
+ tbl_s = StringIO()
+ tbl = StandingsTable(tbl_s)
tbl.headings(' %d aboard' % len(aboard),
' sorted by '+sort.desc())
else:
tbl.pirate(pi, xs)
- s += tbl.results()
+ s += tbl_s.getvalue()
displayer.show(s)
+ tbl_s.close()
k = kreader.getch()
if k is None: