Answer the architecture pass's second round
This commit is contained in:
+20
-11
@@ -19,7 +19,7 @@ YELLOW = "\033[33m"
|
||||
@dataclasses.dataclass(frozen=True)
|
||||
class Limit:
|
||||
percent: float
|
||||
resets_at: float | None
|
||||
resets_at: datetime.datetime | None
|
||||
|
||||
|
||||
@dataclasses.dataclass(frozen=True)
|
||||
@@ -53,9 +53,18 @@ def text(raw: object, *path: str) -> str:
|
||||
return found if isinstance(found, str) else ""
|
||||
|
||||
|
||||
def instant(epoch: float | None) -> datetime.datetime | None:
|
||||
if epoch is None:
|
||||
return None
|
||||
try:
|
||||
return datetime.datetime.fromtimestamp(round(epoch), datetime.timezone.utc)
|
||||
except (OSError, OverflowError, ValueError):
|
||||
return None
|
||||
|
||||
|
||||
def limit(raw: object, window: str) -> Limit | None:
|
||||
percent = number(raw, "rate_limits", window, "used_percentage")
|
||||
return None if percent is None else Limit(percent, number(raw, "rate_limits", window, "resets_at"))
|
||||
return None if percent is None else Limit(percent, instant(number(raw, "rate_limits", window, "resets_at")))
|
||||
|
||||
|
||||
# The input's fields: https://code.claude.com/docs/en/statusline
|
||||
@@ -83,19 +92,16 @@ def humanize_tokens(count: int) -> str:
|
||||
return str(count)
|
||||
|
||||
|
||||
def reset_suffix(epoch: float, now: int, zone: datetime.tzinfo | None) -> str:
|
||||
try:
|
||||
at = datetime.datetime.fromtimestamp(round(epoch), zone)
|
||||
except (OSError, OverflowError, ValueError):
|
||||
return ""
|
||||
seconds = max(0, round(epoch) - now)
|
||||
def reset_suffix(resets_at: datetime.datetime, now: int, zone: datetime.tzinfo | None) -> str:
|
||||
at = resets_at.astimezone(zone)
|
||||
seconds = max(0, int(resets_at.timestamp()) - now)
|
||||
if seconds >= 86400:
|
||||
left = f"{seconds // 86400}d{seconds % 86400 // 3600}h"
|
||||
elif seconds >= 3600:
|
||||
left = f"{seconds // 3600}h{seconds % 3600 // 60}m"
|
||||
else:
|
||||
left = f"{seconds // 60}m"
|
||||
clock = at.strftime("%H:%M" if at.date() == datetime.datetime.fromtimestamp(now, zone).date() else "%a %H:%M")
|
||||
clock = at.strftime("%H:%M" if at.date() == datetime.datetime.fromtimestamp(now, datetime.timezone.utc).astimezone(zone).date() else "%a %H:%M")
|
||||
return f" {DIM}{left} left ({clock}){RESET}"
|
||||
|
||||
|
||||
@@ -144,9 +150,12 @@ def current_branch(where: str) -> str:
|
||||
|
||||
def main() -> str:
|
||||
try:
|
||||
status = parse(json.load(sys.stdin))
|
||||
raw = json.load(sys.stdin)
|
||||
except ValueError:
|
||||
return f"{RED}warning: statusline.py could not parse its input as JSON{RESET}"
|
||||
raw = None
|
||||
if not isinstance(raw, dict):
|
||||
return f"{RED}warning: statusline.py could not parse its input as a JSON object{RESET}"
|
||||
status = parse(raw)
|
||||
return render(status, current_branch(status.directory), os.environ.get("HOME", ""), int(time.time()), None)
|
||||
|
||||
|
||||
|
||||
@@ -77,7 +77,7 @@ class Render(unittest.TestCase):
|
||||
self.assertEqual(render({"rate_limits": {"seven_day": {"resets_at": 1e20, "used_percentage": 1}}}), f"{DIM}7d{RESET} {GREEN}1%{RESET}")
|
||||
|
||||
def test_counts_a_past_reset_as_zero_left(self):
|
||||
self.assertEqual(statusline.reset_suffix(NOW - 60, NOW, UTC), f" {DIM}0m left (11:59){RESET}")
|
||||
self.assertEqual(statusline.reset_suffix(datetime.datetime.fromtimestamp(NOW - 60, UTC), NOW, UTC), f" {DIM}0m left (11:59){RESET}")
|
||||
|
||||
|
||||
class Script(unittest.TestCase):
|
||||
@@ -88,9 +88,10 @@ class Script(unittest.TestCase):
|
||||
ran = self.run_script(json.dumps({"cwd": "/nonexistent/a", "model": {"display_name": "Opus"}}))
|
||||
self.assertEqual((ran.returncode, ran.stdout, ran.stderr), (0, SEP.join([f"{CYAN}~/a{RESET}", f"{DIM}Opus{RESET}"]), ""))
|
||||
|
||||
def test_warns_on_the_status_line_for_input_that_is_not_json(self):
|
||||
ran = self.run_script("not json")
|
||||
self.assertEqual((ran.returncode, ran.stdout, ran.stderr), (0, f"{RED}warning: statusline.py could not parse its input as JSON{RESET}", ""))
|
||||
def test_warns_on_the_status_line_for_input_that_is_not_a_json_object(self):
|
||||
for stdin in ("not json", "[]"):
|
||||
ran = self.run_script(stdin)
|
||||
self.assertEqual((ran.returncode, ran.stdout, ran.stderr), (0, f"{RED}warning: statusline.py could not parse its input as a JSON object{RESET}", ""))
|
||||
|
||||
def test_shows_the_branch_of_the_directory(self):
|
||||
with tempfile.TemporaryDirectory() as checkout:
|
||||
|
||||
Reference in New Issue
Block a user