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
|
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(
|
def livestream(
|
||||||
subreddit=None,
|
subreddit=None,
|
||||||
username=None,
|
username=None,
|
||||||
|
@ -36,19 +49,11 @@ def livestream(
|
||||||
if as_a_generator:
|
if as_a_generator:
|
||||||
return generator
|
return generator
|
||||||
|
|
||||||
|
generator = generator_printer(generator)
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
try:
|
try:
|
||||||
step = next(generator)
|
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:
|
if only_once:
|
||||||
break
|
break
|
||||||
time.sleep(sleepy)
|
time.sleep(sleepy)
|
||||||
|
@ -153,10 +158,6 @@ def livestream_argparse(args):
|
||||||
else:
|
else:
|
||||||
limit = int(args.limit)
|
limit = int(args.limit)
|
||||||
|
|
||||||
if args.submissions is False and args.comments is False:
|
|
||||||
args.submissions = True
|
|
||||||
args.comments = True
|
|
||||||
|
|
||||||
return livestream(
|
return livestream(
|
||||||
subreddit=args.subreddit,
|
subreddit=args.subreddit,
|
||||||
username=args.username,
|
username=args.username,
|
||||||
|
|
Loading…
Reference in a new issue