@lowfat/log 0.0.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/README.md ADDED
@@ -0,0 +1,3 @@
1
+ # Lowfat Log
2
+
3
+ A simple and easy-to-use logging tool, small enough to be included with client-side code, but works excellent with Node, and Deno, as well.
@@ -0,0 +1,24 @@
1
+ //#region src/index.d.ts
2
+ interface LoggerProgessOptions {
3
+ color: number;
4
+ symbol: string;
5
+ }
6
+ declare class Log {
7
+ #private;
8
+ static error(...message: string[]): void;
9
+ static warn(...message: string[]): void;
10
+ static note(...message: string[]): void;
11
+ static info(...message: string[]): void;
12
+ constructor(label: string, total: number);
13
+ error(...message: string[]): void;
14
+ warn(...message: string[]): void;
15
+ note(...message: string[]): void;
16
+ info(...message: string[]): void;
17
+ update(total: number): void;
18
+ progress(i: number, {
19
+ color,
20
+ symbol
21
+ }?: Partial<LoggerProgessOptions>): void;
22
+ }
23
+ //#endregion
24
+ export { LoggerProgessOptions, Log as default };
package/dist/index.mjs ADDED
@@ -0,0 +1,80 @@
1
+ import { zeroPad } from "@lowfat/utils/strings";
2
+ //#region src/index.ts
3
+ var Log = class Log {
4
+ /** label The custom LFP label */
5
+ static #label = "[LFP]";
6
+ /** The column width of the `stdout` terminal window, minus padding */
7
+ static #maxColumns = process.stdout.columns - Log.#label.length - 6;
8
+ /** Per default `false` gets set to `true` when first instance gets created and sets up one listener to change the static variable `#maxColumns` */
9
+ static #listening = false;
10
+ /** Styles the line prefix. */
11
+ static #prefix(label = Log.#label) {
12
+ return `\x1b[90m${label}\x1b[0m`;
13
+ }
14
+ /** Creates a colored output string for printing; https://stackoverflow.com/a/41407246 */
15
+ static #clr(clr, msg) {
16
+ return `\x1b[${clr}m${msg}\x1b[0m`;
17
+ }
18
+ static error(...message) {
19
+ console.error(Log.#prefix(), Log.#clr(31, message.join(" ")));
20
+ }
21
+ static warn(...message) {
22
+ console.warn(Log.#prefix(), Log.#clr(33, message.join(" ")));
23
+ }
24
+ static note(...message) {
25
+ console.info(Log.#prefix(), Log.#clr(36, message.join(" ")));
26
+ }
27
+ static info(...message) {
28
+ console.log(Log.#prefix(), message.join(" "));
29
+ }
30
+ /** Updates the static value for maximum column count. */
31
+ static #setMaxColumns(label, padding = 6) {
32
+ Log.#maxColumns = process.stdout.columns - label.length - padding;
33
+ }
34
+ /** Sets up a listener to listen for output window size changes to adjust column count. */
35
+ static #setupListener(label = Log.#label) {
36
+ if (!Log.#listening) {
37
+ process.stdout.on("resize", () => Log.#setMaxColumns(label));
38
+ Log.#listening = false;
39
+ }
40
+ }
41
+ /** The total amount of data to process for the upper limit of the progress bar. */
42
+ #total = 0;
43
+ /** A custom prefix to identify origin of the logging. */
44
+ #instancePrefix;
45
+ /** Log instance that allows for updating */
46
+ constructor(label, total) {
47
+ const instanceLabel = !!label ? `${Log.#label.slice(0, -1)}/${label}]` : Log.#label;
48
+ this.#instancePrefix = Log.#prefix(instanceLabel);
49
+ this.update(total);
50
+ Log.#setMaxColumns(instanceLabel);
51
+ Log.#setupListener(instanceLabel);
52
+ }
53
+ error(...message) {
54
+ console.error(this.#instancePrefix, Log.#clr(31, message.join(" ")));
55
+ }
56
+ warn(...message) {
57
+ console.warn(this.#instancePrefix, Log.#clr(33, message.join(" ")));
58
+ }
59
+ note(...message) {
60
+ console.info(this.#instancePrefix, Log.#clr(36, message.join(" ")));
61
+ }
62
+ info(...message) {
63
+ console.log(this.#instancePrefix, message.join(" "));
64
+ }
65
+ /** Updates the total number, resets the progress visualizer. */
66
+ update(total) {
67
+ this.#total = total ?? 0;
68
+ }
69
+ /** Displays the next step in terminal progress bar. */
70
+ progress(i, { color = 0, symbol = "#" } = {}) {
71
+ if (!process.stdout.isTTY) return;
72
+ const progress = i / this.#total;
73
+ const columns = Math.ceil(progress * Log.#maxColumns);
74
+ process.stdout.cursorTo(0);
75
+ process.stdout.write(`${this.#instancePrefix} ${Log.#clr(color, symbol).repeat(columns)}${" ".repeat(Log.#maxColumns - columns)} ${zeroPad(Math.ceil(progress * 100), 3)}%`);
76
+ if (i >= this.#total - 1) process.stdout.write("\n");
77
+ }
78
+ };
79
+ //#endregion
80
+ export { Log as default };
package/package.json ADDED
@@ -0,0 +1,41 @@
1
+ {
2
+ "author": {
3
+ "email": "lowfatprophet@googlemail.com",
4
+ "name": "lowfatprophet",
5
+ "url": "https://lowfatprophet.netlify.app"
6
+ },
7
+ "dependencies": {
8
+ "@lowfat/utils": "^0.0.1"
9
+ },
10
+ "devDependencies": {
11
+ "@biomejs/biome": "^2.4.14",
12
+ "@types/node": "^25.6.2",
13
+ "@typescript/native-preview": "^7.0.0-dev.20260421.2",
14
+ "tsdown": "^0.22.0",
15
+ "tslib": "^2.8.1",
16
+ "typescript": "^6.0.3",
17
+ "vitest": "^4.1.5"
18
+ },
19
+ "main": "./dist/index.mjs",
20
+ "name": "@lowfat/log",
21
+ "repository": {
22
+ "type": "git",
23
+ "url": "https://codeberg.org/lowfatprophet/log.git"
24
+ },
25
+ "scripts": {
26
+ "build": "vitest --run && npx tsdown",
27
+ "dev": "npx tsdown --watch",
28
+ "test": "vitest --disableConsoleIntercept true"
29
+ },
30
+ "tsdown": {
31
+ "format": {
32
+ "esm": [
33
+ "nodenext"
34
+ ]
35
+ },
36
+ "outDir": "dist"
37
+ },
38
+ "type": "module",
39
+ "types": "./dist/index.d.mts",
40
+ "version": "0.0.1"
41
+ }