rsformat 1.0.0 → 1.1.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.
- package/README.md +6 -4
- package/lib/format.d.ts +14 -11
- package/lib/format.js +28 -56
- package/lib/index.js +1 -1
- package/lib/print.js +2 -8
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -31,23 +31,25 @@ npm install rsformat
|
|
|
31
31
|
### Basic formatting and printing to console
|
|
32
32
|
|
|
33
33
|
The `rs` template tag can be used to enable rust-style formatting in a template.
|
|
34
|
-
|
|
34
|
+
|
|
35
|
+
To reference a previous or following argument, use `rs.ref` with the argument number. This is useful if you want to reuse a complicated expression without having to declare it separately.
|
|
35
36
|
|
|
36
37
|
```js
|
|
37
38
|
import { rs, println } from 'rsformat'; // ESM
|
|
38
39
|
const { rs, println } = require('rsformat'); // CommonJS
|
|
39
40
|
|
|
40
|
-
let number =
|
|
41
|
+
let number = 14;
|
|
41
42
|
|
|
42
|
-
let info = rs`${number} is ${rs.ref(0)}:x in hex`; // info == '15 is f in hex'
|
|
43
|
+
let info = rs`${number+1} is ${rs.ref(0)}:x in hex`; // info == '15 is f in hex'
|
|
43
44
|
```
|
|
44
45
|
|
|
45
|
-
> NB: templates tagged with `rs` are instances of a special class `RsString` that extends `String`, rather than a primitive value. This is to enable colors for debug formatting inside the printing functions. This difference should not affect normal usage, but `rs.raw` can be used as an alternative tag to get a primitive `string
|
|
46
|
+
> NB: templates tagged with `rs` are instances of a special class `RsString` that extends `String`, rather than a primitive value. This is to enable colors for debug formatting inside the printing functions. This difference should not affect normal usage, but `rs.raw` can be used as an alternative tag to get a primitive `string` with ANSI control characters escaped.
|
|
46
47
|
|
|
47
48
|
The printing functions can be called with plain strings, instances of `String` or templates formatted with `rs`:
|
|
48
49
|
|
|
49
50
|
```ts
|
|
50
51
|
println('Hello World');
|
|
52
|
+
println(`This template did ${'Not'} need fancy formatting`);
|
|
51
53
|
println(rs`...`);
|
|
52
54
|
```
|
|
53
55
|
|
package/lib/format.d.ts
CHANGED
|
@@ -3,23 +3,25 @@
|
|
|
3
3
|
* An extension of `String`.
|
|
4
4
|
*/
|
|
5
5
|
export declare class RsString extends String {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
private params;
|
|
6
|
+
/**
|
|
7
|
+
* A version of the string that includes ANSI escape codes for debug formatting.
|
|
8
|
+
*/
|
|
9
|
+
colored: string;
|
|
11
10
|
constructor(strings: TemplateStringsArray, params: any[]);
|
|
12
|
-
toString(): string;
|
|
13
|
-
valueOf(): string;
|
|
11
|
+
toString(debugColors?: boolean): string;
|
|
14
12
|
}
|
|
15
13
|
/**
|
|
16
|
-
* Format a template literal with rust-style formatting and return it as a string.
|
|
14
|
+
* Format a template literal with rust-style formatting and return it as a raw and colored string.
|
|
17
15
|
*
|
|
18
16
|
* @param strings String parts of the template
|
|
19
17
|
* @param params Template parameters
|
|
20
|
-
*
|
|
18
|
+
*
|
|
19
|
+
* @returns An object with raw and colored versions of the formatted parameter
|
|
21
20
|
*/
|
|
22
|
-
export declare function buildString(strings: TemplateStringsArray, params: any[]
|
|
21
|
+
export declare function buildString(strings: TemplateStringsArray, params: any[]): {
|
|
22
|
+
raw: string;
|
|
23
|
+
colored: string;
|
|
24
|
+
};
|
|
23
25
|
type AlignDirection = '<' | '^' | '>';
|
|
24
26
|
type Sign = '-' | '+' | '';
|
|
25
27
|
type FormatType = '?' | 'o' | 'x' | 'X' | 'b' | 'e' | 'E' | 'n' | 'N' | '';
|
|
@@ -35,11 +37,12 @@ type FormatSpecifier = {
|
|
|
35
37
|
};
|
|
36
38
|
/**
|
|
37
39
|
* Format a parameter as a string according to a specifier.
|
|
40
|
+
* Will include colors in the output of debug formating
|
|
38
41
|
*
|
|
39
42
|
* @param param parameter to format
|
|
40
43
|
* @param format format specifier object
|
|
41
44
|
* @param debugColors whether to use colors in debug formatting
|
|
42
45
|
* @returns `param` as a formatted string
|
|
43
46
|
*/
|
|
44
|
-
export declare function formatParam(param: any, format: FormatSpecifier
|
|
47
|
+
export declare function formatParam(param: any, format: FormatSpecifier): string;
|
|
45
48
|
export {};
|
package/lib/format.js
CHANGED
|
@@ -12,48 +12,32 @@ const error = (param, char, reason) => new Error(`rs[param ${param}, char ${char
|
|
|
12
12
|
* An extension of `String`.
|
|
13
13
|
*/
|
|
14
14
|
class RsString extends String {
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
params;
|
|
15
|
+
/**
|
|
16
|
+
* A version of the string that includes ANSI escape codes for debug formatting.
|
|
17
|
+
*/
|
|
18
|
+
colored;
|
|
20
19
|
constructor(strings, params) {
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
params: { value: params, enumerable: false },
|
|
25
|
-
cachedPlain: { value: null, writable: true, enumerable: false },
|
|
26
|
-
cachedColor: { value: null, writable: true, enumerable: false },
|
|
27
|
-
__debugColors: { value: false, writable: true, enumerable: false },
|
|
28
|
-
});
|
|
29
|
-
}
|
|
30
|
-
toString() {
|
|
31
|
-
if (this.__debugColors) {
|
|
32
|
-
if (this.cachedColor === null)
|
|
33
|
-
this.cachedColor = buildString(this.strings, this.params, true);
|
|
34
|
-
return this.cachedColor;
|
|
35
|
-
}
|
|
36
|
-
else {
|
|
37
|
-
if (this.cachedPlain === null)
|
|
38
|
-
this.cachedPlain = buildString(this.strings, this.params, false);
|
|
39
|
-
return this.cachedPlain;
|
|
40
|
-
}
|
|
41
|
-
;
|
|
20
|
+
let { raw, colored } = buildString(strings, params);
|
|
21
|
+
super(raw);
|
|
22
|
+
this.colored = colored;
|
|
42
23
|
}
|
|
43
|
-
|
|
44
|
-
|
|
24
|
+
toString(debugColors = false) {
|
|
25
|
+
if (debugColors)
|
|
26
|
+
return this.colored;
|
|
27
|
+
return this.valueOf();
|
|
45
28
|
}
|
|
46
29
|
}
|
|
47
30
|
exports.RsString = RsString;
|
|
48
31
|
/**
|
|
49
|
-
* Format a template literal with rust-style formatting and return it as a string.
|
|
32
|
+
* Format a template literal with rust-style formatting and return it as a raw and colored string.
|
|
50
33
|
*
|
|
51
34
|
* @param strings String parts of the template
|
|
52
35
|
* @param params Template parameters
|
|
53
|
-
*
|
|
36
|
+
*
|
|
37
|
+
* @returns An object with raw and colored versions of the formatted parameter
|
|
54
38
|
*/
|
|
55
|
-
function buildString(strings, params
|
|
56
|
-
let out =
|
|
39
|
+
function buildString(strings, params) {
|
|
40
|
+
let out = strings[0];
|
|
57
41
|
for (let i = 1; i < strings.length; ++i) {
|
|
58
42
|
let string = strings[i];
|
|
59
43
|
let param = params[i - 1];
|
|
@@ -73,12 +57,12 @@ function buildString(strings, params, debugColors = false) {
|
|
|
73
57
|
// If it has two the first : is being escaped and can be removed
|
|
74
58
|
if (string[0] == ':') {
|
|
75
59
|
if (string[1] == ':') {
|
|
76
|
-
out
|
|
60
|
+
out += param.toString() + string.substring(1);
|
|
77
61
|
continue;
|
|
78
62
|
}
|
|
79
63
|
}
|
|
80
64
|
else {
|
|
81
|
-
out
|
|
65
|
+
out += param.toString() + string;
|
|
82
66
|
continue;
|
|
83
67
|
}
|
|
84
68
|
;
|
|
@@ -163,23 +147,23 @@ function buildString(strings, params, debugColors = false) {
|
|
|
163
147
|
width,
|
|
164
148
|
precision,
|
|
165
149
|
type: format_type
|
|
166
|
-
}
|
|
167
|
-
out
|
|
150
|
+
});
|
|
151
|
+
out += formatted + string.substring(idx);
|
|
168
152
|
}
|
|
169
|
-
return
|
|
153
|
+
return { raw: node_util_1.default.stripVTControlCharacters(out), colored: out };
|
|
170
154
|
}
|
|
171
155
|
exports.buildString = buildString;
|
|
172
156
|
/**
|
|
173
157
|
* Format a parameter as a string according to a specifier.
|
|
158
|
+
* Will include colors in the output of debug formating
|
|
174
159
|
*
|
|
175
160
|
* @param param parameter to format
|
|
176
161
|
* @param format format specifier object
|
|
177
162
|
* @param debugColors whether to use colors in debug formatting
|
|
178
163
|
* @returns `param` as a formatted string
|
|
179
164
|
*/
|
|
180
|
-
function formatParam(param, format
|
|
165
|
+
function formatParam(param, format) {
|
|
181
166
|
let param_type = typeof param;
|
|
182
|
-
let true_length = -1;
|
|
183
167
|
// Process parameter type
|
|
184
168
|
switch (format.type) {
|
|
185
169
|
case 'o':
|
|
@@ -238,14 +222,9 @@ function formatParam(param, format, debugColors) {
|
|
|
238
222
|
format.precision = -1;
|
|
239
223
|
break;
|
|
240
224
|
case '?':
|
|
241
|
-
true_length = node_util_1.default.inspect(param, {
|
|
242
|
-
depth: Infinity,
|
|
243
|
-
colors: false,
|
|
244
|
-
compact: !format.pretty
|
|
245
|
-
}).length;
|
|
246
225
|
param = node_util_1.default.inspect(param, {
|
|
247
226
|
depth: Infinity,
|
|
248
|
-
colors:
|
|
227
|
+
colors: true,
|
|
249
228
|
compact: !format.pretty
|
|
250
229
|
});
|
|
251
230
|
// Do not force sign, pad with zeroes or align to precision when using debug formatting
|
|
@@ -256,9 +235,6 @@ function formatParam(param, format, debugColors) {
|
|
|
256
235
|
break;
|
|
257
236
|
}
|
|
258
237
|
;
|
|
259
|
-
if (true_length == -1) {
|
|
260
|
-
true_length = param.length;
|
|
261
|
-
}
|
|
262
238
|
// Compute radix-point precision on numbers
|
|
263
239
|
if (param_type == 'number' && format.precision != -1) {
|
|
264
240
|
let [pre, post] = param.split('.');
|
|
@@ -269,10 +245,8 @@ function formatParam(param, format, debugColors) {
|
|
|
269
245
|
post = ((post || '') + '0'.repeat(format.precision)).slice(0, format.precision);
|
|
270
246
|
param = pre + '.' + post;
|
|
271
247
|
}
|
|
272
|
-
// Update true length for fill/align
|
|
273
|
-
true_length = param.length;
|
|
274
248
|
}
|
|
275
|
-
let filled = false;
|
|
249
|
+
// let filled = false;
|
|
276
250
|
if ((param_type == 'number') || (param_type == 'bigint')) {
|
|
277
251
|
// Compute parameter sign
|
|
278
252
|
let maybe_sign = param.substring(0, 1);
|
|
@@ -305,20 +279,18 @@ function formatParam(param, format, debugColors) {
|
|
|
305
279
|
}
|
|
306
280
|
//pad with zeroes if specified
|
|
307
281
|
if (format.pad_zeroes) {
|
|
308
|
-
filled = true;
|
|
282
|
+
// filled = true;
|
|
309
283
|
while (param.length < format.width - maybe_sign.length) {
|
|
310
284
|
param = '0' + param;
|
|
311
|
-
true_length++;
|
|
312
285
|
}
|
|
313
286
|
}
|
|
314
|
-
true_length += maybe_sign.length;
|
|
315
287
|
param = maybe_sign + param;
|
|
316
288
|
}
|
|
317
|
-
if (
|
|
289
|
+
if ( /*!filled && */format.width > param.length) {
|
|
318
290
|
// Compute fill/align
|
|
319
291
|
let left = '';
|
|
320
292
|
let right = '';
|
|
321
|
-
let diff = format.width -
|
|
293
|
+
let diff = format.width - param.length;
|
|
322
294
|
switch (format.align) {
|
|
323
295
|
case '>':
|
|
324
296
|
left = format.fill.repeat(diff);
|
package/lib/index.js
CHANGED
|
@@ -24,7 +24,7 @@ exports.rs = rs;
|
|
|
24
24
|
* @returns a string primitive of the formatted string
|
|
25
25
|
*/
|
|
26
26
|
rs.raw = function (strings, ...params) {
|
|
27
|
-
return (0, format_1.buildString)(strings, params);
|
|
27
|
+
return (0, format_1.buildString)(strings, params).raw;
|
|
28
28
|
};
|
|
29
29
|
/**
|
|
30
30
|
* Reference another parameter in a `rs`-tagged template.
|
package/lib/print.js
CHANGED
|
@@ -4,7 +4,6 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
exports.dbg = exports.eprintln = exports.eprint = exports.println = exports.print = exports.printToStream = void 0;
|
|
7
|
-
const format_1 = require("./format");
|
|
8
7
|
const node_process_1 = __importDefault(require("node:process"));
|
|
9
8
|
const _1 = require(".");
|
|
10
9
|
/**
|
|
@@ -16,13 +15,8 @@ const _1 = require(".");
|
|
|
16
15
|
* @param colored Whether to use colors for `rs` debug formatting
|
|
17
16
|
*/
|
|
18
17
|
function printToStream(stream, string, newline = false, colored = false) {
|
|
19
|
-
if (string instanceof
|
|
20
|
-
|
|
21
|
-
if (colored)
|
|
22
|
-
string.__debugColors = true;
|
|
23
|
-
let stringified = string.toString();
|
|
24
|
-
string.__debugColors = previousColors;
|
|
25
|
-
string = stringified;
|
|
18
|
+
if (string instanceof String) {
|
|
19
|
+
string = string.toString(true);
|
|
26
20
|
}
|
|
27
21
|
if (newline)
|
|
28
22
|
string = string + '\n';
|