-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCodex Usage.js
More file actions
99 lines (80 loc) · 2.59 KB
/
Copy pathCodex Usage.js
File metadata and controls
99 lines (80 loc) · 2.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
// Variables used by Scriptable.
// These must be at the very top of the file. Do not edit.
// icon-color: deep-brown; icon-glyph: magic;
/*
Codex Limits Lock Screen Widget for Scriptable.
Displays percentage REMAINING for the 5-hour and weekly Codex windows.
Recommended widget family: accessoryRectangular.
*/
const FILE_NAME = "codex-limits.json";
const REFRESH_MINUTES = 15;
const fm = FileManager.iCloud();
const filePath = fm.joinPath(fm.documentsDirectory(), FILE_NAME);
let data = null;
let errorMessage = null;
try {
if (!fm.fileExists(filePath)) {
throw new Error(`Missing ${FILE_NAME}`);
}
await fm.downloadFileFromiCloud(filePath);
data = JSON.parse(fm.readString(filePath));
if (
typeof data.fiveHourPercent !== "number" ||
typeof data.weeklyPercent !== "number"
) {
throw new Error("Invalid data file");
}
} catch (error) {
errorMessage = String(error.message || error);
}
const widget = new ListWidget();
widget.setPadding(0, 2, 0, 2);
widget.refreshAfterDate = new Date(Date.now() + REFRESH_MINUTES * 60 * 1000);
if (errorMessage) {
addHeader();
addTerminalLine("DATA UNAVAILABLE", 7);
} else {
addHeader();
addUsageRow("05H", data.fiveHourPercent, formatResetTime(data.fiveHourResetsAt));
addUsageRow("07D", data.weeklyPercent, formatResetDate(data.weeklyResetsAt));
}
function addHeader() {
const title = widget.addText(">> CODEX");
title.font = Font.semiboldMonospacedSystemFont(15);
title.lineLimit = 1;
title.minimumScaleFactor = 1;
}
function addUsageRow(label, percent, reset) {
const resetSuffix = reset ? ` ${reset}` : "";
addTerminalLine(`${label} ${formatPercent(percent)}${resetSuffix}`, 7);
}
function addTerminalLine(value, size) {
const text = widget.addText(value);
text.font = Font.semiboldMonospacedSystemFont(16);
text.lineLimit = 1;
text.minimumScaleFactor = 1;
}
function normalizedPercent(value) {
return Math.round(Math.max(0, Math.min(100, value)));
}
function formatPercent(value) {
return `${String(normalizedPercent(value)).padStart(3, " ")}%`;
}
function formatResetTime(timestamp) {
if (typeof timestamp !== "number") return "";
const date = new Date(timestamp * 1000);
return `${pad2(date.getHours())}:${pad2(date.getMinutes())}`;
}
function formatResetDate(timestamp) {
if (typeof timestamp !== "number") return "";
const date = new Date(timestamp * 1000);
return `${pad2(date.getDate())}.${pad2(date.getMonth() + 1)}`;
}
function pad2(value) {
return String(value).padStart(2, "0");
}
Script.setWidget(widget);
if (config.runsInApp) {
await widget.presentAccessoryRectangular();
}
Script.complete();