@gjsify/console 0.3.13 → 0.3.14

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.
Files changed (2) hide show
  1. package/lib/esm/index.js +190 -221
  2. package/package.json +3 -3
package/lib/esm/index.js CHANGED
@@ -1,171 +1,167 @@
1
+ //#region src/index.ts
1
2
  const _isGJS = typeof print === "function" && typeof printerr === "function";
2
3
  function _formatArgs(...args) {
3
- const fmt = args[0];
4
- const rest = args.slice(1);
5
- if (typeof fmt !== "string" || !/%(s|d|i|f|o|O|c)/.test(fmt)) {
6
- return args.map((a) => typeof a === "string" ? a : JSON.stringify(a)).join(" ");
7
- }
8
- let i = 0;
9
- const result = fmt.replace(/%([sdifOoc])/g, (_match, spec) => {
10
- if (i >= rest.length) return _match;
11
- const val = rest[i++];
12
- switch (spec) {
13
- case "s":
14
- return String(val);
15
- case "d":
16
- case "i":
17
- return String(parseInt(String(val), 10));
18
- case "f":
19
- return String(parseFloat(String(val)));
20
- case "o":
21
- case "O":
22
- return JSON.stringify(val);
23
- case "c":
24
- return "";
25
- // CSS styles ignore
26
- default:
27
- return _match;
28
- }
29
- });
30
- const remaining = rest.slice(i);
31
- if (remaining.length === 0) return result;
32
- return result + " " + remaining.map((a) => typeof a === "string" ? a : JSON.stringify(a)).join(" ");
33
- }
34
- class Console {
35
- _stdout;
36
- _stderr;
37
- _groupDepth = 0;
38
- _groupIndentation;
39
- _timers = /* @__PURE__ */ new Map();
40
- _counters = /* @__PURE__ */ new Map();
41
- constructor(stdoutOrOptions, stderr) {
42
- if (stdoutOrOptions && typeof stdoutOrOptions.write === "function") {
43
- this._stdout = stdoutOrOptions;
44
- this._stderr = stderr || this._stdout;
45
- } else if (stdoutOrOptions && typeof stdoutOrOptions === "object") {
46
- const opts = stdoutOrOptions;
47
- this._stdout = opts.stdout;
48
- this._stderr = opts.stderr || opts.stdout;
49
- }
50
- this._groupIndentation = 2;
51
- }
52
- _write(stream, ...args) {
53
- const target = stream === "stderr" ? this._stderr || this._stdout : this._stdout;
54
- if (target) {
55
- const indent = " ".repeat(this._groupDepth * this._groupIndentation);
56
- const message = _formatArgs(...args);
57
- target.write(indent + message + "\n");
58
- } else if (_isGJS) {
59
- const indent = " ".repeat(this._groupDepth * this._groupIndentation);
60
- const message = indent + _formatArgs(...args);
61
- if (stream === "stderr") {
62
- printerr(message);
63
- } else {
64
- print(message);
65
- }
66
- } else {
67
- const gc = globalThis.console;
68
- if (stream === "stderr") {
69
- gc.error(...args);
70
- } else {
71
- gc.log(...args);
72
- }
73
- }
74
- }
75
- log(...args) {
76
- this._write("stdout", ...args);
77
- }
78
- info(...args) {
79
- this._write("stdout", ...args);
80
- }
81
- debug(...args) {
82
- this._write("stdout", ...args);
83
- }
84
- warn(...args) {
85
- this._write("stderr", ...args);
86
- }
87
- error(...args) {
88
- this._write("stderr", ...args);
89
- }
90
- dir(obj, _options) {
91
- this._write("stdout", obj);
92
- }
93
- dirxml(...args) {
94
- this.log(...args);
95
- }
96
- assert(value, ...args) {
97
- if (!value) {
98
- this.error("Assertion failed:", ...args);
99
- }
100
- }
101
- clear() {
102
- if (this._stdout) {
103
- this._stdout.write("\x1Bc");
104
- } else if (_isGJS) {
105
- print("\x1Bc");
106
- } else {
107
- globalThis.console.clear();
108
- }
109
- }
110
- count(label = "default") {
111
- const count2 = (this._counters.get(label) || 0) + 1;
112
- this._counters.set(label, count2);
113
- this.log(`${label}: ${count2}`);
114
- }
115
- countReset(label = "default") {
116
- this._counters.delete(label);
117
- }
118
- group(...args) {
119
- if (args.length > 0) this.log(...args);
120
- this._groupDepth++;
121
- }
122
- groupCollapsed(...args) {
123
- this.group(...args);
124
- }
125
- groupEnd() {
126
- if (this._groupDepth > 0) this._groupDepth--;
127
- }
128
- table(tabularData, _properties) {
129
- if (this._stdout) {
130
- this._write("stdout", tabularData);
131
- } else if (_isGJS) {
132
- print(JSON.stringify(tabularData, null, 2));
133
- } else {
134
- globalThis.console.table(tabularData, _properties);
135
- }
136
- }
137
- time(label = "default") {
138
- this._timers.set(label, Date.now());
139
- }
140
- timeEnd(label = "default") {
141
- const start = this._timers.get(label);
142
- if (start !== void 0) {
143
- this.log(`${label}: ${Date.now() - start}ms`);
144
- this._timers.delete(label);
145
- } else {
146
- this.warn(`Warning: No such label '${label}' for console.timeEnd()`);
147
- }
148
- }
149
- timeLog(label = "default", ...args) {
150
- const start = this._timers.get(label);
151
- if (start !== void 0) {
152
- this.log(`${label}: ${Date.now() - start}ms`, ...args);
153
- } else {
154
- this.warn(`Warning: No such label '${label}' for console.timeLog()`);
155
- }
156
- }
157
- trace(...args) {
158
- const err = new Error();
159
- const stack = err.stack?.split("\n").slice(1).join("\n") || "";
160
- this._write("stderr", "Trace:", ...args, "\n" + stack);
161
- }
162
- profile(_label) {
163
- }
164
- profileEnd(_label) {
165
- }
166
- timeStamp(_label) {
167
- }
4
+ const fmt = args[0];
5
+ const rest = args.slice(1);
6
+ if (typeof fmt !== "string" || !/%(s|d|i|f|o|O|c)/.test(fmt)) {
7
+ return args.map((a) => typeof a === "string" ? a : JSON.stringify(a)).join(" ");
8
+ }
9
+ let i = 0;
10
+ const result = fmt.replace(/%([sdifOoc])/g, (_match, spec) => {
11
+ if (i >= rest.length) return _match;
12
+ const val = rest[i++];
13
+ switch (spec) {
14
+ case "s": return String(val);
15
+ case "d":
16
+ case "i": return String(parseInt(String(val), 10));
17
+ case "f": return String(parseFloat(String(val)));
18
+ case "o":
19
+ case "O": return JSON.stringify(val);
20
+ case "c": return "";
21
+ default: return _match;
22
+ }
23
+ });
24
+ const remaining = rest.slice(i);
25
+ if (remaining.length === 0) return result;
26
+ return result + " " + remaining.map((a) => typeof a === "string" ? a : JSON.stringify(a)).join(" ");
168
27
  }
