alacritty-theme-switch
Version:
CLI utility for switching Alacritty color themes
284 lines (283 loc) • 13.2 kB
JavaScript
// Copyright 2018-2025 the Deno authors. MIT license.
// This module is browser compatible.
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
};
var _Dumper_instances, _Dumper_arrayTypeCache, _Dumper_printObject, _Dumper_isPrimitive, _Dumper_getTypeOfArray, _Dumper_doGetTypeOfArray, _Dumper_printAsInlineValue, _Dumper_isSimplySerializable, _Dumper_header, _Dumper_headerGroup, _Dumper_declaration, _Dumper_arrayDeclaration, _Dumper_strDeclaration, _Dumper_numberDeclaration, _Dumper_boolDeclaration, _Dumper_printDate, _Dumper_dateDeclaration, _Dumper_format;
// Bare keys may only contain ASCII letters,
// ASCII digits, underscores, and dashes (A-Za-z0-9_-).
function joinKeys(keys) {
// Dotted keys are a sequence of bare or quoted keys joined with a dot.
// This allows for grouping similar properties together:
return keys
.map((str) => {
return str.length === 0 || str.match(/[^A-Za-z0-9_-]/)
? JSON.stringify(str)
: str;
})
.join(".");
}
class Dumper {
constructor(srcObjc) {
_Dumper_instances.add(this);
Object.defineProperty(this, "maxPad", {
enumerable: true,
configurable: true,
writable: true,
value: 0
});
Object.defineProperty(this, "srcObject", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
Object.defineProperty(this, "output", {
enumerable: true,
configurable: true,
writable: true,
value: []
});
_Dumper_arrayTypeCache.set(this, new Map());
this.srcObject = srcObjc;
}
dump(fmtOptions = {}) {
// deno-lint-ignore no-explicit-any
this.output = __classPrivateFieldGet(this, _Dumper_instances, "m", _Dumper_printObject).call(this, this.srcObject);
this.output = __classPrivateFieldGet(this, _Dumper_instances, "m", _Dumper_format).call(this, fmtOptions);
return this.output;
}
}
_Dumper_arrayTypeCache = new WeakMap(), _Dumper_instances = new WeakSet(), _Dumper_printObject = function _Dumper_printObject(obj, keys = []) {
const out = [];
const props = Object.keys(obj);
const inlineProps = [];
const multilineProps = [];
for (const prop of props) {
if (__classPrivateFieldGet(this, _Dumper_instances, "m", _Dumper_isSimplySerializable).call(this, obj[prop])) {
inlineProps.push(prop);
}
else {
multilineProps.push(prop);
}
}
const sortedProps = inlineProps.concat(multilineProps);
for (const prop of sortedProps) {
const value = obj[prop];
if (value instanceof Date) {
out.push(__classPrivateFieldGet(this, _Dumper_instances, "m", _Dumper_dateDeclaration).call(this, [prop], value));
}
else if (typeof value === "string" || value instanceof RegExp) {
out.push(__classPrivateFieldGet(this, _Dumper_instances, "m", _Dumper_strDeclaration).call(this, [prop], value.toString()));
}
else if (typeof value === "number") {
out.push(__classPrivateFieldGet(this, _Dumper_instances, "m", _Dumper_numberDeclaration).call(this, [prop], value));
}
else if (typeof value === "boolean") {
out.push(__classPrivateFieldGet(this, _Dumper_instances, "m", _Dumper_boolDeclaration).call(this, [prop], value));
}
else if (value instanceof Array) {
const arrayType = __classPrivateFieldGet(this, _Dumper_instances, "m", _Dumper_getTypeOfArray).call(this, value);
if (arrayType === "ONLY_PRIMITIVE") {
out.push(__classPrivateFieldGet(this, _Dumper_instances, "m", _Dumper_arrayDeclaration).call(this, [prop], value));
}
else if (arrayType === "ONLY_OBJECT_EXCLUDING_ARRAY") {
// array of objects
for (let i = 0; i < value.length; i++) {
out.push("");
out.push(__classPrivateFieldGet(this, _Dumper_instances, "m", _Dumper_headerGroup).call(this, [...keys, prop]));
out.push(...__classPrivateFieldGet(this, _Dumper_instances, "m", _Dumper_printObject).call(this, value[i], [...keys, prop]));
}
}
else {
// this is a complex array, use the inline format.
const str = value.map((x) => __classPrivateFieldGet(this, _Dumper_instances, "m", _Dumper_printAsInlineValue).call(this, x)).join(",");
out.push(`${__classPrivateFieldGet(this, _Dumper_instances, "m", _Dumper_declaration).call(this, [prop])}[${str}]`);
}
}
else if (typeof value === "object") {
out.push("");
out.push(__classPrivateFieldGet(this, _Dumper_instances, "m", _Dumper_header).call(this, [...keys, prop]));
if (value) {
const toParse = value;
out.push(...__classPrivateFieldGet(this, _Dumper_instances, "m", _Dumper_printObject).call(this, toParse, [...keys, prop]));
}
// out.push(...this._parse(value, `${path}${prop}.`));
}
}
out.push("");
return out;
}, _Dumper_isPrimitive = function _Dumper_isPrimitive(value) {
return value instanceof Date ||
value instanceof RegExp ||
["string", "number", "boolean"].includes(typeof value);
}, _Dumper_getTypeOfArray = function _Dumper_getTypeOfArray(arr) {
if (__classPrivateFieldGet(this, _Dumper_arrayTypeCache, "f").has(arr)) {
return __classPrivateFieldGet(this, _Dumper_arrayTypeCache, "f").get(arr);
}
const type = __classPrivateFieldGet(this, _Dumper_instances, "m", _Dumper_doGetTypeOfArray).call(this, arr);
__classPrivateFieldGet(this, _Dumper_arrayTypeCache, "f").set(arr, type);
return type;
}, _Dumper_doGetTypeOfArray = function _Dumper_doGetTypeOfArray(arr) {
if (!arr.length) {
// any type should be fine
return "ONLY_PRIMITIVE";
}
const onlyPrimitive = __classPrivateFieldGet(this, _Dumper_instances, "m", _Dumper_isPrimitive).call(this, arr[0]);
if (arr[0] instanceof Array) {
return "MIXED";
}
for (let i = 1; i < arr.length; i++) {
if (onlyPrimitive !== __classPrivateFieldGet(this, _Dumper_instances, "m", _Dumper_isPrimitive).call(this, arr[i]) || arr[i] instanceof Array) {
return "MIXED";
}
}
return onlyPrimitive ? "ONLY_PRIMITIVE" : "ONLY_OBJECT_EXCLUDING_ARRAY";
}, _Dumper_printAsInlineValue = function _Dumper_printAsInlineValue(value) {
if (value instanceof Date) {
return `"${__classPrivateFieldGet(this, _Dumper_instances, "m", _Dumper_printDate).call(this, value)}"`;
}
else if (typeof value === "string" || value instanceof RegExp) {
return JSON.stringify(value.toString());
}
else if (typeof value === "number") {
return value;
}
else if (typeof value === "boolean") {
return value.toString();
}
else if (value instanceof Array) {
const str = value.map((x) => __classPrivateFieldGet(this, _Dumper_instances, "m", _Dumper_printAsInlineValue).call(this, x)).join(",");
return `[${str}]`;
}
else if (typeof value === "object") {
if (!value) {
throw new Error("Should never reach");
}
const str = Object.keys(value).map((key) => {
return `${joinKeys([key])} = ${
// deno-lint-ignore no-explicit-any
__classPrivateFieldGet(this, _Dumper_instances, "m", _Dumper_printAsInlineValue).call(this, value[key])}`;
}).join(",");
return `{${str}}`;
}
throw new Error("Should never reach");
}, _Dumper_isSimplySerializable = function _Dumper_isSimplySerializable(value) {
return (typeof value === "string" ||
typeof value === "number" ||
typeof value === "boolean" ||
value instanceof RegExp ||
value instanceof Date ||
(value instanceof Array &&
__classPrivateFieldGet(this, _Dumper_instances, "m", _Dumper_getTypeOfArray).call(this, value) !== "ONLY_OBJECT_EXCLUDING_ARRAY"));
}, _Dumper_header = function _Dumper_header(keys) {
return `[${joinKeys(keys)}]`;
}, _Dumper_headerGroup = function _Dumper_headerGroup(keys) {
return `[[${joinKeys(keys)}]]`;
}, _Dumper_declaration = function _Dumper_declaration(keys) {
const title = joinKeys(keys);
if (title.length > this.maxPad) {
this.maxPad = title.length;
}
return `${title} = `;
}, _Dumper_arrayDeclaration = function _Dumper_arrayDeclaration(keys, value) {
return `${__classPrivateFieldGet(this, _Dumper_instances, "m", _Dumper_declaration).call(this, keys)}${JSON.stringify(value)}`;
}, _Dumper_strDeclaration = function _Dumper_strDeclaration(keys, value) {
return `${__classPrivateFieldGet(this, _Dumper_instances, "m", _Dumper_declaration).call(this, keys)}${JSON.stringify(value)}`;
}, _Dumper_numberDeclaration = function _Dumper_numberDeclaration(keys, value) {
if (Number.isNaN(value)) {
return `${__classPrivateFieldGet(this, _Dumper_instances, "m", _Dumper_declaration).call(this, keys)}nan`;
}
switch (value) {
case Infinity:
return `${__classPrivateFieldGet(this, _Dumper_instances, "m", _Dumper_declaration).call(this, keys)}inf`;
case -Infinity:
return `${__classPrivateFieldGet(this, _Dumper_instances, "m", _Dumper_declaration).call(this, keys)}-inf`;
default:
return `${__classPrivateFieldGet(this, _Dumper_instances, "m", _Dumper_declaration).call(this, keys)}${value}`;
}
}, _Dumper_boolDeclaration = function _Dumper_boolDeclaration(keys, value) {
return `${__classPrivateFieldGet(this, _Dumper_instances, "m", _Dumper_declaration).call(this, keys)}${value}`;
}, _Dumper_printDate = function _Dumper_printDate(value) {
function dtPad(v, lPad = 2) {
return v.padStart(lPad, "0");
}
const m = dtPad((value.getUTCMonth() + 1).toString());
const d = dtPad(value.getUTCDate().toString());
const h = dtPad(value.getUTCHours().toString());
const min = dtPad(value.getUTCMinutes().toString());
const s = dtPad(value.getUTCSeconds().toString());
const ms = dtPad(value.getUTCMilliseconds().toString(), 3);
// formatted date
const fData = `${value.getUTCFullYear()}-${m}-${d}T${h}:${min}:${s}.${ms}`;
return fData;
}, _Dumper_dateDeclaration = function _Dumper_dateDeclaration(keys, value) {
return `${__classPrivateFieldGet(this, _Dumper_instances, "m", _Dumper_declaration).call(this, keys)}${__classPrivateFieldGet(this, _Dumper_instances, "m", _Dumper_printDate).call(this, value)}`;
}, _Dumper_format = function _Dumper_format(options = {}) {
const { keyAlignment = false } = options;
const rDeclaration = /^(\".*\"|[^=]*)\s=/;
const out = [];
for (let i = 0; i < this.output.length; i++) {
const l = this.output[i];
// we keep empty entry for array of objects
if (l[0] === "[" && l[1] !== "[") {
// non-empty object with only subobjects as properties
if (this.output[i + 1] === "" &&
this.output[i + 2]?.slice(0, l.length) === l.slice(0, -1) + ".") {
i += 1;
continue;
}
out.push(l);
}
else {
if (keyAlignment) {
const m = rDeclaration.exec(l);
if (m && m[1]) {
out.push(l.replace(m[1], m[1].padEnd(this.maxPad)));
}
else {
out.push(l);
}
}
else {
out.push(l);
}
}
}
// Cleaning multiple spaces
const cleanedOutput = [];
for (let i = 0; i < out.length; i++) {
const l = out[i];
if (!(l === "" && out[i + 1] === "")) {
cleanedOutput.push(l);
}
}
return cleanedOutput;
};
/**
* Converts an object to a {@link https://toml.io | TOML} string.
*
* @example Usage
* ```ts
* import { stringify } from "@std/toml/stringify";
* import { assertEquals } from "@std/assert";
*
* const obj = {
* title: "TOML Example",
* owner: {
* name: "Bob",
* bio: "Bob is a cool guy",
* }
* };
* const tomlString = stringify(obj);
* assertEquals(tomlString, `title = "TOML Example"\n\n[owner]\nname = "Bob"\nbio = "Bob is a cool guy"\n`);
* ```
* @param obj Source object
* @param options Options for stringifying.
* @returns TOML string
*/
export function stringify(obj, options) {
return new Dumper(obj).dump(options).join("\n");
}