Trim script
This commit is contained in:
@@ -136,47 +136,12 @@ def format_window(name, window):
|
|||||||
return {
|
return {
|
||||||
"label": label,
|
"label": label,
|
||||||
"left": left,
|
"left": left,
|
||||||
"text": f"{left}% {label}",
|
|
||||||
"used": used,
|
"used": used,
|
||||||
"tooltip": tooltip,
|
"tooltip": tooltip,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
def credit_text(credits):
|
def output_window(data, source, selector):
|
||||||
if not isinstance(credits, dict) or not credits.get("has_credits"):
|
|
||||||
return None
|
|
||||||
if credits.get("unlimited"):
|
|
||||||
return "credits unlimited"
|
|
||||||
balance = credits.get("balance")
|
|
||||||
if balance is None:
|
|
||||||
return "credits enabled"
|
|
||||||
try:
|
|
||||||
return f"credits {round(float(balance))}"
|
|
||||||
except (TypeError, ValueError):
|
|
||||||
return f"credits {balance}"
|
|
||||||
|
|
||||||
|
|
||||||
def spend_control_text(spend_control):
|
|
||||||
limit = (spend_control or {}).get("individual_limit")
|
|
||||||
if not isinstance(limit, dict):
|
|
||||||
return None
|
|
||||||
|
|
||||||
remaining = limit.get("remaining_percent")
|
|
||||||
used = limit.get("used")
|
|
||||||
cap = limit.get("limit")
|
|
||||||
reset = reset_text(limit.get("reset_after_seconds"))
|
|
||||||
if not isinstance(remaining, int):
|
|
||||||
return None
|
|
||||||
|
|
||||||
text = f"monthly credits: {100 - remaining}% used"
|
|
||||||
if used and cap:
|
|
||||||
text += f" ({used}/{cap})"
|
|
||||||
if reset:
|
|
||||||
text += f", resets in {reset}"
|
|
||||||
return text
|
|
||||||
|
|
||||||
|
|
||||||
def selected_window_output(data, source, selector):
|
|
||||||
rate_limit = data.get("rate_limit") or {}
|
rate_limit = data.get("rate_limit") or {}
|
||||||
window = format_window(selector, rate_limit.get(f"{selector}_window"))
|
window = format_window(selector, rate_limit.get(f"{selector}_window"))
|
||||||
if not window:
|
if not window:
|
||||||
@@ -187,42 +152,12 @@ def selected_window_output(data, source, selector):
|
|||||||
waybar(f"{window['left']}% <span font-size=\"7pt\">{window['label']}</span>", klass, "\n".join(tooltip))
|
waybar(f"{window['left']}% <span font-size=\"7pt\">{window['label']}</span>", klass, "\n".join(tooltip))
|
||||||
|
|
||||||
|
|
||||||
def combined_output(data, source):
|
|
||||||
rate_limit = data.get("rate_limit") or {}
|
|
||||||
windows = [
|
|
||||||
format_window("primary", rate_limit.get("primary_window")),
|
|
||||||
format_window("secondary", rate_limit.get("secondary_window")),
|
|
||||||
]
|
|
||||||
windows = [window for window in windows if window]
|
|
||||||
|
|
||||||
parts = [window["text"] for window in windows]
|
|
||||||
credits = credit_text(data.get("credits"))
|
|
||||||
if credits:
|
|
||||||
parts.append(credits)
|
|
||||||
|
|
||||||
if not parts:
|
|
||||||
waybar("AI n/a", "warning", f"No displayable usage data\nsource: {source}")
|
|
||||||
|
|
||||||
worst_used = max(window["used"] for window in windows) if windows else 0
|
|
||||||
klass = usage_class(worst_used, rate_limit.get("limit_reached"))
|
|
||||||
|
|
||||||
tooltip = [window["tooltip"] for window in windows]
|
|
||||||
spend = spend_control_text(data.get("spend_control"))
|
|
||||||
if credits:
|
|
||||||
tooltip.append(credits)
|
|
||||||
if spend:
|
|
||||||
tooltip.append(spend)
|
|
||||||
tooltip.append(f"source: {source}")
|
|
||||||
|
|
||||||
waybar(f"AI {' '.join(parts)}", klass, "\n".join(tooltip))
|
|
||||||
|
|
||||||
|
|
||||||
def parse_args():
|
def parse_args():
|
||||||
parser = ArgumentParser(description="Waybar Codex rate-limit widget")
|
parser = ArgumentParser(description="Waybar Codex rate-limit widget")
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
"--window",
|
"--window",
|
||||||
choices=("primary", "secondary", "all"),
|
choices=("primary", "secondary"),
|
||||||
default="all",
|
required=True,
|
||||||
help="usage window to display",
|
help="usage window to display",
|
||||||
)
|
)
|
||||||
return parser.parse_args()
|
return parser.parse_args()
|
||||||
@@ -232,11 +167,7 @@ def main():
|
|||||||
args = parse_args()
|
args = parse_args()
|
||||||
token, account_id, source = load_auth()
|
token, account_id, source = load_auth()
|
||||||
data = fetch_usage(token, account_id)
|
data = fetch_usage(token, account_id)
|
||||||
|
output_window(data, source, args.window)
|
||||||
if args.window == "all":
|
|
||||||
combined_output(data, source)
|
|
||||||
else:
|
|
||||||
selected_window_output(data, source, args.window)
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
|||||||
Reference in New Issue
Block a user