Skip to content

Commit

Permalink
write-entities: nicer progress status
Browse files Browse the repository at this point in the history
This makes two changes to make a progress status a bit nicer:

- Print the number with thousands separator, e.g. "150_000" instead of
  150000".

- For interactive terminals it will also replace the current line rather
  than printing a new one. "\r" moves the cursor to the first column,
  and "\x1b[K" clears the line. This works on all current terminals (
  where "current" means "since the 90s").
  • Loading branch information
arp242 authored and stchris committed Aug 1, 2024
1 parent 12fd603 commit dfd37b7
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion alephclient/cli.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import json
import click
import logging
import sys
from pathlib import Path

from alephclient import settings
Expand Down Expand Up @@ -296,7 +297,10 @@ def read_json_stream(stream):
return
count += 1
if count % chunksize == 0:
log.info("[%s] Bulk load entities: %s...", foreign_id, count)
if sys.stdout.isatty():
print(f"\r\x1b[K[{foreign_id}] Bulk load entities: {count:_}...", end='')
else:
log.info(f"[{foreign_id}] Bulk load entities: {count:_}...")
yield json.loads(line)

api.write_entities(
Expand All @@ -312,6 +316,9 @@ def read_json_stream(stream):
raise click.ClickException(exc.message)
except BrokenPipeError:
raise click.Abort()
finally:
if sys.stdout.isatty():
print()


@cli.command("stream-entities")
Expand Down

0 comments on commit dfd37b7

Please sign in to comment.