colorino 0.3.3 → 0.5.0

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,768 @@
1
+ function hexToRgb(hex) {
2
+ const match = hex.toString().match(/[a-f0-9]{6}|[a-f0-9]{3}/i);
3
+ if (!match) {
4
+ return [0, 0, 0];
5
+ }
6
+ let colorString = match[0];
7
+ if (match[0].length === 3) {
8
+ colorString = [...colorString].map((char) => char + char).join("");
9
+ }
10
+ const integer = parseInt(colorString, 16);
11
+ const r = integer >> 16 & 255;
12
+ const g = integer >> 8 & 255;
13
+ const b = integer & 255;
14
+ return [r, g, b];
15
+ }
16
+ function rgbToAnsi256(rgb) {
17
+ const [r, g, b] = rgb;
18
+ if (r === g && g === b) {
19
+ if (r < 8) return 16;
20
+ if (r > 248) return 231;
21
+ return Math.round((r - 8) / 247 * 24) + 232;
22
+ }
23
+ return 16 + 36 * Math.round(r / 255 * 5) + 6 * Math.round(g / 255 * 5) + Math.round(b / 255 * 5);
24
+ }
25
+ function rgbToHsvValue(rgb) {
26
+ const r = rgb[0] / 255;
27
+ const g = rgb[1] / 255;
28
+ const b = rgb[2] / 255;
29
+ const v = Math.max(r, g, b);
30
+ return v * 100;
31
+ }
32
+ function rgbToAnsi16(rgb) {
33
+ const [r, g, b] = rgb;
34
+ const value = rgbToHsvValue(rgb);
35
+ const roundedValue = Math.round(value / 50);
36
+ if (roundedValue === 0) {
37
+ return 30;
38
+ }
39
+ let ansi = 30 + (Math.round(b / 255) << 2 | Math.round(g / 255) << 1 | Math.round(r / 255));
40
+ if (roundedValue === 2) {
41
+ ansi += 60;
42
+ }
43
+ return ansi;
44
+ }
45
+ const colorConverter = {
46
+ hex: {
47
+ toRgb: hexToRgb,
48
+ toAnsi16: (hex) => rgbToAnsi16(hexToRgb(hex)),
49
+ toAnsi256: (hex) => rgbToAnsi256(hexToRgb(hex))
50
+ }};
51
+
52
+ var ColorLevel = /* @__PURE__ */ ((ColorLevel2) => {
53
+ ColorLevel2[ColorLevel2["NO_COLOR"] = 0] = "NO_COLOR";
54
+ ColorLevel2[ColorLevel2["ANSI"] = 1] = "ANSI";
55
+ ColorLevel2[ColorLevel2["ANSI256"] = 2] = "ANSI256";
56
+ ColorLevel2[ColorLevel2["TRUECOLOR"] = 3] = "TRUECOLOR";
57
+ return ColorLevel2;
58
+ })(ColorLevel || {});
59
+
60
+ function isConsoleMethod(level) {
61
+ return ["log", "info", "warn", "error", "trace", "debug"].includes(level);
62
+ }
63
+
64
+ class Colorino {
65
+ constructor(_palette, _validator, _browserColorSupportDetector, _nodeColorSupportDetector, _options = {}) {
66
+ this._palette = _palette;
67
+ this._validator = _validator;
68
+ this._browserColorSupportDetector = _browserColorSupportDetector;
69
+ this._nodeColorSupportDetector = _nodeColorSupportDetector;
70
+ this._options = _options;
71
+ this.isBrowser = !!this._browserColorSupportDetector;
72
+ this._colorLevel = this._detectColorSupport();
73
+ const validatePaletteResult = this._validator.validatePalette(this._palette);
74
+ if (validatePaletteResult.isErr()) throw validatePaletteResult.error;
75
+ if (!this.isBrowser && (this._colorLevel === ColorLevel.NO_COLOR || this._colorLevel === "UnknownEnv") && !this._options.disableWarnings) {
76
+ this._maybeWarnUser();
77
+ }
78
+ }
79
+ _alreadyWarned = false;
80
+ _colorLevel;
81
+ isBrowser;
82
+ log(...args) {
83
+ this._out("log", args);
84
+ }
85
+ info(...args) {
86
+ this._out("info", args);
87
+ }
88
+ warn(...args) {
89
+ this._out("warn", args);
90
+ }
91
+ error(...args) {
92
+ this._out("error", args);
93
+ }
94
+ trace(...args) {
95
+ this._out("trace", args);
96
+ }
97
+ debug(...args) {
98
+ this._out("debug", args);
99
+ }
100
+ _detectColorSupport() {
101
+ if (this.isBrowser) {
102
+ return this._browserColorSupportDetector?.getColorLevel() ?? "UnknownEnv";
103
+ }
104
+ if (this._nodeColorSupportDetector?.isNodeEnv()) {
105
+ return this._nodeColorSupportDetector?.getColorLevel() ?? "UnknownEnv";
106
+ }
107
+ return "UnknownEnv";
108
+ }
109
+ _maybeWarnUser() {
110
+ if (this._alreadyWarned) return;
111
+ this._alreadyWarned = true;
112
+ console.warn(
113
+ "[Colorino] No ANSI color support detected in this terminal. See [https://github.com/chalk/supports-color#support-matrix](https://github.com/chalk/supports-color#support-matrix) to learn how to enable terminal color."
114
+ );
115
+ }
116
+ _out(level, args) {
117
+ const consoleMethod = isConsoleMethod(level) ? level : "log";
118
+ if (this._colorLevel === ColorLevel.NO_COLOR || this._colorLevel === "UnknownEnv") {
119
+ if (level === "trace") console.trace(...args);
120
+ else console[consoleMethod](...args);
121
+ return;
122
+ }
123
+ if (this.isBrowser) {
124
+ const hex2 = this._palette[level];
125
+ if (typeof args[0] === "string") {
126
+ console[consoleMethod](`%c${args[0]}`, `color:${hex2}`, ...args.slice(1));
127
+ } else {
128
+ console[consoleMethod](...args);
129
+ }
130
+ return;
131
+ }
132
+ const hex = this._palette[level];
133
+ let ansiCode;
134
+ switch (this._colorLevel) {
135
+ case ColorLevel.TRUECOLOR: {
136
+ const [r, g, b] = colorConverter.hex.toRgb(hex);
137
+ ansiCode = `\x1B[38;2;${r};${g};${b}m`;
138
+ break;
139
+ }
140
+ case ColorLevel.ANSI256: {
141
+ const code = colorConverter.hex.toAnsi256(hex);
142
+ ansiCode = `\x1B[38;5;${code}m`;
143
+ break;
144
+ }
145
+ case ColorLevel.ANSI:
146
+ default: {
147
+ const code = colorConverter.hex.toAnsi16(hex);
148
+ ansiCode = `\x1B[${code}m`;
149
+ break;
150
+ }
151
+ }
152
+ const processedArgs = [...args];
153
+ const firstStringIndex = processedArgs.findIndex(
154
+ (arg) => typeof arg === "string"
155
+ );
156
+ if (firstStringIndex !== -1) {
157
+ processedArgs[firstStringIndex] = `${ansiCode}${processedArgs[firstStringIndex]}\x1B[0m`;
158
+ }
159
+ if (level === "trace") {
160
+ console.trace(...processedArgs);
161
+ } else {
162
+ console[consoleMethod](...processedArgs);
163
+ }
164
+ }
165
+ }
166
+
167
+ const defaultErrorConfig = {
168
+ withStackTrace: false,
169
+ };
170
+ // Custom error object
171
+ // Context / discussion: https://github.com/supermacro/neverthrow/pull/215
172
+ const createNeverThrowError = (message, result, config = defaultErrorConfig) => {
173
+ const data = result.isOk()
174
+ ? { type: 'Ok', value: result.value }
175
+ : { type: 'Err', value: result.error };
176
+ const maybeStack = config.withStackTrace ? new Error().stack : undefined;
177
+ return {
178
+ data,
179
+ message,
180
+ stack: maybeStack,
181
+ };
182
+ };
183
+
184
+ /******************************************************************************
185
+ Copyright (c) Microsoft Corporation.
186
+
187
+ Permission to use, copy, modify, and/or distribute this software for any
188
+ purpose with or without fee is hereby granted.
189
+
190
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
191
+ REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
192
+ AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
193
+ INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
194
+ LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
195
+ OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
196
+ PERFORMANCE OF THIS SOFTWARE.
197
+ ***************************************************************************** */
198
+ /* global Reflect, Promise, SuppressedError, Symbol, Iterator */
199
+
200
+
201
+ function __awaiter(thisArg, _arguments, P, generator) {
202
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
203
+ return new (P || (P = Promise))(function (resolve, reject) {
204
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
205
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
206
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
207
+ step((generator = generator.apply(thisArg, [])).next());
208
+ });
209
+ }
210
+
211
+ function __values(o) {
212
+ var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
213
+ if (m) return m.call(o);
214
+ if (o && typeof o.length === "number") return {
215
+ next: function () {
216
+ if (o && i >= o.length) o = void 0;
217
+ return { value: o && o[i++], done: !o };
218
+ }
219
+ };
220
+ throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
221
+ }
222
+
223
+ function __await(v) {
224
+ return this instanceof __await ? (this.v = v, this) : new __await(v);
225
+ }
226
+
227
+ function __asyncGenerator(thisArg, _arguments, generator) {
228
+ if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
229
+ var g = generator.apply(thisArg, _arguments || []), i, q = [];
230
+ return i = Object.create((typeof AsyncIterator === "function" ? AsyncIterator : Object).prototype), verb("next"), verb("throw"), verb("return", awaitReturn), i[Symbol.asyncIterator] = function () { return this; }, i;
231
+ function awaitReturn(f) { return function (v) { return Promise.resolve(v).then(f, reject); }; }
232
+ function verb(n, f) { if (g[n]) { i[n] = function (v) { return new Promise(function (a, b) { q.push([n, v, a, b]) > 1 || resume(n, v); }); }; if (f) i[n] = f(i[n]); } }
233
+ function resume(n, v) { try { step(g[n](v)); } catch (e) { settle(q[0][3], e); } }
234
+ function step(r) { r.value instanceof __await ? Promise.resolve(r.value.v).then(fulfill, reject) : settle(q[0][2], r); }
235
+ function fulfill(value) { resume("next", value); }
236
+ function reject(value) { resume("throw", value); }
237
+ function settle(f, v) { if (f(v), q.shift(), q.length) resume(q[0][0], q[0][1]); }
238
+ }
239
+
240
+ function __asyncDelegator(o) {
241
+ var i, p;
242
+ return i = {}, verb("next"), verb("throw", function (e) { throw e; }), verb("return"), i[Symbol.iterator] = function () { return this; }, i;
243
+ function verb(n, f) { i[n] = o[n] ? function (v) { return (p = !p) ? { value: __await(o[n](v)), done: false } : f ? f(v) : v; } : f; }
244
+ }
245
+
246
+ function __asyncValues(o) {
247
+ if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
248
+ var m = o[Symbol.asyncIterator], i;
249
+ return m ? m.call(o) : (o = typeof __values === "function" ? __values(o) : o[Symbol.iterator](), i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i);
250
+ function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; }
251
+ function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); }
252
+ }
253
+
254
+ typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
255
+ var e = new Error(message);
256
+ return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
257
+ };
258
+
259
+ class ResultAsync {
260
+ constructor(res) {
261
+ this._promise = res;
262
+ }
263
+ static fromSafePromise(promise) {
264
+ const newPromise = promise.then((value) => new Ok(value));
265
+ return new ResultAsync(newPromise);
266
+ }
267
+ static fromPromise(promise, errorFn) {
268
+ const newPromise = promise
269
+ .then((value) => new Ok(value))
270
+ .catch((e) => new Err(errorFn(e)));
271
+ return new ResultAsync(newPromise);
272
+ }
273
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
274
+ static fromThrowable(fn, errorFn) {
275
+ return (...args) => {
276
+ return new ResultAsync((() => __awaiter(this, void 0, void 0, function* () {
277
+ try {
278
+ return new Ok(yield fn(...args));
279
+ }
280
+ catch (error) {
281
+ return new Err(errorFn ? errorFn(error) : error);
282
+ }
283
+ }))());
284
+ };
285
+ }
286
+ static combine(asyncResultList) {
287
+ return combineResultAsyncList(asyncResultList);
288
+ }
289
+ static combineWithAllErrors(asyncResultList) {
290
+ return combineResultAsyncListWithAllErrors(asyncResultList);
291
+ }
292
+ map(f) {
293
+ return new ResultAsync(this._promise.then((res) => __awaiter(this, void 0, void 0, function* () {
294
+ if (res.isErr()) {
295
+ return new Err(res.error);
296
+ }
297
+ return new Ok(yield f(res.value));
298
+ })));
299
+ }
300
+ andThrough(f) {
301
+ return new ResultAsync(this._promise.then((res) => __awaiter(this, void 0, void 0, function* () {
302
+ if (res.isErr()) {
303
+ return new Err(res.error);
304
+ }
305
+ const newRes = yield f(res.value);
306
+ if (newRes.isErr()) {
307
+ return new Err(newRes.error);
308
+ }
309
+ return new Ok(res.value);
310
+ })));
311
+ }
312
+ andTee(f) {
313
+ return new ResultAsync(this._promise.then((res) => __awaiter(this, void 0, void 0, function* () {
314
+ if (res.isErr()) {
315
+ return new Err(res.error);
316
+ }
317
+ try {
318
+ yield f(res.value);
319
+ }
320
+ catch (e) {
321
+ // Tee does not care about the error
322
+ }
323
+ return new Ok(res.value);
324
+ })));
325
+ }
326
+ orTee(f) {
327
+ return new ResultAsync(this._promise.then((res) => __awaiter(this, void 0, void 0, function* () {
328
+ if (res.isOk()) {
329
+ return new Ok(res.value);
330
+ }
331
+ try {
332
+ yield f(res.error);
333
+ }
334
+ catch (e) {
335
+ // Tee does not care about the error
336
+ }
337
+ return new Err(res.error);
338
+ })));
339
+ }
340
+ mapErr(f) {
341
+ return new ResultAsync(this._promise.then((res) => __awaiter(this, void 0, void 0, function* () {
342
+ if (res.isOk()) {
343
+ return new Ok(res.value);
344
+ }
345
+ return new Err(yield f(res.error));
346
+ })));
347
+ }
348
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/explicit-module-boundary-types
349
+ andThen(f) {
350
+ return new ResultAsync(this._promise.then((res) => {
351
+ if (res.isErr()) {
352
+ return new Err(res.error);
353
+ }
354
+ const newValue = f(res.value);
355
+ return newValue instanceof ResultAsync ? newValue._promise : newValue;
356
+ }));
357
+ }
358
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/explicit-module-boundary-types
359
+ orElse(f) {
360
+ return new ResultAsync(this._promise.then((res) => __awaiter(this, void 0, void 0, function* () {
361
+ if (res.isErr()) {
362
+ return f(res.error);
363
+ }
364
+ return new Ok(res.value);
365
+ })));
366
+ }
367
+ match(ok, _err) {
368
+ return this._promise.then((res) => res.match(ok, _err));
369
+ }
370
+ unwrapOr(t) {
371
+ return this._promise.then((res) => res.unwrapOr(t));
372
+ }
373
+ /**
374
+ * @deprecated will be removed in 9.0.0.
375
+ *
376
+ * You can use `safeTry` without this method.
377
+ * @example
378
+ * ```typescript
379
+ * safeTry(async function* () {
380
+ * const okValue = yield* yourResult
381
+ * })
382
+ * ```
383
+ * Emulates Rust's `?` operator in `safeTry`'s body. See also `safeTry`.
384
+ */
385
+ safeUnwrap() {
386
+ return __asyncGenerator(this, arguments, function* safeUnwrap_1() {
387
+ return yield __await(yield __await(yield* __asyncDelegator(__asyncValues(yield __await(this._promise.then((res) => res.safeUnwrap()))))));
388
+ });
389
+ }
390
+ // Makes ResultAsync implement PromiseLike<Result>
391
+ then(successCallback, failureCallback) {
392
+ return this._promise.then(successCallback, failureCallback);
393
+ }
394
+ [Symbol.asyncIterator]() {
395
+ return __asyncGenerator(this, arguments, function* _a() {
396
+ const result = yield __await(this._promise);
397
+ if (result.isErr()) {
398
+ // @ts-expect-error -- This is structurally equivalent and safe
399
+ yield yield __await(errAsync(result.error));
400
+ }
401
+ // @ts-expect-error -- This is structurally equivalent and safe
402
+ return yield __await(result.value);
403
+ });
404
+ }
405
+ }
406
+ function errAsync(err) {
407
+ return new ResultAsync(Promise.resolve(new Err(err)));
408
+ }
409
+
410
+ /**
411
+ * Short circuits on the FIRST Err value that we find
412
+ */
413
+ const combineResultList = (resultList) => {
414
+ let acc = ok([]);
415
+ for (const result of resultList) {
416
+ if (result.isErr()) {
417
+ acc = err(result.error);
418
+ break;
419
+ }
420
+ else {
421
+ acc.map((list) => list.push(result.value));
422
+ }
423
+ }
424
+ return acc;
425
+ };
426
+ /* This is the typesafe version of Promise.all
427
+ *
428
+ * Takes a list of ResultAsync<T, E> and success if all inner results are Ok values
429
+ * or fails if one (or more) of the inner results are Err values
430
+ */
431
+ const combineResultAsyncList = (asyncResultList) => ResultAsync.fromSafePromise(Promise.all(asyncResultList)).andThen(combineResultList);
432
+ /**
433
+ * Give a list of all the errors we find
434
+ */
435
+ const combineResultListWithAllErrors = (resultList) => {
436
+ let acc = ok([]);
437
+ for (const result of resultList) {
438
+ if (result.isErr() && acc.isErr()) {
439
+ acc.error.push(result.error);
440
+ }
441
+ else if (result.isErr() && acc.isOk()) {
442
+ acc = err([result.error]);
443
+ }
444
+ else if (result.isOk() && acc.isOk()) {
445
+ acc.value.push(result.value);
446
+ }
447
+ // do nothing when result.isOk() && acc.isErr()
448
+ }
449
+ return acc;
450
+ };
451
+ const combineResultAsyncListWithAllErrors = (asyncResultList) => ResultAsync.fromSafePromise(Promise.all(asyncResultList)).andThen(combineResultListWithAllErrors);
452
+
453
+ // eslint-disable-next-line @typescript-eslint/no-namespace
454
+ var Result;
455
+ (function (Result) {
456
+ /**
457
+ * Wraps a function with a try catch, creating a new function with the same
458
+ * arguments but returning `Ok` if successful, `Err` if the function throws
459
+ *
460
+ * @param fn function to wrap with ok on success or err on failure
461
+ * @param errorFn when an error is thrown, this will wrap the error result if provided
462
+ */
463
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
464
+ function fromThrowable(fn, errorFn) {
465
+ return (...args) => {
466
+ try {
467
+ const result = fn(...args);
468
+ return ok(result);
469
+ }
470
+ catch (e) {
471
+ return err(errorFn ? errorFn(e) : e);
472
+ }
473
+ };
474
+ }
475
+ Result.fromThrowable = fromThrowable;
476
+ function combine(resultList) {
477
+ return combineResultList(resultList);
478
+ }
479
+ Result.combine = combine;
480
+ function combineWithAllErrors(resultList) {
481
+ return combineResultListWithAllErrors(resultList);
482
+ }
483
+ Result.combineWithAllErrors = combineWithAllErrors;
484
+ })(Result || (Result = {}));
485
+ function ok(value) {
486
+ return new Ok(value);
487
+ }
488
+ function err(err) {
489
+ return new Err(err);
490
+ }
491
+ class Ok {
492
+ constructor(value) {
493
+ this.value = value;
494
+ }
495
+ isOk() {
496
+ return true;
497
+ }
498
+ isErr() {
499
+ return !this.isOk();
500
+ }
501
+ map(f) {
502
+ return ok(f(this.value));
503
+ }
504
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
505
+ mapErr(_f) {
506
+ return ok(this.value);
507
+ }
508
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/explicit-module-boundary-types
509
+ andThen(f) {
510
+ return f(this.value);
511
+ }
512
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/explicit-module-boundary-types
513
+ andThrough(f) {
514
+ return f(this.value).map((_value) => this.value);
515
+ }
516
+ andTee(f) {
517
+ try {
518
+ f(this.value);
519
+ }
520
+ catch (e) {
521
+ // Tee doesn't care about the error
522
+ }
523
+ return ok(this.value);
524
+ }
525
+ orTee(_f) {
526
+ return ok(this.value);
527
+ }
528
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/explicit-module-boundary-types
529
+ orElse(_f) {
530
+ return ok(this.value);
531
+ }
532
+ asyncAndThen(f) {
533
+ return f(this.value);
534
+ }
535
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/explicit-module-boundary-types
536
+ asyncAndThrough(f) {
537
+ return f(this.value).map(() => this.value);
538
+ }
539
+ asyncMap(f) {
540
+ return ResultAsync.fromSafePromise(f(this.value));
541
+ }
542
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
543
+ unwrapOr(_v) {
544
+ return this.value;
545
+ }
546
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
547
+ match(ok, _err) {
548
+ return ok(this.value);
549
+ }
550
+ safeUnwrap() {
551
+ const value = this.value;
552
+ /* eslint-disable-next-line require-yield */
553
+ return (function* () {
554
+ return value;
555
+ })();
556
+ }
557
+ _unsafeUnwrap(_) {
558
+ return this.value;
559
+ }
560
+ _unsafeUnwrapErr(config) {
561
+ throw createNeverThrowError('Called `_unsafeUnwrapErr` on an Ok', this, config);
562
+ }
563
+ // eslint-disable-next-line @typescript-eslint/no-this-alias, require-yield
564
+ *[Symbol.iterator]() {
565
+ return this.value;
566
+ }
567
+ }
568
+ class Err {
569
+ constructor(error) {
570
+ this.error = error;
571
+ }
572
+ isOk() {
573
+ return false;
574
+ }
575
+ isErr() {
576
+ return !this.isOk();
577
+ }
578
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
579
+ map(_f) {
580
+ return err(this.error);
581
+ }
582
+ mapErr(f) {
583
+ return err(f(this.error));
584
+ }
585
+ andThrough(_f) {
586
+ return err(this.error);
587
+ }
588
+ andTee(_f) {
589
+ return err(this.error);
590
+ }
591
+ orTee(f) {
592
+ try {
593
+ f(this.error);
594
+ }
595
+ catch (e) {
596
+ // Tee doesn't care about the error
597
+ }
598
+ return err(this.error);
599
+ }
600
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/explicit-module-boundary-types
601
+ andThen(_f) {
602
+ return err(this.error);
603
+ }
604
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/explicit-module-boundary-types
605
+ orElse(f) {
606
+ return f(this.error);
607
+ }
608
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
609
+ asyncAndThen(_f) {
610
+ return errAsync(this.error);
611
+ }
612
+ asyncAndThrough(_f) {
613
+ return errAsync(this.error);
614
+ }
615
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
616
+ asyncMap(_f) {
617
+ return errAsync(this.error);
618
+ }
619
+ unwrapOr(v) {
620
+ return v;
621
+ }
622
+ match(_ok, err) {
623
+ return err(this.error);
624
+ }
625
+ safeUnwrap() {
626
+ const error = this.error;
627
+ return (function* () {
628
+ yield err(error);
629
+ throw new Error('Do not use this generator out of `safeTry`');
630
+ })();
631
+ }
632
+ _unsafeUnwrap(config) {
633
+ throw createNeverThrowError('Called `_unsafeUnwrap` on an Err', this, config);
634
+ }
635
+ _unsafeUnwrapErr(_) {
636
+ return this.error;
637
+ }
638
+ *[Symbol.iterator]() {
639
+ // eslint-disable-next-line @typescript-eslint/no-this-alias
640
+ const self = this;
641
+ // @ts-expect-error -- This is structurally equivalent and safe
642
+ yield self;
643
+ // @ts-expect-error -- This is structurally equivalent and safe
644
+ return self;
645
+ }
646
+ }
647
+ Result.fromThrowable;
648
+
649
+ class ColorinoError extends Error {
650
+ constructor(message) {
651
+ super(message);
652
+ this.name = "ColorinoError";
653
+ Object.setPrototypeOf(this, ColorinoError.prototype);
654
+ }
655
+ }
656
+ class OscQueryError extends Error {
657
+ constructor(message) {
658
+ super(message);
659
+ this.name = "OscQueryError";
660
+ Object.setPrototypeOf(this, OscQueryError.prototype);
661
+ }
662
+ }
663
+
664
+ class InputValidator {
665
+ validateHex(hex) {
666
+ const trimmedHex = hex.trim();
667
+ const isHexValid = /^#[0-9A-F]{6}$/i.test(trimmedHex);
668
+ if (!isHexValid) {
669
+ return err(new ColorinoError(`Invalid hex color: '${hex}'`));
670
+ }
671
+ return ok(true);
672
+ }
673
+ validatePalette(palette) {
674
+ for (const level in palette) {
675
+ const hex = palette[level];
676
+ const result = this.validateHex(hex);
677
+ if (result.isErr()) {
678
+ return err(result.error);
679
+ }
680
+ }
681
+ return ok(true);
682
+ }
683
+ }
684
+
685
+ const catppuccinMochaPalette = {
686
+ log: "#cdd6f4",
687
+ // Text
688
+ info: "#89b4fa",
689
+ // Blue
690
+ warn: "#f9e2af",
691
+ // Yellow
692
+ error: "#f38ba8",
693
+ // Red
694
+ debug: "#a6adc8",
695
+ // Subtext0
696
+ trace: "#9399b2"
697
+ // Subtext1
698
+ };
699
+ const catppuccinLattePalette = {
700
+ log: "#4c4f69",
701
+ // Text
702
+ info: "#1e66f5",
703
+ // Blue
704
+ warn: "#df8e1d",
705
+ // Yellow
706
+ error: "#d20f39",
707
+ // Red
708
+ debug: "#7c7f93",
709
+ // Subtext0
710
+ trace: "#8c8fa1"
711
+ // Subtext1
712
+ };
713
+ const draculaPalette = {
714
+ log: "#f8f8f2",
715
+ // Foreground
716
+ info: "#8be9fd",
717
+ // Cyan
718
+ warn: "#f1fa8c",
719
+ // Yellow
720
+ error: "#ff5555",
721
+ // Red
722
+ debug: "#bd93f9",
723
+ // Purple
724
+ trace: "#6272a4"
725
+ // Comment
726
+ };
727
+ const githubLightPalette = {
728
+ log: "#24292e",
729
+ // Text
730
+ info: "#0366d6",
731
+ // Blue
732
+ warn: "#f9a002",
733
+ // Yellow
734
+ error: "#d73a49",
735
+ // Red
736
+ debug: "#586069",
737
+ // Gray
738
+ trace: "#6a737d"
739
+ // Gray-light
740
+ };
741
+
742
+ const themePalettes = {
743
+ "catppuccin-mocha": catppuccinMochaPalette,
744
+ "catppuccin-latte": catppuccinLattePalette,
745
+ dracula: draculaPalette,
746
+ "github-light": githubLightPalette
747
+ };
748
+ const defaultDarkTheme = "catppuccin-mocha";
749
+ const defaultLightTheme = "github-light";
750
+ function isThemeName(theme) {
751
+ return theme in themePalettes;
752
+ }
753
+
754
+ function determineBaseTheme(themeOpt, detectedBrowserTheme) {
755
+ let baseThemeName;
756
+ if (isThemeName(themeOpt)) {
757
+ baseThemeName = themeOpt;
758
+ } else if (themeOpt === "light") {
759
+ baseThemeName = defaultLightTheme;
760
+ } else if (themeOpt === "dark") {
761
+ baseThemeName = defaultDarkTheme;
762
+ } else {
763
+ baseThemeName = detectedBrowserTheme === "light" ? defaultLightTheme : defaultDarkTheme;
764
+ }
765
+ return baseThemeName;
766
+ }
767
+
768
+ export { ColorLevel as C, InputValidator as I, OscQueryError as O, Colorino as a, determineBaseTheme as d, err as e, ok as o, themePalettes as t };