onenote-cli 0.2.1-win32-arm64 → 0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,220 @@
1
+ import React, { useState, useEffect } from "react";
2
+ import { Box, Text, useApp } from "ink";
3
+
4
+ export type SyncEvent =
5
+ | { type: "account"; email: string; index: number; total: number }
6
+ | { type: "total"; total: number; notebooks: number }
7
+ | { type: "section"; index: number; notebook: string; section: string; size?: string }
8
+ | { type: "retag"; index: number; notebook: string; section: string }
9
+ | { type: "tags"; count: number }
10
+ | { type: "hint"; text: string }
11
+ | { type: "done"; notebook: string; section: string; pages: number; status: "ok" | "failed" | "skipped" }
12
+ | { type: "complete"; downloaded: number; retagged: number; skipped: number };
13
+
14
+ interface State {
15
+ total: number;
16
+ notebooks: number;
17
+ done: number;
18
+ downloaded: number;
19
+ retagged: number;
20
+ skipped: number;
21
+ account: { email: string; index: number; total: number } | null;
22
+ current: { notebook: string; section: string; size?: string; retag?: boolean } | null;
23
+ log: string[];
24
+ complete: boolean;
25
+ startMs: number;
26
+ }
27
+
28
+ const BAR_WIDTH = 24;
29
+
30
+ function bar(ratio: number): string {
31
+ const filled = Math.round(ratio * BAR_WIDTH);
32
+ return "█".repeat(filled) + "░".repeat(BAR_WIDTH - filled);
33
+ }
34
+
35
+ function fmtTime(ms: number): string {
36
+ const s = Math.round(ms / 1000);
37
+ if (s < 60) return `${s}s`;
38
+ return `${Math.floor(s / 60)}m${s % 60}s`;
39
+ }
40
+
41
+ function SyncUI({ events }: { events: AsyncIterable<SyncEvent> }) {
42
+ const { exit } = useApp();
43
+ const [state, setState] = useState<State>({
44
+ total: 0, notebooks: 0, done: 0, downloaded: 0, retagged: 0, skipped: 0,
45
+ account: null, current: null, log: [], complete: false, startMs: Date.now(),
46
+ });
47
+
48
+ useEffect(() => {
49
+ (async () => {
50
+ for await (const ev of events) {
51
+ setState((s) => {
52
+ switch (ev.type) {
53
+ case "account":
54
+ return { ...s, account: { email: ev.email, index: ev.index, total: ev.total }, total: 0, done: 0 };
55
+ case "total":
56
+ return { ...s, total: ev.total, notebooks: ev.notebooks };
57
+ case "section":
58
+ return { ...s, current: { notebook: ev.notebook, section: ev.section, size: ev.size } };
59
+ case "retag":
60
+ return { ...s, current: { notebook: ev.notebook, section: ev.section, retag: true } };
61
+ case "tags": {
62
+ const last = s.log[s.log.length - 1];
63
+ const next = last?.startsWith(" tags: ")
64
+ ? [...s.log.slice(0, -1), ` tags: ${ev.count} pages tagged`]
65
+ : [...s.log, ` tags: ${ev.count} pages tagged`];
66
+ return { ...s, log: next.slice(-6) };
67
+ }
68
+ case "done": {
69
+ const icon = ev.status === "ok" ? "✓" : ev.status === "failed" ? "✗" : "–";
70
+ const line = `${icon} ${ev.notebook}/${ev.section} (${ev.pages} pages)`;
71
+ return {
72
+ ...s,
73
+ done: s.done + 1,
74
+ downloaded: s.downloaded + (ev.status === "ok" ? 1 : 0),
75
+ skipped: s.skipped + (ev.status === "skipped" ? 1 : 0),
76
+ log: [...s.log, line].slice(-6),
77
+ current: null,
78
+ };
79
+ }
80
+ case "hint":
81
+ return { ...s, log: [...s.log, `💡 ${ev.text}`].slice(-6) };
82
+ case "complete":
83
+ return { ...s, complete: true, downloaded: ev.downloaded, retagged: ev.retagged, skipped: ev.skipped };
84
+ }
85
+ });
86
+ }
87
+ exit();
88
+ })();
89
+ }, []);
90
+
91
+ const elapsed = Date.now() - state.startMs;
92
+ const ratio = state.total > 0 ? state.done / state.total : 0;
93
+ const speed = state.done > 0 ? state.done / (elapsed / 1000) : 0;
94
+ const eta = speed > 0 && !state.complete ? (state.total - state.done) / speed : 0;
95
+ const pct = Math.round(ratio * 100);
96
+
97
+ return (
98
+ <Box flexDirection="column" paddingTop={1}>
99
+ {/* Account */}
100
+ {state.account && (
101
+ <Box>
102
+ <Text bold color="magenta">Account [{state.account.index}/{state.account.total}]: </Text>
103
+ <Text>{state.account.email}</Text>
104
+ </Box>
105
+ )}
106
+ {/* Header */}
107
+ <Box>
108
+ <Text bold color="cyan">Syncing OneNote </Text>
109
+ <Text color="green">{bar(ratio)}</Text>
110
+ <Text> </Text>
111
+ <Text bold>{state.done}/{state.total}</Text>
112
+ <Text color="gray"> {pct}%</Text>
113
+ </Box>
114
+
115
+ {/* Current section */}
116
+ {state.current && (
117
+ <Box marginTop={1}>
118
+ <Text color="yellow"> ⟳ </Text>
119
+ <Text>{state.current.notebook}/</Text>
120
+ <Text bold>{state.current.section}</Text>
121
+ {state.current.size && <Text color="gray"> {state.current.size}</Text>}
122
+ {state.current.retag && <Text color="blue"> (retag)</Text>}
123
+ </Box>
124
+ )}
125
+
126
+ {/* Speed / ETA */}
127
+ <Box marginTop={state.current ? 0 : 1}>
128
+ <Text color="gray"> </Text>
129
+ <Text color="gray">speed: </Text>
130
+ <Text>{speed > 0 ? `${speed.toFixed(1)} sec/s` : "–"}</Text>
131
+ <Text color="gray"> elapsed: </Text>
132
+ <Text>{fmtTime(elapsed)}</Text>
133
+ {!state.complete && eta > 0 && (
134
+ <>
135
+ <Text color="gray"> ETA: </Text>
136
+ <Text color="yellow">{fmtTime(eta * 1000)}</Text>
137
+ </>
138
+ )}
139
+ </Box>
140
+
141
+ {/* Recent log */}
142
+ <Box flexDirection="column" marginTop={1}>
143
+ {state.log.map((line, i) => (
144
+ <Text key={i} color={line.startsWith("✗") ? "red" : line.startsWith("✓") ? "green" : line.startsWith("💡") ? "cyan" : "gray"}>
145
+ {" "}{line}
146
+ </Text>
147
+ ))}
148
+ </Box>
149
+
150
+ {/* Complete summary */}
151
+ {state.complete && (
152
+ <Box marginTop={1}>
153
+ <Text bold color="green">✓ Sync complete </Text>
154
+ <Text color="gray">
155
+ {state.downloaded} downloaded {state.retagged} retagged {state.skipped} up-to-date
156
+ {" in "}{fmtTime(elapsed)}
157
+ </Text>
158
+ </Box>
159
+ )}
160
+ </Box>
161
+ );
162
+ }
163
+
164
+ export async function runSyncUI(
165
+ syncFn: (emit: (ev: SyncEvent) => void) => Promise<void>
166
+ ): Promise<void> {
167
+ // Fall back to plain text if not a TTY
168
+ if (!process.stdout.isTTY) {
169
+ const plain = { startMs: Date.now(), total: 0, done: 0 };
170
+ const emit = (ev: SyncEvent) => {
171
+ if (ev.type === "account") { plain.startMs = Date.now(); plain.done = 0; plain.total = 0; process.stdout.write(`\n=== [${ev.index}/${ev.total}] ${ev.email} ===\n`); }
172
+ if (ev.type === "total") { plain.total = ev.total; process.stdout.write(`${ev.total} sections across ${ev.notebooks} notebooks\n`); }
173
+ if (ev.type === "section") process.stdout.write(` [${ev.index}] ${ev.notebook}/${ev.section}${ev.size ? ` (${ev.size})` : ""}\n`);
174
+ if (ev.type === "retag") process.stdout.write(` [${ev.index}] ${ev.notebook}/${ev.section} (retag only)\n`);
175
+ if (ev.type === "done") {
176
+ plain.done++;
177
+ if (ev.status === "ok") process.stdout.write(` [ok] ${ev.section} (${ev.pages} pages)\n`);
178
+ else if (ev.status === "failed") process.stdout.write(` [failed] ${ev.section}\n`);
179
+ if (plain.total > 0) {
180
+ const elapsed = Date.now() - plain.startMs;
181
+ const speed = plain.done / (elapsed / 1000);
182
+ const remaining = plain.total - plain.done;
183
+ const eta = speed > 0 && remaining > 0 ? remaining / speed : 0;
184
+ const pct = Math.round((plain.done / plain.total) * 100);
185
+ const etaStr = eta > 0 ? ` ETA: ${fmtTime(eta * 1000)}` : "";
186
+ process.stdout.write(` [${plain.done}/${plain.total}] ${pct}% elapsed: ${fmtTime(elapsed)}${etaStr}\n`);
187
+ }
188
+ }
189
+ if (ev.type === "hint") process.stdout.write(` tip: ${ev.text}\n`);
190
+ if (ev.type === "complete") process.stdout.write(`Sync complete. ${ev.downloaded} downloaded, ${ev.retagged} retagged, ${ev.skipped} up-to-date.\n`);
191
+ };
192
+ await syncFn(emit);
193
+ return;
194
+ }
195
+
196
+ const queue: SyncEvent[] = [];
197
+ const state = { resolve: null as ((v: void) => void) | null, done: false };
198
+
199
+ const emit = (ev: SyncEvent) => {
200
+ queue.push(ev);
201
+ state.resolve?.(undefined);
202
+ state.resolve = null;
203
+ };
204
+
205
+ async function* makeStream(): AsyncIterable<SyncEvent> {
206
+ while (!state.done || queue.length > 0) {
207
+ while (queue.length > 0) yield queue.shift()!;
208
+ if (!state.done) await new Promise<void>((r) => { state.resolve = r; });
209
+ }
210
+ }
211
+
212
+ const { render } = await import("ink");
213
+ const stream = makeStream();
214
+ const { waitUntilExit } = render(<SyncUI events={stream} />);
215
+
216
+ await syncFn(emit);
217
+ state.done = true;
218
+ state.resolve?.(undefined);
219
+ await waitUntilExit();
220
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,16 @@
1
+ {
2
+ "compilerOptions": {
3
+ "target": "ESNext",
4
+ "module": "ESNext",
5
+ "moduleResolution": "bundler",
6
+ "strict": true,
7
+ "noEmit": true,
8
+ "esModuleInterop": true,
9
+ "skipLibCheck": true,
10
+ "resolveJsonModule": true,
11
+ "isolatedModules": true,
12
+ "jsx": "react-jsx",
13
+ "types": ["bun-types"]
14
+ },
15
+ "include": ["src"]
16
+ }
Binary file