28
+ /**
29
+ * The Console class can be used to create a simple logger with configurable output streams.
30
+ * This implementation delegates to the global console for the basic case,
31
+ * and writes to custom streams when provided.
32
+ */
33
+ var Console = class {
34
+ _stdout;
35
+ _stderr;
36
+ _groupDepth = 0;
37
+ _groupIndentation;
38
+ _timers = new Map();
39
+ _counters = new Map();
40
+ constructor(stdoutOrOptions, stderr) {
41
+ if (stdoutOrOptions && typeof stdoutOrOptions.write === "function") {
42
+ this._stdout = stdoutOrOptions;
43
+ this._stderr = stderr || this._stdout;
44
+ } else if (stdoutOrOptions && typeof stdoutOrOptions === "object") {
45
+ const opts = stdoutOrOptions;
46
+ this._stdout = opts.stdout;
47
+ this._stderr = opts.stderr || opts.stdout;
48
+ }
49
+ this._groupIndentation = 2;
50
+ }
51
+ _write(stream, ...args) {
52
+ const target = stream === "stderr" ? this._stderr || this._stdout : this._stdout;
53
+ if (target) {
54
+ const indent = " ".repeat(this._groupDepth * this._groupIndentation);
55
+ const message = _formatArgs(...args);
56
+ target.write(indent + message + "\n");
57
+ } else if (_isGJS) {
58
+ const indent = " ".repeat(this._groupDepth * this._groupIndentation);
59
+ const message = indent + _formatArgs(...args);
60
+ if (stream === "stderr") {
61
+ printerr(message);
62
+ } else {
63
+ print(message);
64
+ }
65
+ } else {
66
+ const gc = globalThis.console;
67
+ if (stream === "stderr") {
68
+ gc.error(...args);
69
+ } else {
70
+ gc.log(...args);
71
+ }
72
+ }
73
+ }
74
+ log(...args) {
75
+ this._write("stdout", ...args);
76
+ }
77
+ info(...args) {
78
+ this._write("stdout", ...args);
79
+ }
80
+ debug(...args) {
81
+ this._write("stdout", ...args);
82
+ }
83
+ warn(...args) {
84
+ this._write("stderr", ...args);
85
+ }
86
+ error(...args) {
87
+ this._write("stderr", ...args);
88
+ }
89
+ dir(obj, _options) {
90
+ this._write("stdout", obj);
91
+ }
92
+ dirxml(...args) {
93
+ this.log(...args);
94
+ }
95
+ assert(value, ...args) {
96
+ if (!value) {
97
+ this.error("Assertion failed:", ...args);
98
+ }
99
+ }
100
+ clear() {
101
+ if (this._stdout) {
102
+ this._stdout.write("\x1Bc");
103
+ } else if (_isGJS) {
104
+ print("\x1Bc");
105
+ } else {
106
+ globalThis.console.clear();
107
+ }
108
+ }
109
+ count(label = "default") {
110
+ const count = (this._counters.get(label) || 0) + 1;
111
+ this._counters.set(label, count);
112
+ this.log(`${label}: ${count}`);
113
+ }
114
+ countReset(label = "default") {
115
+ this._counters.delete(label);
116
+ }
117
+ group(...args) {
118
+ if (args.length > 0) this.log(...args);
119
+ this._groupDepth++;
120
+ }
121
+ groupCollapsed(...args) {
122
+ this.group(...args);
123
+ }
124
+ groupEnd() {
125
+ if (this._groupDepth > 0) this._groupDepth--;
126
+ }
127
+ table(tabularData, _properties) {
128
+ if (this._stdout) {
129
+ this._write("stdout", tabularData);
130
+ } else if (_isGJS) {
131
+ print(JSON.stringify(tabularData, null, 2));
132
+ } else {
133
+ globalThis.console.table(tabularData, _properties);
134
+ }
135
+ }
136
+ time(label = "default") {
137
+ this._timers.set(label, Date.now());
138
+ }
139
+ timeEnd(label = "default") {
140
+ const start = this._timers.get(label);
141
+ if (start !== undefined) {
142
+ this.log(`${label}: ${Date.now() - start}ms`);
143
+ this._timers.delete(label);
144
+ } else {
145
+ this.warn(`Warning: No such label '${label}' for console.timeEnd()`);
146
+ }
147
+ }
148
+ timeLog(label = "default", ...args) {
149
+ const start = this._timers.get(label);
150
+ if (start !== undefined) {
151
+ this.log(`${label}: ${Date.now() - start}ms`, ...args);
152
+ } else {
153
+ this.warn(`Warning: No such label '${label}' for console.timeLog()`);
154
+ }
155
+ }
156
+ trace(...args) {
157
+ const err = new Error();
158
+ const stack = err.stack?.split("\n").slice(1).join("\n") || "";
159
+ this._write("stderr", "Trace:", ...args, "\n" + stack);
160
+ }
161
+ profile(_label) {}
162
+ profileEnd(_label) {}
163
+ timeStamp(_label) {}
164
+ };
169
165
  const _default = new Console();
