@kiosko/sdk 0.1.0 → 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.
- package/dist/context.d.ts +1 -0
- package/dist/context.js +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/plugin.d.ts +107 -2
- package/dist/system.d.ts +35 -0
- package/dist/system.js +80 -0
- package/package.json +1 -1
- package/src/context.ts +2 -1
- package/src/index.ts +1 -0
- package/src/plugin.ts +102 -2
- package/src/system.ts +87 -0
package/dist/context.d.ts
CHANGED
package/dist/context.js
CHANGED
|
@@ -17,6 +17,7 @@ export function createBaseContext(o) {
|
|
|
17
17
|
fmt: createFormatters({ locale, timeFormat: o.timeFormat ?? 'auto', now: o.now }),
|
|
18
18
|
log: o.log ?? (() => undefined),
|
|
19
19
|
dataDir: o.dataDir ?? '',
|
|
20
|
+
cacheDir: o.cacheDir ?? o.dataDir ?? '',
|
|
20
21
|
...(o.location ? { location: o.location } : {}),
|
|
21
22
|
workday: o.workday ?? DEFAULT_WORKDAY,
|
|
22
23
|
};
|
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
package/dist/plugin.d.ts
CHANGED
|
@@ -72,8 +72,15 @@ export interface BaseContext {
|
|
|
72
72
|
*/
|
|
73
73
|
fmt: Formatters;
|
|
74
74
|
log: Logger;
|
|
75
|
-
/** Where the plugin may keep files (
|
|
75
|
+
/** Where the plugin may keep files that must survive a reboot (settings, history). Created on demand. */
|
|
76
76
|
dataDir: string;
|
|
77
|
+
/**
|
|
78
|
+
* Where the plugin keeps what can be fetched again (downloads, feed copies,
|
|
79
|
+
* photos, tiles). It is RAM (tmpfs), so the constant rewriting never wears
|
|
80
|
+
* the SSD, and it is empty after a reboot. Created on demand. Falls back to
|
|
81
|
+
* `dataDir` where there is no tmpfs. Use this, not `dataDir`, for caches.
|
|
82
|
+
*/
|
|
83
|
+
cacheDir: string;
|
|
77
84
|
/**
|
|
78
85
|
* The user's place, if set in Settings (weather, sun, earthquakes…). Use it
|
|
79
86
|
* by default; offer a place of your own only where a second one makes sense
|
|
@@ -120,9 +127,84 @@ export interface RenderContext<O, D> extends BaseContext, StateContext<O, D> {
|
|
|
120
127
|
canvas: CanvasApi;
|
|
121
128
|
/** The widget is drawn in the editor (gallery thumbnail or live preview) and not on the panel. */
|
|
122
129
|
preview: boolean;
|
|
123
|
-
/**
|
|
130
|
+
/**
|
|
131
|
+
* Set while an animation of this widget is being painted (see
|
|
132
|
+
* `WidgetDefinition.animation`): which frame this call must draw. Without
|
|
133
|
+
* it, paint the widget at rest. The first and the last frame of an
|
|
134
|
+
* animation should look exactly like the still one.
|
|
135
|
+
*/
|
|
136
|
+
anim?: AnimationFrame;
|
|
137
|
+
/** Index of the frame being painted while an animation runs (`anim.i`). */
|
|
124
138
|
frame?: number;
|
|
125
139
|
}
|
|
140
|
+
/**
|
|
141
|
+
* A short animation a widget asks the runtime to play inside its own tile: a
|
|
142
|
+
* cat's tail flick, a tile sliding in. The runtime plans it (when, one at a
|
|
143
|
+
* time per screen), paints every frame by calling `render` with `ctx.anim`
|
|
144
|
+
* before playing, sends them at `fps` (never above the screen's `maxFps`) and
|
|
145
|
+
* finishes with a real still repaint.
|
|
146
|
+
*/
|
|
147
|
+
export interface AnimationPlan {
|
|
148
|
+
/** What it is (`tail`, `walk`…): for the log and for `render`, which receives it in `ctx.anim.plan`. */
|
|
149
|
+
id: string;
|
|
150
|
+
/** Frames, counting the first and the last (those are the still picture and are not sent). At least 3. */
|
|
151
|
+
frames: number;
|
|
152
|
+
/** Frames per second it was designed for. The screen's `maxFps` caps it. */
|
|
153
|
+
fps: number;
|
|
154
|
+
/** Whatever `render` needs to draw it (who does what). Plain data: it is not interpreted. */
|
|
155
|
+
params?: unknown;
|
|
156
|
+
/**
|
|
157
|
+
* `false`: new data doesn't cut it short (something that must be seen to
|
|
158
|
+
* the end). Default `true`: a data change cancels it and the screen is
|
|
159
|
+
* repainted with the new data.
|
|
160
|
+
*/
|
|
161
|
+
interruptible?: boolean;
|
|
162
|
+
/** Stay still at `at` (0..1 of the animation) for `ms` (a long task being shown), or until interrupted. */
|
|
163
|
+
hold?: {
|
|
164
|
+
at: number;
|
|
165
|
+
ms: number;
|
|
166
|
+
};
|
|
167
|
+
}
|
|
168
|
+
/** The frame `render` must paint (see `RenderContext.anim`). */
|
|
169
|
+
export interface AnimationFrame {
|
|
170
|
+
plan: AnimationPlan;
|
|
171
|
+
/** Frame number, 0..plan.frames-1. */
|
|
172
|
+
i: number;
|
|
173
|
+
/** Progress, 0..1: `i / (frames - 1)`. 0 and 1 are the still picture. */
|
|
174
|
+
t: number;
|
|
175
|
+
}
|
|
176
|
+
/** What `WidgetDefinition.animationEnded` is told. */
|
|
177
|
+
export interface AnimationEndContext<O, D> extends BaseContext {
|
|
178
|
+
options: O;
|
|
179
|
+
/** The animation that has just ended. */
|
|
180
|
+
plan: AnimationPlan;
|
|
181
|
+
/**
|
|
182
|
+
* `true` if it played to its last frame; `false` if it was cut short (new
|
|
183
|
+
* data, the screen went away) or never got to play.
|
|
184
|
+
*/
|
|
185
|
+
completed: boolean;
|
|
186
|
+
/** The widget's data when it started. */
|
|
187
|
+
data: D | undefined;
|
|
188
|
+
}
|
|
189
|
+
/** What `WidgetDefinition.animation` is asked with. */
|
|
190
|
+
export interface AnimationContext<O, D> extends StateContext<O, D> {
|
|
191
|
+
/** Where the widget paints (the same `box` its `render` gets), to decide with the layout it will have. */
|
|
192
|
+
box: Box;
|
|
193
|
+
/**
|
|
194
|
+
* Why it is asked: `idle`, every now and then while nothing else happens
|
|
195
|
+
* (see `animationGap`), or `data`, because this instance's data just changed
|
|
196
|
+
* or one of its sources did (see `prev`) and a reaction may be due, like a
|
|
197
|
+
* cat walking to its new spot.
|
|
198
|
+
*/
|
|
199
|
+
trigger: 'idle' | 'data';
|
|
200
|
+
/** What the widget had before the change, for `trigger: 'data'`: its own data and its sources. */
|
|
201
|
+
prev?: {
|
|
202
|
+
data: D | undefined;
|
|
203
|
+
sources: Record<string, unknown>;
|
|
204
|
+
};
|
|
205
|
+
/** Random numbers in [0, 1). Use these and not `Math.random`, so tests can seed them. */
|
|
206
|
+
rnd: () => number;
|
|
207
|
+
}
|
|
126
208
|
/**
|
|
127
209
|
* When the widget must be repainted even though its data didn't change: a
|
|
128
210
|
* clock every second, a countdown every minute. Fewer repaints are fewer
|
|
@@ -173,6 +255,29 @@ export interface WidgetDefinition<OF extends ObjectField<any> = ObjectField<any>
|
|
|
173
255
|
timeoutMs?: number;
|
|
174
256
|
/** Paint. Synchronous and fast: load images in `fetch` and paint them here. */
|
|
175
257
|
render: (g: Graphics, ctx: RenderContext<Infer<OF>, D>) => void;
|
|
258
|
+
/**
|
|
259
|
+
* Asks for a short animation inside the tile, or `null` for none. The
|
|
260
|
+
* runtime calls it only while this widget is the one shown, the screen has
|
|
261
|
+
* room for another animation (several widgets may ask: they wait their turn,
|
|
262
|
+
* and how many play together is up to the runtime) and its `animationGap` /
|
|
263
|
+
* `animationCooldownMs` allow it. It must be cheap and decide from the data alone; the drawing goes in
|
|
264
|
+
* `render`, which gets `ctx.anim` for every frame. Every frame costs USB
|
|
265
|
+
* time: keep them few and short.
|
|
266
|
+
*/
|
|
267
|
+
animation?: (ctx: AnimationContext<Infer<OF>, D>) => AnimationPlan | null;
|
|
268
|
+
/**
|
|
269
|
+
* Called when an animation of this widget ends, whether it played through
|
|
270
|
+
* or not (see `completed`): to remember what it did, like a trophy for
|
|
271
|
+
* a set that got built. Synchronous and quick; keep files under `dataDir`.
|
|
272
|
+
*/
|
|
273
|
+
animationEnded?: (ctx: AnimationEndContext<Infer<OF>, D>) => void;
|
|
274
|
+
/** How long to wait between the `idle` questions of `animation`, a random time in the range. Default 20-60 s. */
|
|
275
|
+
animationGap?: {
|
|
276
|
+
minMs: number;
|
|
277
|
+
maxMs: number;
|
|
278
|
+
};
|
|
279
|
+
/** Minimum time from the end of an animation to the next one asked because of a data change. Default 0. */
|
|
280
|
+
animationCooldownMs?: number;
|
|
176
281
|
/** Also a function, when the cadence depends on the options or the data (a carousel every `rotateMs`). */
|
|
177
282
|
redraw?: Redraw | ((ctx: StateContext<Infer<OF>, D>) => Redraw);
|
|
178
283
|
/** Has nothing to show right now (nothing playing, no rain): skipped in its cell's rotation. */
|
package/dist/system.d.ts
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `~`, `~/x` (and `~\x`), `$VAR`, `${VAR}` and, on Windows, `%VAR%` expanded.
|
|
3
|
+
* A variable that isn't set stays as written. `os` and `env` are for tests.
|
|
4
|
+
*/
|
|
5
|
+
export declare function expandPath(p: string, o?: {
|
|
6
|
+
os?: NodeJS.Platform;
|
|
7
|
+
env?: Record<string, string | undefined>;
|
|
8
|
+
home?: string;
|
|
9
|
+
}): string;
|
|
10
|
+
/** What to hand to `execFile` to run `cmd` with `args`. */
|
|
11
|
+
export interface Command {
|
|
12
|
+
file: string;
|
|
13
|
+
args: string[];
|
|
14
|
+
/** Pass as `windowsVerbatimArguments` to `execFile`/`spawn`. */
|
|
15
|
+
windowsVerbatimArguments?: boolean;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* On Windows, tools installed with npm (`ccusage`, `npx`, `claude`…) are
|
|
19
|
+
* `.cmd` shims, and `execFile('ccusage')` neither finds them (no `PATHEXT`
|
|
20
|
+
* search) nor, since Node 18.20 / 20.12, runs a `.cmd` without a shell.
|
|
21
|
+
* `commandFor` returns the pair to pass to `execFile`: for a `.cmd`/`.bat`
|
|
22
|
+
* (looked up on `PATH`), `cmd.exe /d /s /c "…"` with each argument quoted; for
|
|
23
|
+
* anything else, the command as it was. Elsewhere it changes nothing.
|
|
24
|
+
*
|
|
25
|
+
* const c = commandFor('ccusage', ['daily', '--json']);
|
|
26
|
+
* execFile(c.file, c.args, { windowsHide: true, windowsVerbatimArguments: c.windowsVerbatimArguments }, cb);
|
|
27
|
+
*
|
|
28
|
+
* Throws if an argument holds a quote, a `%` or a line break and the command needs
|
|
29
|
+
* cmd.exe: there is no safe way to pass those through a batch file.
|
|
30
|
+
*/
|
|
31
|
+
export declare function commandFor(cmd: string, args: string[], o?: {
|
|
32
|
+
os?: NodeJS.Platform;
|
|
33
|
+
env?: Record<string, string | undefined>;
|
|
34
|
+
exists?: (path: string) => boolean;
|
|
35
|
+
}): Command;
|
package/dist/system.js
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Small helpers for what differs between operating systems, shared by Kiosko
|
|
3
|
+
* and by plugins (`import { expandPath, commandFor } from '@kiosko/sdk'`).
|
|
4
|
+
*/
|
|
5
|
+
import { statSync } from 'node:fs';
|
|
6
|
+
import { homedir } from 'node:os';
|
|
7
|
+
import { join, win32 } from 'node:path';
|
|
8
|
+
/**
|
|
9
|
+
* `~`, `~/x` (and `~\x`), `$VAR`, `${VAR}` and, on Windows, `%VAR%` expanded.
|
|
10
|
+
* A variable that isn't set stays as written. `os` and `env` are for tests.
|
|
11
|
+
*/
|
|
12
|
+
export function expandPath(p, o = {}) {
|
|
13
|
+
const env = o.env ?? process.env;
|
|
14
|
+
let v = p.replace(/\$(?:\{(\w+)\}|(\w+))/g, (whole, a, b) => env[a ?? b ?? ''] ?? whole);
|
|
15
|
+
// `%VAR%` is a Windows thing; on other systems a `%` is just a character.
|
|
16
|
+
if ((o.os ?? process.platform) === 'win32')
|
|
17
|
+
v = v.replace(/%([^%\r\n]+)%/g, (whole, name) => env[name] ?? whole);
|
|
18
|
+
const home = o.home ?? homedir();
|
|
19
|
+
if (v === '~')
|
|
20
|
+
return home;
|
|
21
|
+
return v.startsWith('~/') || v.startsWith('~\\') ? join(home, v.slice(2)) : v;
|
|
22
|
+
}
|
|
23
|
+
/** Characters cmd.exe would interpret inside a batch shim's arguments. */
|
|
24
|
+
const CMD_UNSAFE = /["%\r\n\0]/;
|
|
25
|
+
/**
|
|
26
|
+
* On Windows, tools installed with npm (`ccusage`, `npx`, `claude`…) are
|
|
27
|
+
* `.cmd` shims, and `execFile('ccusage')` neither finds them (no `PATHEXT`
|
|
28
|
+
* search) nor, since Node 18.20 / 20.12, runs a `.cmd` without a shell.
|
|
29
|
+
* `commandFor` returns the pair to pass to `execFile`: for a `.cmd`/`.bat`
|
|
30
|
+
* (looked up on `PATH`), `cmd.exe /d /s /c "…"` with each argument quoted; for
|
|
31
|
+
* anything else, the command as it was. Elsewhere it changes nothing.
|
|
32
|
+
*
|
|
33
|
+
* const c = commandFor('ccusage', ['daily', '--json']);
|
|
34
|
+
* execFile(c.file, c.args, { windowsHide: true, windowsVerbatimArguments: c.windowsVerbatimArguments }, cb);
|
|
35
|
+
*
|
|
36
|
+
* Throws if an argument holds a quote, a `%` or a line break and the command needs
|
|
37
|
+
* cmd.exe: there is no safe way to pass those through a batch file.
|
|
38
|
+
*/
|
|
39
|
+
export function commandFor(cmd, args, o = {}) {
|
|
40
|
+
if ((o.os ?? process.platform) !== 'win32')
|
|
41
|
+
return { file: cmd, args };
|
|
42
|
+
const env = o.env ?? process.env;
|
|
43
|
+
const exists = o.exists ?? defaultExists;
|
|
44
|
+
const shim = findShim(cmd, env, exists);
|
|
45
|
+
if (!shim)
|
|
46
|
+
return { file: cmd, args };
|
|
47
|
+
for (const a of args)
|
|
48
|
+
if (CMD_UNSAFE.test(a))
|
|
49
|
+
throw new Error(`argument not allowed for ${cmd}: ${JSON.stringify(a)}`);
|
|
50
|
+
const quote = (s) => (/^[\w@+=:,./\\-]+$/.test(s) ? s : `"${s}"`);
|
|
51
|
+
return { file: env.ComSpec || 'cmd.exe', args: ['/d', '/s', '/c', `"${[shim, ...args].map(quote).join(' ')}"`], windowsVerbatimArguments: true };
|
|
52
|
+
}
|
|
53
|
+
function defaultExists(path) {
|
|
54
|
+
try {
|
|
55
|
+
// Sync and tiny: a handful of PATH entries, once per call.
|
|
56
|
+
return statSync(path).isFile();
|
|
57
|
+
}
|
|
58
|
+
catch {
|
|
59
|
+
return false;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
/** The `.cmd`/`.bat` that `cmd` names on Windows, or null if it is (or may be) an executable. */
|
|
63
|
+
function findShim(cmd, env, exists) {
|
|
64
|
+
if (/\.(exe|com)$/i.test(cmd))
|
|
65
|
+
return null;
|
|
66
|
+
if (/\.(cmd|bat)$/i.test(cmd) && /[\\/]/.test(cmd))
|
|
67
|
+
return exists(cmd) ? cmd : null;
|
|
68
|
+
const dirs = /[\\/]/.test(cmd) ? [''] : (env.PATH ?? env.Path ?? '').split(';').filter(Boolean);
|
|
69
|
+
const exts = (env.PATHEXT ?? '.COM;.EXE;.BAT;.CMD').split(';').filter(Boolean);
|
|
70
|
+
for (const dir of dirs) {
|
|
71
|
+
const base = dir ? win32.join(dir, cmd) : cmd;
|
|
72
|
+
for (const ext of /\.\w+$/.test(cmd) ? [''] : exts) {
|
|
73
|
+
const p = base + ext.toLowerCase();
|
|
74
|
+
if (!exists(p))
|
|
75
|
+
continue;
|
|
76
|
+
return /\.(cmd|bat)$/i.test(p) ? p : null;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
return null;
|
|
80
|
+
}
|
package/package.json
CHANGED
package/src/context.ts
CHANGED
|
@@ -10,7 +10,7 @@ import { localize, type Localized } from './i18n.ts';
|
|
|
10
10
|
import type { BaseContext, Location, Logger } from './plugin.ts';
|
|
11
11
|
import { DEFAULT_WORKDAY, type Workday } from './workday.ts';
|
|
12
12
|
|
|
13
|
-
export function createBaseContext(o: { now: number; locale?: string; timeFormat?: TimeFormat; workday?: Workday; location?: Location; log?: Logger; dataDir?: string }): BaseContext {
|
|
13
|
+
export function createBaseContext(o: { now: number; locale?: string; timeFormat?: TimeFormat; workday?: Workday; location?: Location; log?: Logger; dataDir?: string; cacheDir?: string }): BaseContext {
|
|
14
14
|
const locale = o.locale ?? 'en';
|
|
15
15
|
return {
|
|
16
16
|
now: o.now,
|
|
@@ -19,6 +19,7 @@ export function createBaseContext(o: { now: number; locale?: string; timeFormat?
|
|
|
19
19
|
fmt: createFormatters({ locale, timeFormat: o.timeFormat ?? 'auto', now: o.now }),
|
|
20
20
|
log: o.log ?? (() => undefined),
|
|
21
21
|
dataDir: o.dataDir ?? '',
|
|
22
|
+
cacheDir: o.cacheDir ?? o.dataDir ?? '',
|
|
22
23
|
...(o.location ? { location: o.location } : {}),
|
|
23
24
|
workday: o.workday ?? DEFAULT_WORKDAY,
|
|
24
25
|
};
|
package/src/index.ts
CHANGED
package/src/plugin.ts
CHANGED
|
@@ -88,8 +88,15 @@ export interface BaseContext {
|
|
|
88
88
|
*/
|
|
89
89
|
fmt: Formatters;
|
|
90
90
|
log: Logger;
|
|
91
|
-
/** Where the plugin may keep files (
|
|
91
|
+
/** Where the plugin may keep files that must survive a reboot (settings, history). Created on demand. */
|
|
92
92
|
dataDir: string;
|
|
93
|
+
/**
|
|
94
|
+
* Where the plugin keeps what can be fetched again (downloads, feed copies,
|
|
95
|
+
* photos, tiles). It is RAM (tmpfs), so the constant rewriting never wears
|
|
96
|
+
* the SSD, and it is empty after a reboot. Created on demand. Falls back to
|
|
97
|
+
* `dataDir` where there is no tmpfs. Use this, not `dataDir`, for caches.
|
|
98
|
+
*/
|
|
99
|
+
cacheDir: string;
|
|
93
100
|
/**
|
|
94
101
|
* The user's place, if set in Settings (weather, sun, earthquakes…). Use it
|
|
95
102
|
* by default; offer a place of your own only where a second one makes sense
|
|
@@ -139,10 +146,83 @@ export interface RenderContext<O, D> extends BaseContext, StateContext<O, D> {
|
|
|
139
146
|
canvas: CanvasApi;
|
|
140
147
|
/** The widget is drawn in the editor (gallery thumbnail or live preview) and not on the panel. */
|
|
141
148
|
preview: boolean;
|
|
142
|
-
/**
|
|
149
|
+
/**
|
|
150
|
+
* Set while an animation of this widget is being painted (see
|
|
151
|
+
* `WidgetDefinition.animation`): which frame this call must draw. Without
|
|
152
|
+
* it, paint the widget at rest. The first and the last frame of an
|
|
153
|
+
* animation should look exactly like the still one.
|
|
154
|
+
*/
|
|
155
|
+
anim?: AnimationFrame;
|
|
156
|
+
/** Index of the frame being painted while an animation runs (`anim.i`). */
|
|
143
157
|
frame?: number;
|
|
144
158
|
}
|
|
145
159
|
|
|
160
|
+
/**
|
|
161
|
+
* A short animation a widget asks the runtime to play inside its own tile: a
|
|
162
|
+
* cat's tail flick, a tile sliding in. The runtime plans it (when, one at a
|
|
163
|
+
* time per screen), paints every frame by calling `render` with `ctx.anim`
|
|
164
|
+
* before playing, sends them at `fps` (never above the screen's `maxFps`) and
|
|
165
|
+
* finishes with a real still repaint.
|
|
166
|
+
*/
|
|
167
|
+
export interface AnimationPlan {
|
|
168
|
+
/** What it is (`tail`, `walk`…): for the log and for `render`, which receives it in `ctx.anim.plan`. */
|
|
169
|
+
id: string;
|
|
170
|
+
/** Frames, counting the first and the last (those are the still picture and are not sent). At least 3. */
|
|
171
|
+
frames: number;
|
|
172
|
+
/** Frames per second it was designed for. The screen's `maxFps` caps it. */
|
|
173
|
+
fps: number;
|
|
174
|
+
/** Whatever `render` needs to draw it (who does what). Plain data: it is not interpreted. */
|
|
175
|
+
params?: unknown;
|
|
176
|
+
/**
|
|
177
|
+
* `false`: new data doesn't cut it short (something that must be seen to
|
|
178
|
+
* the end). Default `true`: a data change cancels it and the screen is
|
|
179
|
+
* repainted with the new data.
|
|
180
|
+
*/
|
|
181
|
+
interruptible?: boolean;
|
|
182
|
+
/** Stay still at `at` (0..1 of the animation) for `ms` (a long task being shown), or until interrupted. */
|
|
183
|
+
hold?: { at: number; ms: number };
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
/** The frame `render` must paint (see `RenderContext.anim`). */
|
|
187
|
+
export interface AnimationFrame {
|
|
188
|
+
plan: AnimationPlan;
|
|
189
|
+
/** Frame number, 0..plan.frames-1. */
|
|
190
|
+
i: number;
|
|
191
|
+
/** Progress, 0..1: `i / (frames - 1)`. 0 and 1 are the still picture. */
|
|
192
|
+
t: number;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
/** What `WidgetDefinition.animationEnded` is told. */
|
|
196
|
+
export interface AnimationEndContext<O, D> extends BaseContext {
|
|
197
|
+
options: O;
|
|
198
|
+
/** The animation that has just ended. */
|
|
199
|
+
plan: AnimationPlan;
|
|
200
|
+
/**
|
|
201
|
+
* `true` if it played to its last frame; `false` if it was cut short (new
|
|
202
|
+
* data, the screen went away) or never got to play.
|
|
203
|
+
*/
|
|
204
|
+
completed: boolean;
|
|
205
|
+
/** The widget's data when it started. */
|
|
206
|
+
data: D | undefined;
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
/** What `WidgetDefinition.animation` is asked with. */
|
|
210
|
+
export interface AnimationContext<O, D> extends StateContext<O, D> {
|
|
211
|
+
/** Where the widget paints (the same `box` its `render` gets), to decide with the layout it will have. */
|
|
212
|
+
box: Box;
|
|
213
|
+
/**
|
|
214
|
+
* Why it is asked: `idle`, every now and then while nothing else happens
|
|
215
|
+
* (see `animationGap`), or `data`, because this instance's data just changed
|
|
216
|
+
* or one of its sources did (see `prev`) and a reaction may be due, like a
|
|
217
|
+
* cat walking to its new spot.
|
|
218
|
+
*/
|
|
219
|
+
trigger: 'idle' | 'data';
|
|
220
|
+
/** What the widget had before the change, for `trigger: 'data'`: its own data and its sources. */
|
|
221
|
+
prev?: { data: D | undefined; sources: Record<string, unknown> };
|
|
222
|
+
/** Random numbers in [0, 1). Use these and not `Math.random`, so tests can seed them. */
|
|
223
|
+
rnd: () => number;
|
|
224
|
+
}
|
|
225
|
+
|
|
146
226
|
/**
|
|
147
227
|
* When the widget must be repainted even though its data didn't change: a
|
|
148
228
|
* clock every second, a countdown every minute. Fewer repaints are fewer
|
|
@@ -185,6 +265,26 @@ export interface WidgetDefinition<OF extends ObjectField<any> = ObjectField<any>
|
|
|
185
265
|
timeoutMs?: number;
|
|
186
266
|
/** Paint. Synchronous and fast: load images in `fetch` and paint them here. */
|
|
187
267
|
render: (g: Graphics, ctx: RenderContext<Infer<OF>, D>) => void;
|
|
268
|
+
/**
|
|
269
|
+
* Asks for a short animation inside the tile, or `null` for none. The
|
|
270
|
+
* runtime calls it only while this widget is the one shown, the screen has
|
|
271
|
+
* room for another animation (several widgets may ask: they wait their turn,
|
|
272
|
+
* and how many play together is up to the runtime) and its `animationGap` /
|
|
273
|
+
* `animationCooldownMs` allow it. It must be cheap and decide from the data alone; the drawing goes in
|
|
274
|
+
* `render`, which gets `ctx.anim` for every frame. Every frame costs USB
|
|
275
|
+
* time: keep them few and short.
|
|
276
|
+
*/
|
|
277
|
+
animation?: (ctx: AnimationContext<Infer<OF>, D>) => AnimationPlan | null;
|
|
278
|
+
/**
|
|
279
|
+
* Called when an animation of this widget ends, whether it played through
|
|
280
|
+
* or not (see `completed`): to remember what it did, like a trophy for
|
|
281
|
+
* a set that got built. Synchronous and quick; keep files under `dataDir`.
|
|
282
|
+
*/
|
|
283
|
+
animationEnded?: (ctx: AnimationEndContext<Infer<OF>, D>) => void;
|
|
284
|
+
/** How long to wait between the `idle` questions of `animation`, a random time in the range. Default 20-60 s. */
|
|
285
|
+
animationGap?: { minMs: number; maxMs: number };
|
|
286
|
+
/** Minimum time from the end of an animation to the next one asked because of a data change. Default 0. */
|
|
287
|
+
animationCooldownMs?: number;
|
|
188
288
|
/** Also a function, when the cadence depends on the options or the data (a carousel every `rotateMs`). */
|
|
189
289
|
redraw?: Redraw | ((ctx: StateContext<Infer<OF>, D>) => Redraw);
|
|
190
290
|
/** Has nothing to show right now (nothing playing, no rain): skipped in its cell's rotation. */
|
package/src/system.ts
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Small helpers for what differs between operating systems, shared by Kiosko
|
|
3
|
+
* and by plugins (`import { expandPath, commandFor } from '@kiosko/sdk'`).
|
|
4
|
+
*/
|
|
5
|
+
import { statSync } from 'node:fs';
|
|
6
|
+
import { homedir } from 'node:os';
|
|
7
|
+
import { join, win32 } from 'node:path';
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* `~`, `~/x` (and `~\x`), `$VAR`, `${VAR}` and, on Windows, `%VAR%` expanded.
|
|
11
|
+
* A variable that isn't set stays as written. `os` and `env` are for tests.
|
|
12
|
+
*/
|
|
13
|
+
export function expandPath(p: string, o: { os?: NodeJS.Platform; env?: Record<string, string | undefined>; home?: string } = {}): string {
|
|
14
|
+
const env = o.env ?? process.env;
|
|
15
|
+
let v = p.replace(/\$(?:\{(\w+)\}|(\w+))/g, (whole, a?: string, b?: string) => env[a ?? b ?? ''] ?? whole);
|
|
16
|
+
// `%VAR%` is a Windows thing; on other systems a `%` is just a character.
|
|
17
|
+
if ((o.os ?? process.platform) === 'win32') v = v.replace(/%([^%\r\n]+)%/g, (whole, name: string) => env[name] ?? whole);
|
|
18
|
+
const home = o.home ?? homedir();
|
|
19
|
+
if (v === '~') return home;
|
|
20
|
+
return v.startsWith('~/') || v.startsWith('~\\') ? join(home, v.slice(2)) : v;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/** What to hand to `execFile` to run `cmd` with `args`. */
|
|
24
|
+
export interface Command {
|
|
25
|
+
file: string;
|
|
26
|
+
args: string[];
|
|
27
|
+
/** Pass as `windowsVerbatimArguments` to `execFile`/`spawn`. */
|
|
28
|
+
windowsVerbatimArguments?: boolean;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/** Characters cmd.exe would interpret inside a batch shim's arguments. */
|
|
32
|
+
const CMD_UNSAFE = /["%\r\n\0]/;
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* On Windows, tools installed with npm (`ccusage`, `npx`, `claude`…) are
|
|
36
|
+
* `.cmd` shims, and `execFile('ccusage')` neither finds them (no `PATHEXT`
|
|
37
|
+
* search) nor, since Node 18.20 / 20.12, runs a `.cmd` without a shell.
|
|
38
|
+
* `commandFor` returns the pair to pass to `execFile`: for a `.cmd`/`.bat`
|
|
39
|
+
* (looked up on `PATH`), `cmd.exe /d /s /c "…"` with each argument quoted; for
|
|
40
|
+
* anything else, the command as it was. Elsewhere it changes nothing.
|
|
41
|
+
*
|
|
42
|
+
* const c = commandFor('ccusage', ['daily', '--json']);
|
|
43
|
+
* execFile(c.file, c.args, { windowsHide: true, windowsVerbatimArguments: c.windowsVerbatimArguments }, cb);
|
|
44
|
+
*
|
|
45
|
+
* Throws if an argument holds a quote, a `%` or a line break and the command needs
|
|
46
|
+
* cmd.exe: there is no safe way to pass those through a batch file.
|
|
47
|
+
*/
|
|
48
|
+
export function commandFor(
|
|
49
|
+
cmd: string,
|
|
50
|
+
args: string[],
|
|
51
|
+
o: { os?: NodeJS.Platform; env?: Record<string, string | undefined>; exists?: (path: string) => boolean } = {},
|
|
52
|
+
): Command {
|
|
53
|
+
if ((o.os ?? process.platform) !== 'win32') return { file: cmd, args };
|
|
54
|
+
const env = o.env ?? process.env;
|
|
55
|
+
const exists = o.exists ?? defaultExists;
|
|
56
|
+
const shim = findShim(cmd, env, exists);
|
|
57
|
+
if (!shim) return { file: cmd, args };
|
|
58
|
+
for (const a of args) if (CMD_UNSAFE.test(a)) throw new Error(`argument not allowed for ${cmd}: ${JSON.stringify(a)}`);
|
|
59
|
+
const quote = (s: string) => (/^[\w@+=:,./\\-]+$/.test(s) ? s : `"${s}"`);
|
|
60
|
+
return { file: env.ComSpec || 'cmd.exe', args: ['/d', '/s', '/c', `"${[shim, ...args].map(quote).join(' ')}"`], windowsVerbatimArguments: true };
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function defaultExists(path: string): boolean {
|
|
64
|
+
try {
|
|
65
|
+
// Sync and tiny: a handful of PATH entries, once per call.
|
|
66
|
+
return statSync(path).isFile();
|
|
67
|
+
} catch {
|
|
68
|
+
return false;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/** The `.cmd`/`.bat` that `cmd` names on Windows, or null if it is (or may be) an executable. */
|
|
73
|
+
function findShim(cmd: string, env: Record<string, string | undefined>, exists: (p: string) => boolean): string | null {
|
|
74
|
+
if (/\.(exe|com)$/i.test(cmd)) return null;
|
|
75
|
+
if (/\.(cmd|bat)$/i.test(cmd) && /[\\/]/.test(cmd)) return exists(cmd) ? cmd : null;
|
|
76
|
+
const dirs = /[\\/]/.test(cmd) ? [''] : (env.PATH ?? env.Path ?? '').split(';').filter(Boolean);
|
|
77
|
+
const exts = (env.PATHEXT ?? '.COM;.EXE;.BAT;.CMD').split(';').filter(Boolean);
|
|
78
|
+
for (const dir of dirs) {
|
|
79
|
+
const base = dir ? win32.join(dir, cmd) : cmd;
|
|
80
|
+
for (const ext of /\.\w+$/.test(cmd) ? [''] : exts) {
|
|
81
|
+
const p = base + ext.toLowerCase();
|
|
82
|
+
if (!exists(p)) continue;
|
|
83
|
+
return /\.(cmd|bat)$/i.test(p) ? p : null;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
return null;
|
|
87
|
+
}
|