Skip to content

Commit

Permalink
feat: add status print to bitsrun
Browse files Browse the repository at this point in the history
  • Loading branch information
spencerwooo committed Feb 4, 2023
1 parent f7c9223 commit 9d0a383
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 5 deletions.
23 changes: 18 additions & 5 deletions bitsrun/cli.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import sys
from getpass import getpass
from pprint import pprint

import click
from rich import print_json

from bitsrun.config import get_config_paths, read_config
from bitsrun.user import User, get_login_status
from bitsrun.utils import print_status_table

# A hacky way to specify shared options for multiple click commands:
# https://stackoverflow.com/questions/40182157/shared-options-and-flags-between-commands
Expand Down Expand Up @@ -42,9 +43,20 @@ def config_paths():
@cli.command()
def status():
"""Check current network login status."""
status = get_login_status()
# TODO: Pretty print the status
pprint(status)
login_status = get_login_status()

if login_status.get("user_name"):
click.echo(
click.style("bitsrun: ", fg="green")
+ f"{login_status['user_name']} ({login_status['online_ip']}) is online"
)
print_status_table(login_status)

else:
click.echo(
click.style("bitsrun: ", fg="cyan")
+ f"{login_status['online_ip']} is offline"
)


@cli.command()
Expand Down Expand Up @@ -82,6 +94,7 @@ def do_action(action, username, password, verbose):
else:
ctx = click.get_current_context()
ctx.fail("No username or password provided")
sys.exit(1)

if action == "login":
resp = user.login()
Expand All @@ -96,7 +109,7 @@ def do_action(action, username, password, verbose):
# Output direct result of the API response if verbose
if verbose:
click.echo(f"{click.style('bitsrun:', fg='cyan')} Response from API:")
pprint(resp)
print_json(data=resp)

# Handle error from API response. When field `error` is not `ok`, then the
# login/logout action has likely failed. Hints are provided in the `error_msg`.
Expand Down
40 changes: 40 additions & 0 deletions bitsrun/utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,46 @@
import math
from base64 import b64encode

from humanize import naturaldelta, naturalsize
from rich import box
from rich.console import Console
from rich.table import Table

from bitsrun.models import LoginStatusRespType


def print_status_table(login_status: LoginStatusRespType) -> None:
"""Print the login status table to the console if logged in.
You should get something like this:
┌──────────────┬──────────────┬──────────────┬──────────────┐
│ Traffic Used │ Online Time │ User Balance │ Wallet │
├──────────────┼──────────────┼──────────────┼──────────────┤
│ 879.3 MiB │ 3 hours │ 10.00 │ 0.00 │
└──────────────┴──────────────┴──────────────┴──────────────┘
"""

if not login_status.get("user_name"):
return

table = Table(box=box.SQUARE)

table.add_column("Traffic Used", style="magenta", width=12)
table.add_column("Online Time", style="yellow", width=12)
table.add_column("User Balance", style="green", width=12)
table.add_column("Wallet", style="blue", width=12)

table.add_row(
naturalsize(login_status.get("sum_bytes", 0), binary=True), # type: ignore
naturaldelta(login_status.get("sum_seconds", 0)), # type: ignore
f"{login_status.get('user_balance', 0):0.2f}",
f"{login_status.get('wallet_balance', 0):0.2f}",
)

console = Console()
console.print(table)


def fkbase64(raw_s: str) -> str:
"""Encode string with a magic base64 mask"""
Expand Down

0 comments on commit 9d0a383

Please sign in to comment.