alacritty-theme-switch
Version:
CLI utility for switching Alacritty color themes
216 lines (215 loc) • 10.3 kB
JavaScript
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
if (kind === "m") throw new TypeError("Private method is not writable");
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
};
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 _ProgressBar_instances, _ProgressBar_writer, _ProgressBar_id, _ProgressBar_startTime, _ProgressBar_previousTime, _ProgressBar_previousValue, _ProgressBar_barLength, _ProgressBar_fillChar, _ProgressBar_emptyChar, _ProgressBar_clear, _ProgressBar_formatter, _ProgressBar_keepOpen, _ProgressBar_pipePromise, _ProgressBar_createFormatterObject, _ProgressBar_print;
// Copyright 2018-2025 the Deno authors. MIT license.
import * as dntShim from "../../../../../_dnt.shims.js";
import { formatUnitFraction } from "./_unit.js";
const LINE_CLEAR = "\r\u001b[K";
function defaultFormatter(formatter) {
return `[${formatter.styledTime}] [${formatter.progressBar}] [${formatter.styledData()}]`;
}
function clamp(value, min, max) {
return Math.min(Math.max(value, min), max);
}
/**
* `ProgressBar` is a customisable class that reports updates to a
* {@link WritableStream} on a 1s interval. Progress is communicated by using
* the `ProgressBar.value` property.
*
* @experimental **UNSTABLE**: New API, yet to be vetted.
*
* @example Basic Usage
* ```ts ignore
* import { delay } from "@std/async";
* import { ProgressBar } from "@std/cli/unstable-progress-bar";
*
* const gen = async function* () {
* for (let i = 0; i < 100; ++i) {
* yield new Uint8Array(1000).fill(97);
* await delay(Math.random() * 200 | 0);
* }
* }();
* const writer = (await Deno.create("./_tmp/output.txt")).writable.getWriter();
*
* const bar = new ProgressBar({ max: 100_000 });
*
* for await (const buffer of gen) {
* bar.value += buffer.length;
* await writer.write(buffer);
* }
*
* await bar.stop();
* await writer.close();
* ```
*
* @example Custom Formatting
* ```ts ignore
* import { delay } from "@std/async";
* import { ProgressBar } from "@std/cli/unstable-progress-bar";
*
* const bar = new ProgressBar({
* max: 100,
* formatter(formatter) {
* return `[${formatter.styledTime}] [${formatter.progressBar}] [${formatter.value}/${formatter.max} files]`;
* },
* });
*
* for (const x of Array(100)) {
* bar.value += 1;
* await delay(Math.random() * 500);
* }
*
* await bar.stop();
*/
export class ProgressBar {
/**
* Constructs a new instance.
*
* @param options The options to configure various settings of the progress bar.
*/
constructor(options) {
_ProgressBar_instances.add(this);
/**
* The current progress that has been completed.
* @example Usage
* ```ts no-assert
* import { ProgressBar } from "@std/cli/unstable-progress-bar";
*
* const progressBar = new ProgressBar({ max : 10 });
* progressBar.value += 1;
*
* // do stuff
*
* await progressBar.stop();
* ```
*/
Object.defineProperty(this, "value", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
/**
* The maximum progress that is expected.
* @example Usage
* ```ts no-assert
* import { ProgressBar } from "@std/cli/unstable-progress-bar";
*
* const progressBar = new ProgressBar({ max : 1 });
* progressBar.max = 100;
*
* // do stuff
*
* await progressBar.stop();
* ```
*/
Object.defineProperty(this, "max", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
_ProgressBar_writer.set(this, void 0);
_ProgressBar_id.set(this, void 0);
_ProgressBar_startTime.set(this, void 0);
_ProgressBar_previousTime.set(this, void 0);
_ProgressBar_previousValue.set(this, void 0);
_ProgressBar_barLength.set(this, void 0);
_ProgressBar_fillChar.set(this, void 0);
_ProgressBar_emptyChar.set(this, void 0);
_ProgressBar_clear.set(this, void 0);
_ProgressBar_formatter.set(this, void 0);
_ProgressBar_keepOpen.set(this, void 0);
_ProgressBar_pipePromise.set(this, void 0);
const { writable = dntShim.Deno.stderr.writable, value = 0, max, barLength = 50, fillChar = "#", emptyChar = "-", clear = false, formatter = defaultFormatter, keepOpen = true, refreshMilliseconds = 1000, } = options;
this.value = value;
this.max = max;
__classPrivateFieldSet(this, _ProgressBar_barLength, barLength, "f");
__classPrivateFieldSet(this, _ProgressBar_fillChar, fillChar, "f");
__classPrivateFieldSet(this, _ProgressBar_emptyChar, emptyChar, "f");
__classPrivateFieldSet(this, _ProgressBar_clear, clear, "f");
__classPrivateFieldSet(this, _ProgressBar_formatter, formatter, "f");
__classPrivateFieldSet(this, _ProgressBar_keepOpen, keepOpen, "f");
const stream = new TextEncoderStream();
__classPrivateFieldSet(this, _ProgressBar_pipePromise, stream.readable
.pipeTo(writable, { preventClose: __classPrivateFieldGet(this, _ProgressBar_keepOpen, "f") })
.catch(() => clearInterval(__classPrivateFieldGet(this, _ProgressBar_id, "f"))), "f");
__classPrivateFieldSet(this, _ProgressBar_writer, stream.writable.getWriter(), "f");
__classPrivateFieldSet(this, _ProgressBar_startTime, Date.now(), "f");
__classPrivateFieldSet(this, _ProgressBar_previousTime, 0, "f");
__classPrivateFieldSet(this, _ProgressBar_previousValue, this.value, "f");
__classPrivateFieldSet(this, _ProgressBar_id, setInterval(() => __classPrivateFieldGet(this, _ProgressBar_instances, "m", _ProgressBar_print).call(this), refreshMilliseconds), "f");
__classPrivateFieldGet(this, _ProgressBar_instances, "m", _ProgressBar_print).call(this);
}
/**
* Ends the progress bar and cleans up any lose ends.
*
* @example Usage
* ```ts ignore
* import { ProgressBar } from "@std/cli/unstable-progress-bar";
*
* const progressBar = new ProgressBar({ max: 100 });
* await progressBar.stop()
* ```
*/
async stop() {
clearInterval(__classPrivateFieldGet(this, _ProgressBar_id, "f"));
try {
if (__classPrivateFieldGet(this, _ProgressBar_clear, "f")) {
await __classPrivateFieldGet(this, _ProgressBar_writer, "f").write(LINE_CLEAR);
}
else {
await __classPrivateFieldGet(this, _ProgressBar_instances, "m", _ProgressBar_print).call(this);
await __classPrivateFieldGet(this, _ProgressBar_writer, "f").write("\n");
}
await __classPrivateFieldGet(this, _ProgressBar_writer, "f").close();
await __classPrivateFieldGet(this, _ProgressBar_pipePromise, "f");
}
catch {
// ignore
}
}
}
_ProgressBar_writer = new WeakMap(), _ProgressBar_id = new WeakMap(), _ProgressBar_startTime = new WeakMap(), _ProgressBar_previousTime = new WeakMap(), _ProgressBar_previousValue = new WeakMap(), _ProgressBar_barLength = new WeakMap(), _ProgressBar_fillChar = new WeakMap(), _ProgressBar_emptyChar = new WeakMap(), _ProgressBar_clear = new WeakMap(), _ProgressBar_formatter = new WeakMap(), _ProgressBar_keepOpen = new WeakMap(), _ProgressBar_pipePromise = new WeakMap(), _ProgressBar_instances = new WeakSet(), _ProgressBar_createFormatterObject = function _ProgressBar_createFormatterObject() {
const time = Date.now() - __classPrivateFieldGet(this, _ProgressBar_startTime, "f");
const ratio = clamp(this.value / this.max, 0, 1);
const size = Math.trunc(ratio * __classPrivateFieldGet(this, _ProgressBar_barLength, "f"));
const fillChars = __classPrivateFieldGet(this, _ProgressBar_fillChar, "f").repeat(size);
const emptyChars = __classPrivateFieldGet(this, _ProgressBar_emptyChar, "f").repeat(__classPrivateFieldGet(this, _ProgressBar_barLength, "f") - size);
return {
get styledTime() {
const minutes = (this.time / 1000 / 60 | 0).toString().padStart(2, "0");
const seconds = (this.time / 1000 % 60 | 0).toString().padStart(2, "0");
return `${minutes}:${seconds}`;
},
styledData() {
return formatUnitFraction(this.value, this.max);
},
progressBar: `${fillChars}${emptyChars}`,
time,
previousTime: __classPrivateFieldGet(this, _ProgressBar_previousTime, "f"),
value: this.value,
previousValue: __classPrivateFieldGet(this, _ProgressBar_previousValue, "f"),
max: this.max,
};
}, _ProgressBar_print = async function _ProgressBar_print() {
const formatter = __classPrivateFieldGet(this, _ProgressBar_instances, "m", _ProgressBar_createFormatterObject).call(this);
const output = __classPrivateFieldGet(this, _ProgressBar_formatter, "f").call(this, formatter);
try {
await __classPrivateFieldGet(this, _ProgressBar_writer, "f").write(LINE_CLEAR + output);
}
catch {
// ignore
}
__classPrivateFieldSet(this, _ProgressBar_previousTime, formatter.time, "f");
__classPrivateFieldSet(this, _ProgressBar_previousValue, formatter.value, "f");
};