170
166
  const log = (...args) => _default.log(...args);
171
167
  const info = (...args) => _default.info(...args);
@@ -186,61 +182,34 @@ const countReset = (label) => _default.countReset(label);
186
182
  const group = (...args) => _default.group(...args);
187
183
  const groupCollapsed = (...args) => _default.groupCollapsed(...args);
188
184
  const groupEnd = () => _default.groupEnd();
189
- const profile = (_label) => {
190
- };
191
- const profileEnd = (_label) => {
192
- };
193
- const timeStamp = (_label) => {
194
- };
185
+ const profile = (_label) => {};
186
+ const profileEnd = (_label) => {};
187
+ const timeStamp = (_label) => {};
195
188
  const consoleModule = {
196
- Console,
197
- log,
198
- info,
199
- debug,
200
- warn,
201
- error,
202
- dir,
203
- dirxml,
204
- table,
205
- time,
206
- timeEnd,
207
- timeLog,
208
- trace,
209
- assert,
210
- clear,
211
- count,
212
- countReset,
213
- group,
214
- groupCollapsed,
215
- groupEnd,
216
- profile,
217
- profileEnd,
218
- timeStamp
219
- };
220
- var index_default = consoleModule;
221
- export {
222
- Console,
223
- assert,
224
- clear,
225
- count,
226
- countReset,
227
- debug,
228
- index_default as default,
229
- dir,
230
- dirxml,
231
- error,
232
- group,
233
- groupCollapsed,
234
- groupEnd,
235
- info,
236
- log,
237
- profile,
238
- profileEnd,
239
- table,
240
- time,
241
- timeEnd,
242
- timeLog,
243
- timeStamp,
244
- trace,
245
- warn
189
+ Console,
190
+ log,
191
+ info,
192
+ debug,
193
+ warn,
194
+ error,
195
+ dir,
196
+ dirxml,
197
+ table,
198
+ time,
199
+ timeEnd,
200
+ timeLog,
201
+ trace,
202
+ assert,
203
+ clear,
204
+ count,
205
+ countReset,
206
+ group,
207
+ groupCollapsed,
208
+ groupEnd,
209
+ profile,
210
+ profileEnd,
211
+ timeStamp
246
212
  };
213
+
214
+ //#endregion
215
+ export { Console, assert, clear, count, countReset, debug, consoleModule as default, dir, dirxml, error, group, groupCollapsed, groupEnd, info, log, profile, profileEnd, table, time, timeEnd, timeLog, timeStamp, trace, warn };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gjsify/console",
3
- "version": "0.3.13",
3
+ "version": "0.3.14",
4
4
  "description": "Node.js console module for Gjs",
5
5
  "module": "lib/esm/index.js",
6
6
  "types": "lib/types/index.d.ts",
@@ -30,8 +30,8 @@
30
30
  "console"
31
31
  ],
32
32
  "devDependencies": {
33
- "@gjsify/cli": "^0.3.13",
34
- "@gjsify/unit": "^0.3.13",
33
+ "@gjsify/cli": "^0.3.14",
34
+ "@gjsify/unit": "^0.3.14",
35
35
  "@types/node": "^25.6.0",
36
36
  "typescript": "^6.0.3"
37
37
  }