Add function generator_printer
so it can be used outside.
This way, people who use _as_a_generator can get the same print statements as the normal version without rewriting it themselves.
This commit is contained in:
parent
f7e96975bf
commit
c87d38a36b
1 changed files with 15 additions and 14 deletions
|
@ -6,6 +6,19 @@ from . import common
|
|||
from . import tsdb
|
||||
|
||||
|
||||
def generator_printer(generator):
|
||||
for step in generator:
|
||||
newtext = '%ds, %dc' % (step['new_submissions'], step['new_comments'])
|
||||
totalnew = step['new_submissions'] + step['new_comments']
|
||||
status = '{now} +{new}'.format(now=common.human(common.get_now()), new=newtext)
|
||||
print(status, end='')
|
||||
if totalnew == 0 and common.log.level != common.logging.DEBUG:
|
||||
# Since there were no news, allow the next line to overwrite status
|
||||
print('\r', end='', flush=True)
|
||||
else:
|
||||
print()
|
||||
yield None
|
||||
|
||||
def livestream(
|
||||
subreddit=None,
|
||||
username=None,
|
||||
|
@ -36,19 +49,11 @@ def livestream(
|
|||
if as_a_generator:
|
||||
return generator
|
||||
|
||||
generator = generator_printer(generator)
|
||||
|
||||
while True:
|
||||
try:
|
||||
step = next(generator)
|
||||
newtext = '%ds, %dc' % (step['new_submissions'], step['new_comments'])
|
||||
totalnew = step['new_submissions'] + step['new_comments']
|
||||
status = '{now} +{new}'.format(now=common.human(common.get_now()), new=newtext)
|
||||
print(status, end='')
|
||||
if totalnew == 0 and common.log.level != common.logging.DEBUG:
|
||||
# Since there were no news, allow the next line to overwrite status
|
||||
print('\r', end='', flush=True)
|
||||
else:
|
||||
print()
|
||||
|
||||
if only_once:
|
||||
break
|
||||
time.sleep(sleepy)
|
||||
|
@ -153,10 +158,6 @@ def livestream_argparse(args):
|
|||
else:
|
||||
limit = int(args.limit)
|
||||
|
||||
if args.submissions is False and args.comments is False:
|
||||
args.submissions = True
|
||||
args.comments = True
|
||||
|
||||
return livestream(
|
||||
subreddit=args.subreddit,
|
||||
username=args.username,
|
||||
|
|
Loading…
Reference in a new issue