balagan 0.0.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/LICENSE +21 -0
- package/README.md +48 -0
- package/dist/index.cjs +157 -0
- package/dist/index.d.cts +26 -0
- package/dist/index.d.cts.map +1 -0
- package/dist/index.d.ts +26 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +146 -0
- package/package.json +54 -0
- package/src/index.ts +196 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Gal Schlezinger
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
# balagan
|
|
2
|
+
|
|
3
|
+
🙅 put some order to all of this mess
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm i balagan
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
```ts
|
|
14
|
+
import { help, hint, note, code, frame, inline } from "balagan";
|
|
15
|
+
|
|
16
|
+
// Styled messages
|
|
17
|
+
console.log(help("run with --verbose for more info"));
|
|
18
|
+
console.log(hint("try using a different port"));
|
|
19
|
+
console.log(note("this feature is experimental"));
|
|
20
|
+
|
|
21
|
+
// Inline code styling
|
|
22
|
+
console.log(`Use ${code("npm start")} to begin`);
|
|
23
|
+
|
|
24
|
+
// Frame content in a tree structure
|
|
25
|
+
console.log(
|
|
26
|
+
frame("2 errors emitted", ["Missing semicolon", "Undefined variable"]),
|
|
27
|
+
);
|
|
28
|
+
|
|
29
|
+
// Annotate code with explanations (compiler-style)
|
|
30
|
+
console.log(inline`function ${["hello", "invalid name"]}() {}`);
|
|
31
|
+
// Output:
|
|
32
|
+
// function hello() {}
|
|
33
|
+
// ──┬──
|
|
34
|
+
// â•°â–¶ invalid name
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
## API
|
|
38
|
+
|
|
39
|
+
- `help(msg)` - Blue "help:" prefixed message
|
|
40
|
+
- `hint(msg)` - Blue "hint:" prefixed message
|
|
41
|
+
- `note(msg)` - Blue "note:" prefixed message
|
|
42
|
+
- `code(str)` - Inline code with backticks
|
|
43
|
+
- `frame(text, contents)` - Tree-framed content
|
|
44
|
+
- `inline` - Template tag for inline annotations with underlines
|
|
45
|
+
|
|
46
|
+
## License
|
|
47
|
+
|
|
48
|
+
MIT
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
2
|
+
|
|
3
|
+
var pico = require('picocolors');
|
|
4
|
+
|
|
5
|
+
function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
|
|
6
|
+
|
|
7
|
+
var pico__default = /*#__PURE__*/_interopDefault(pico);
|
|
8
|
+
|
|
9
|
+
const styles = {
|
|
10
|
+
info: pico__default.default.blue,
|
|
11
|
+
help: pico__default.default.cyan,
|
|
12
|
+
warn: pico__default.default.yellow,
|
|
13
|
+
error: pico__default.default.red
|
|
14
|
+
};
|
|
15
|
+
function help(messages) {
|
|
16
|
+
const message = Array.isArray(messages) ? messages.join("\n") : messages;
|
|
17
|
+
return styles.help(`${pico__default.default.bold("help:")} ${message}`);
|
|
18
|
+
}
|
|
19
|
+
function hint(messages) {
|
|
20
|
+
const message = Array.isArray(messages) ? messages.join("\n") : messages;
|
|
21
|
+
return styles.info(`${pico__default.default.bold("hint:")} ${message}`);
|
|
22
|
+
}
|
|
23
|
+
function note(messages) {
|
|
24
|
+
const message = Array.isArray(messages) ? messages.join("\n") : messages;
|
|
25
|
+
return styles.info(`${pico__default.default.bold("note:")} ${message}`);
|
|
26
|
+
}
|
|
27
|
+
function code(str) {
|
|
28
|
+
return pico__default.default.italic(`${pico__default.default.dim("`")}${str}${pico__default.default.dim("`")}`);
|
|
29
|
+
}
|
|
30
|
+
function frame(text, contents) {
|
|
31
|
+
const result = [
|
|
32
|
+
text
|
|
33
|
+
];
|
|
34
|
+
contents.forEach((content, index)=>{
|
|
35
|
+
const lines = content.split("\n");
|
|
36
|
+
const isLastContent = index === contents.length - 1;
|
|
37
|
+
const firstLinePrefix = isLastContent ? "╰▶ " : "├▶ ";
|
|
38
|
+
const continuationPrefix = isLastContent ? " " : "│ ";
|
|
39
|
+
const framedLines = lines.map((line, lineIndex)=>{
|
|
40
|
+
const prefix = lineIndex === 0 ? firstLinePrefix : continuationPrefix;
|
|
41
|
+
return `${prefix}${line}`;
|
|
42
|
+
});
|
|
43
|
+
result.push(...framedLines);
|
|
44
|
+
});
|
|
45
|
+
return result.join("\n");
|
|
46
|
+
}
|
|
47
|
+
const identity = (s)=>s;
|
|
48
|
+
function getMarkerMidpoint(marker) {
|
|
49
|
+
const textLen = marker.endCol - marker.startCol;
|
|
50
|
+
return marker.startCol + Math.floor(textLen / 2);
|
|
51
|
+
}
|
|
52
|
+
function buildUnderline(markers) {
|
|
53
|
+
const parts = [];
|
|
54
|
+
let pos = 0;
|
|
55
|
+
for (const marker of markers){
|
|
56
|
+
const textLen = marker.endCol - marker.startCol;
|
|
57
|
+
const midPoint = Math.floor(textLen / 2);
|
|
58
|
+
if (marker.startCol > pos) {
|
|
59
|
+
parts.push(" ".repeat(marker.startCol - pos));
|
|
60
|
+
pos = marker.startCol;
|
|
61
|
+
}
|
|
62
|
+
const segment = `${"─".repeat(midPoint)}┬${"─".repeat(textLen - midPoint - 1)}`;
|
|
63
|
+
const colorFn = marker.color ?? identity;
|
|
64
|
+
parts.push(colorFn(segment));
|
|
65
|
+
pos += textLen;
|
|
66
|
+
}
|
|
67
|
+
return parts.join("");
|
|
68
|
+
}
|
|
69
|
+
function buildExplanationLine(marker, midCol, remainingMids, isOnlyMarker) {
|
|
70
|
+
let line = "â•°";
|
|
71
|
+
let pos = midCol + 1;
|
|
72
|
+
for (const nextMid of remainingMids){
|
|
73
|
+
while(pos < nextMid){
|
|
74
|
+
line += "─";
|
|
75
|
+
pos++;
|
|
76
|
+
}
|
|
77
|
+
line += "┼";
|
|
78
|
+
pos++;
|
|
79
|
+
}
|
|
80
|
+
const arrow = isOnlyMarker ? "▶ " : "─▶ ";
|
|
81
|
+
line += arrow + marker.explain;
|
|
82
|
+
const colorFn = marker.color ?? identity;
|
|
83
|
+
return " ".repeat(midCol) + colorFn(line);
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* @example
|
|
87
|
+
* inline`function ${{text: "hello", explain: "name not allowed bro"}}() {\n return 666\n}`;
|
|
88
|
+
* =>
|
|
89
|
+
* function hello() {
|
|
90
|
+
* ──┬──
|
|
91
|
+
* â•°â–¶ name not allowed bro
|
|
92
|
+
* return 666
|
|
93
|
+
* }
|
|
94
|
+
*/ function inline(text, ...values) {
|
|
95
|
+
const resultLines = [];
|
|
96
|
+
let currentLine = "";
|
|
97
|
+
let currentLineVisualLen = 0;
|
|
98
|
+
let pendingMarkers = [];
|
|
99
|
+
const flushLine = ()=>{
|
|
100
|
+
resultLines.push(currentLine);
|
|
101
|
+
if (pendingMarkers.length === 0) {
|
|
102
|
+
currentLine = "";
|
|
103
|
+
currentLineVisualLen = 0;
|
|
104
|
+
return;
|
|
105
|
+
}
|
|
106
|
+
const markerMids = pendingMarkers.map(getMarkerMidpoint);
|
|
107
|
+
resultLines.push(buildUnderline(pendingMarkers));
|
|
108
|
+
for(let i = 0; i < pendingMarkers.length; i++){
|
|
109
|
+
const line = buildExplanationLine(pendingMarkers[i], markerMids[i], markerMids.slice(i + 1), pendingMarkers.length === 1);
|
|
110
|
+
resultLines.push(line);
|
|
111
|
+
}
|
|
112
|
+
pendingMarkers = [];
|
|
113
|
+
currentLine = "";
|
|
114
|
+
currentLineVisualLen = 0;
|
|
115
|
+
};
|
|
116
|
+
for(let i = 0; i < text.length; i++){
|
|
117
|
+
const segment = text[i];
|
|
118
|
+
const lines = segment.split("\n");
|
|
119
|
+
for(let j = 0; j < lines.length; j++){
|
|
120
|
+
if (j > 0) {
|
|
121
|
+
flushLine();
|
|
122
|
+
}
|
|
123
|
+
currentLine += lines[j];
|
|
124
|
+
currentLineVisualLen += lines[j].length;
|
|
125
|
+
}
|
|
126
|
+
if (i < values.length) {
|
|
127
|
+
const val = values[i];
|
|
128
|
+
const value = !Array.isArray(val) ? val : {
|
|
129
|
+
text: val[0],
|
|
130
|
+
explain: val[1],
|
|
131
|
+
...val[2]
|
|
132
|
+
};
|
|
133
|
+
const startCol = currentLineVisualLen;
|
|
134
|
+
const colorFn = value.color ?? ((s)=>s);
|
|
135
|
+
currentLine += colorFn(value.text);
|
|
136
|
+
currentLineVisualLen += value.text.length;
|
|
137
|
+
const endCol = currentLineVisualLen;
|
|
138
|
+
pendingMarkers.push({
|
|
139
|
+
startCol,
|
|
140
|
+
endCol,
|
|
141
|
+
explain: value.explain,
|
|
142
|
+
color: value.color
|
|
143
|
+
});
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
if (currentLine || pendingMarkers.length > 0) {
|
|
147
|
+
flushLine();
|
|
148
|
+
}
|
|
149
|
+
return resultLines.join("\n");
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
exports.code = code;
|
|
153
|
+
exports.frame = frame;
|
|
154
|
+
exports.help = help;
|
|
155
|
+
exports.hint = hint;
|
|
156
|
+
exports.inline = inline;
|
|
157
|
+
exports.note = note;
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
declare function help(messages: string | string[]): string;
|
|
2
|
+
declare function hint(messages: string | string[]): string;
|
|
3
|
+
declare function note(messages: string | string[]): string;
|
|
4
|
+
declare function code(str: string): string;
|
|
5
|
+
declare function frame(text: string, contents: string[]): string;
|
|
6
|
+
interface Explain {
|
|
7
|
+
text: string;
|
|
8
|
+
explain: string;
|
|
9
|
+
/** adds ansi coloring */
|
|
10
|
+
color?: (s: string) => string;
|
|
11
|
+
}
|
|
12
|
+
type Explainish = Explain | [text: string, explain: string, opts?: Pick<Explain, "color">];
|
|
13
|
+
/**
|
|
14
|
+
* @example
|
|
15
|
+
* inline`function ${{text: "hello", explain: "name not allowed bro"}}() {\n return 666\n}`;
|
|
16
|
+
* =>
|
|
17
|
+
* function hello() {
|
|
18
|
+
* ──┬──
|
|
19
|
+
* â•°â–¶ name not allowed bro
|
|
20
|
+
* return 666
|
|
21
|
+
* }
|
|
22
|
+
*/
|
|
23
|
+
declare function inline(text: TemplateStringsArray, ...values: Explainish[]): string;
|
|
24
|
+
|
|
25
|
+
export { code, frame, help, hint, inline, note };
|
|
26
|
+
//# sourceMappingURL=index.d.cts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.cts","sources":["../src/index.ts"],"sourcesContent":["import pico from \"picocolors\";\n\nconst styles = {\n info: pico.blue,\n help: pico.cyan,\n warn: pico.yellow,\n error: pico.red,\n};\n\nexport function help(messages: string | string[]) {\n const message = Array.isArray(messages) ? messages.join(\"\\n\") : messages;\n return styles.help(`${pico.bold(\"help:\")} ${message}`);\n}\n\nexport function hint(messages: string | string[]) {\n const message = Array.isArray(messages) ? messages.join(\"\\n\") : messages;\n return styles.info(`${pico.bold(\"hint:\")} ${message}`);\n}\n\nexport function note(messages: string | string[]) {\n const message = Array.isArray(messages) ? messages.join(\"\\n\") : messages;\n return styles.info(`${pico.bold(\"note:\")} ${message}`);\n}\n\nexport function code(str: string) {\n return pico.italic(`${pico.dim(\"`\")}${str}${pico.dim(\"`\")}`);\n}\n\nexport function frame(text: string, contents: string[]): string {\n const result = [text];\n\n contents.forEach((content, index) => {\n const lines = content.split(\"\\n\");\n const isLastContent = index === contents.length - 1;\n\n const firstLinePrefix = isLastContent ? \"╰▶ \" : \"├▶ \";\n const continuationPrefix = isLastContent ? \" \" : \"│ \";\n\n const framedLines = lines.map((line, lineIndex) => {\n const prefix = lineIndex === 0 ? firstLinePrefix : continuationPrefix;\n return `${prefix}${line}`;\n });\n\n result.push(...framedLines);\n });\n\n return result.join(\"\\n\");\n}\n\ninterface Explain {\n text: string;\n explain: string;\n /** adds ansi coloring */\n color?: (s: string) => string;\n}\n\ntype Explainish = Explain | [text: string, explain: string, opts?: Pick<Explain, \"color\">];\n\ntype Marker = {\n startCol: number;\n endCol: number;\n explain: string;\n color?: (s: string) => string;\n};\n\nconst identity = (s: string) => s;\n\nfunction getMarkerMidpoint(marker: Marker): number {\n const textLen = marker.endCol - marker.startCol;\n return marker.startCol + Math.floor(textLen / 2);\n}\n\nfunction buildUnderline(markers: Marker[]): string {\n const parts: string[] = [];\n let pos = 0;\n for (const marker of markers) {\n const textLen = marker.endCol - marker.startCol;\n const midPoint = Math.floor(textLen / 2);\n\n if (marker.startCol > pos) {\n parts.push(\" \".repeat(marker.startCol - pos));\n pos = marker.startCol;\n }\n const segment = `${\"─\".repeat(midPoint)}┬${\"─\".repeat(textLen - midPoint - 1)}`;\n const colorFn = marker.color ?? identity;\n parts.push(colorFn(segment));\n pos += textLen;\n }\n return parts.join(\"\");\n}\n\nfunction buildExplanationLine(\n marker: Marker,\n midCol: number,\n remainingMids: number[],\n isOnlyMarker: boolean,\n): string {\n let line = \"╰\";\n let pos = midCol + 1;\n\n for (const nextMid of remainingMids) {\n while (pos < nextMid) {\n line += \"─\";\n pos++;\n }\n line += \"┼\";\n pos++;\n }\n\n const arrow = isOnlyMarker ? \"▶ \" : \"─▶ \";\n line += arrow + marker.explain;\n\n const colorFn = marker.color ?? identity;\n return \" \".repeat(midCol) + colorFn(line);\n}\n\n/**\n * @example\n * inline`function ${{text: \"hello\", explain: \"name not allowed bro\"}}() {\\n return 666\\n}`;\n * =>\n * function hello() {\n * ──┬──\n * ╰▶ name not allowed bro\n * return 666\n * }\n */\nexport function inline(text: TemplateStringsArray, ...values: Explainish[]): string {\n const resultLines: string[] = [];\n let currentLine = \"\";\n let currentLineVisualLen = 0;\n let pendingMarkers: Marker[] = [];\n\n const flushLine = () => {\n resultLines.push(currentLine);\n if (pendingMarkers.length === 0) {\n currentLine = \"\";\n currentLineVisualLen = 0;\n return;\n }\n\n const markerMids = pendingMarkers.map(getMarkerMidpoint);\n\n resultLines.push(buildUnderline(pendingMarkers));\n\n for (let i = 0; i < pendingMarkers.length; i++) {\n const line = buildExplanationLine(\n pendingMarkers[i],\n markerMids[i],\n markerMids.slice(i + 1),\n pendingMarkers.length === 1,\n );\n resultLines.push(line);\n }\n\n pendingMarkers = [];\n currentLine = \"\";\n currentLineVisualLen = 0;\n };\n\n for (let i = 0; i < text.length; i++) {\n const segment = text[i];\n const lines = segment.split(\"\\n\");\n\n for (let j = 0; j < lines.length; j++) {\n if (j > 0) {\n flushLine();\n }\n currentLine += lines[j];\n currentLineVisualLen += lines[j].length;\n }\n\n if (i < values.length) {\n const val = values[i];\n const value: Explain = !Array.isArray(val)\n ? val\n : { text: val[0], explain: val[1], ...val[2] };\n const startCol = currentLineVisualLen;\n const colorFn = value.color ?? ((s: string) => s);\n currentLine += colorFn(value.text);\n currentLineVisualLen += value.text.length;\n const endCol = currentLineVisualLen;\n pendingMarkers.push({\n startCol,\n endCol,\n explain: value.explain,\n color: value.color,\n });\n }\n }\n\n if (currentLine || pendingMarkers.length > 0) {\n flushLine();\n }\n\n return resultLines.join(\"\\n\");\n}\n"],"names":[],"mappings":"AAAO,iBAAA,IAAA;AACA,iBAAA,IAAA;AACA,iBAAA,IAAA;AACA,iBAAA,IAAA;AACA,iBAAA,KAAA;AACP,UAAA,OAAA;AACA;AACA;AACA;AACA;AACA;AACA,KAAA,UAAA,GAAA,OAAA,0CAAA,IAAA,CAAA,OAAA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACO,iBAAA,MAAA,OAAA,oBAAA,aAAA,UAAA;;;;"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
declare function help(messages: string | string[]): string;
|
|
2
|
+
declare function hint(messages: string | string[]): string;
|
|
3
|
+
declare function note(messages: string | string[]): string;
|
|
4
|
+
declare function code(str: string): string;
|
|
5
|
+
declare function frame(text: string, contents: string[]): string;
|
|
6
|
+
interface Explain {
|
|
7
|
+
text: string;
|
|
8
|
+
explain: string;
|
|
9
|
+
/** adds ansi coloring */
|
|
10
|
+
color?: (s: string) => string;
|
|
11
|
+
}
|
|
12
|
+
type Explainish = Explain | [text: string, explain: string, opts?: Pick<Explain, "color">];
|
|
13
|
+
/**
|
|
14
|
+
* @example
|
|
15
|
+
* inline`function ${{text: "hello", explain: "name not allowed bro"}}() {\n return 666\n}`;
|
|
16
|
+
* =>
|
|
17
|
+
* function hello() {
|
|
18
|
+
* ──┬──
|
|
19
|
+
* â•°â–¶ name not allowed bro
|
|
20
|
+
* return 666
|
|
21
|
+
* }
|
|
22
|
+
*/
|
|
23
|
+
declare function inline(text: TemplateStringsArray, ...values: Explainish[]): string;
|
|
24
|
+
|
|
25
|
+
export { code, frame, help, hint, inline, note };
|
|
26
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sources":["../src/index.ts"],"sourcesContent":["import pico from \"picocolors\";\n\nconst styles = {\n info: pico.blue,\n help: pico.cyan,\n warn: pico.yellow,\n error: pico.red,\n};\n\nexport function help(messages: string | string[]) {\n const message = Array.isArray(messages) ? messages.join(\"\\n\") : messages;\n return styles.help(`${pico.bold(\"help:\")} ${message}`);\n}\n\nexport function hint(messages: string | string[]) {\n const message = Array.isArray(messages) ? messages.join(\"\\n\") : messages;\n return styles.info(`${pico.bold(\"hint:\")} ${message}`);\n}\n\nexport function note(messages: string | string[]) {\n const message = Array.isArray(messages) ? messages.join(\"\\n\") : messages;\n return styles.info(`${pico.bold(\"note:\")} ${message}`);\n}\n\nexport function code(str: string) {\n return pico.italic(`${pico.dim(\"`\")}${str}${pico.dim(\"`\")}`);\n}\n\nexport function frame(text: string, contents: string[]): string {\n const result = [text];\n\n contents.forEach((content, index) => {\n const lines = content.split(\"\\n\");\n const isLastContent = index === contents.length - 1;\n\n const firstLinePrefix = isLastContent ? \"╰▶ \" : \"├▶ \";\n const continuationPrefix = isLastContent ? \" \" : \"│ \";\n\n const framedLines = lines.map((line, lineIndex) => {\n const prefix = lineIndex === 0 ? firstLinePrefix : continuationPrefix;\n return `${prefix}${line}`;\n });\n\n result.push(...framedLines);\n });\n\n return result.join(\"\\n\");\n}\n\ninterface Explain {\n text: string;\n explain: string;\n /** adds ansi coloring */\n color?: (s: string) => string;\n}\n\ntype Explainish = Explain | [text: string, explain: string, opts?: Pick<Explain, \"color\">];\n\ntype Marker = {\n startCol: number;\n endCol: number;\n explain: string;\n color?: (s: string) => string;\n};\n\nconst identity = (s: string) => s;\n\nfunction getMarkerMidpoint(marker: Marker): number {\n const textLen = marker.endCol - marker.startCol;\n return marker.startCol + Math.floor(textLen / 2);\n}\n\nfunction buildUnderline(markers: Marker[]): string {\n const parts: string[] = [];\n let pos = 0;\n for (const marker of markers) {\n const textLen = marker.endCol - marker.startCol;\n const midPoint = Math.floor(textLen / 2);\n\n if (marker.startCol > pos) {\n parts.push(\" \".repeat(marker.startCol - pos));\n pos = marker.startCol;\n }\n const segment = `${\"─\".repeat(midPoint)}┬${\"─\".repeat(textLen - midPoint - 1)}`;\n const colorFn = marker.color ?? identity;\n parts.push(colorFn(segment));\n pos += textLen;\n }\n return parts.join(\"\");\n}\n\nfunction buildExplanationLine(\n marker: Marker,\n midCol: number,\n remainingMids: number[],\n isOnlyMarker: boolean,\n): string {\n let line = \"╰\";\n let pos = midCol + 1;\n\n for (const nextMid of remainingMids) {\n while (pos < nextMid) {\n line += \"─\";\n pos++;\n }\n line += \"┼\";\n pos++;\n }\n\n const arrow = isOnlyMarker ? \"▶ \" : \"─▶ \";\n line += arrow + marker.explain;\n\n const colorFn = marker.color ?? identity;\n return \" \".repeat(midCol) + colorFn(line);\n}\n\n/**\n * @example\n * inline`function ${{text: \"hello\", explain: \"name not allowed bro\"}}() {\\n return 666\\n}`;\n * =>\n * function hello() {\n * ──┬──\n * ╰▶ name not allowed bro\n * return 666\n * }\n */\nexport function inline(text: TemplateStringsArray, ...values: Explainish[]): string {\n const resultLines: string[] = [];\n let currentLine = \"\";\n let currentLineVisualLen = 0;\n let pendingMarkers: Marker[] = [];\n\n const flushLine = () => {\n resultLines.push(currentLine);\n if (pendingMarkers.length === 0) {\n currentLine = \"\";\n currentLineVisualLen = 0;\n return;\n }\n\n const markerMids = pendingMarkers.map(getMarkerMidpoint);\n\n resultLines.push(buildUnderline(pendingMarkers));\n\n for (let i = 0; i < pendingMarkers.length; i++) {\n const line = buildExplanationLine(\n pendingMarkers[i],\n markerMids[i],\n markerMids.slice(i + 1),\n pendingMarkers.length === 1,\n );\n resultLines.push(line);\n }\n\n pendingMarkers = [];\n currentLine = \"\";\n currentLineVisualLen = 0;\n };\n\n for (let i = 0; i < text.length; i++) {\n const segment = text[i];\n const lines = segment.split(\"\\n\");\n\n for (let j = 0; j < lines.length; j++) {\n if (j > 0) {\n flushLine();\n }\n currentLine += lines[j];\n currentLineVisualLen += lines[j].length;\n }\n\n if (i < values.length) {\n const val = values[i];\n const value: Explain = !Array.isArray(val)\n ? val\n : { text: val[0], explain: val[1], ...val[2] };\n const startCol = currentLineVisualLen;\n const colorFn = value.color ?? ((s: string) => s);\n currentLine += colorFn(value.text);\n currentLineVisualLen += value.text.length;\n const endCol = currentLineVisualLen;\n pendingMarkers.push({\n startCol,\n endCol,\n explain: value.explain,\n color: value.color,\n });\n }\n }\n\n if (currentLine || pendingMarkers.length > 0) {\n flushLine();\n }\n\n return resultLines.join(\"\\n\");\n}\n"],"names":[],"mappings":"AAAO,iBAAA,IAAA;AACA,iBAAA,IAAA;AACA,iBAAA,IAAA;AACA,iBAAA,IAAA;AACA,iBAAA,KAAA;AACP,UAAA,OAAA;AACA;AACA;AACA;AACA;AACA;AACA,KAAA,UAAA,GAAA,OAAA,0CAAA,IAAA,CAAA,OAAA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACO,iBAAA,MAAA,OAAA,oBAAA,aAAA,UAAA;;;;"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
import pico from 'picocolors';
|
|
2
|
+
|
|
3
|
+
const styles = {
|
|
4
|
+
info: pico.blue,
|
|
5
|
+
help: pico.cyan,
|
|
6
|
+
warn: pico.yellow,
|
|
7
|
+
error: pico.red
|
|
8
|
+
};
|
|
9
|
+
function help(messages) {
|
|
10
|
+
const message = Array.isArray(messages) ? messages.join("\n") : messages;
|
|
11
|
+
return styles.help(`${pico.bold("help:")} ${message}`);
|
|
12
|
+
}
|
|
13
|
+
function hint(messages) {
|
|
14
|
+
const message = Array.isArray(messages) ? messages.join("\n") : messages;
|
|
15
|
+
return styles.info(`${pico.bold("hint:")} ${message}`);
|
|
16
|
+
}
|
|
17
|
+
function note(messages) {
|
|
18
|
+
const message = Array.isArray(messages) ? messages.join("\n") : messages;
|
|
19
|
+
return styles.info(`${pico.bold("note:")} ${message}`);
|
|
20
|
+
}
|
|
21
|
+
function code(str) {
|
|
22
|
+
return pico.italic(`${pico.dim("`")}${str}${pico.dim("`")}`);
|
|
23
|
+
}
|
|
24
|
+
function frame(text, contents) {
|
|
25
|
+
const result = [
|
|
26
|
+
text
|
|
27
|
+
];
|
|
28
|
+
contents.forEach((content, index)=>{
|
|
29
|
+
const lines = content.split("\n");
|
|
30
|
+
const isLastContent = index === contents.length - 1;
|
|
31
|
+
const firstLinePrefix = isLastContent ? "╰▶ " : "├▶ ";
|
|
32
|
+
const continuationPrefix = isLastContent ? " " : "│ ";
|
|
33
|
+
const framedLines = lines.map((line, lineIndex)=>{
|
|
34
|
+
const prefix = lineIndex === 0 ? firstLinePrefix : continuationPrefix;
|
|
35
|
+
return `${prefix}${line}`;
|
|
36
|
+
});
|
|
37
|
+
result.push(...framedLines);
|
|
38
|
+
});
|
|
39
|
+
return result.join("\n");
|
|
40
|
+
}
|
|
41
|
+
const identity = (s)=>s;
|
|
42
|
+
function getMarkerMidpoint(marker) {
|
|
43
|
+
const textLen = marker.endCol - marker.startCol;
|
|
44
|
+
return marker.startCol + Math.floor(textLen / 2);
|
|
45
|
+
}
|
|
46
|
+
function buildUnderline(markers) {
|
|
47
|
+
const parts = [];
|
|
48
|
+
let pos = 0;
|
|
49
|
+
for (const marker of markers){
|
|
50
|
+
const textLen = marker.endCol - marker.startCol;
|
|
51
|
+
const midPoint = Math.floor(textLen / 2);
|
|
52
|
+
if (marker.startCol > pos) {
|
|
53
|
+
parts.push(" ".repeat(marker.startCol - pos));
|
|
54
|
+
pos = marker.startCol;
|
|
55
|
+
}
|
|
56
|
+
const segment = `${"─".repeat(midPoint)}┬${"─".repeat(textLen - midPoint - 1)}`;
|
|
57
|
+
const colorFn = marker.color ?? identity;
|
|
58
|
+
parts.push(colorFn(segment));
|
|
59
|
+
pos += textLen;
|
|
60
|
+
}
|
|
61
|
+
return parts.join("");
|
|
62
|
+
}
|
|
63
|
+
function buildExplanationLine(marker, midCol, remainingMids, isOnlyMarker) {
|
|
64
|
+
let line = "â•°";
|
|
65
|
+
let pos = midCol + 1;
|
|
66
|
+
for (const nextMid of remainingMids){
|
|
67
|
+
while(pos < nextMid){
|
|
68
|
+
line += "─";
|
|
69
|
+
pos++;
|
|
70
|
+
}
|
|
71
|
+
line += "┼";
|
|
72
|
+
pos++;
|
|
73
|
+
}
|
|
74
|
+
const arrow = isOnlyMarker ? "▶ " : "─▶ ";
|
|
75
|
+
line += arrow + marker.explain;
|
|
76
|
+
const colorFn = marker.color ?? identity;
|
|
77
|
+
return " ".repeat(midCol) + colorFn(line);
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* @example
|
|
81
|
+
* inline`function ${{text: "hello", explain: "name not allowed bro"}}() {\n return 666\n}`;
|
|
82
|
+
* =>
|
|
83
|
+
* function hello() {
|
|
84
|
+
* ──┬──
|
|
85
|
+
* â•°â–¶ name not allowed bro
|
|
86
|
+
* return 666
|
|
87
|
+
* }
|
|
88
|
+
*/ function inline(text, ...values) {
|
|
89
|
+
const resultLines = [];
|
|
90
|
+
let currentLine = "";
|
|
91
|
+
let currentLineVisualLen = 0;
|
|
92
|
+
let pendingMarkers = [];
|
|
93
|
+
const flushLine = ()=>{
|
|
94
|
+
resultLines.push(currentLine);
|
|
95
|
+
if (pendingMarkers.length === 0) {
|
|
96
|
+
currentLine = "";
|
|
97
|
+
currentLineVisualLen = 0;
|
|
98
|
+
return;
|
|
99
|
+
}
|
|
100
|
+
const markerMids = pendingMarkers.map(getMarkerMidpoint);
|
|
101
|
+
resultLines.push(buildUnderline(pendingMarkers));
|
|
102
|
+
for(let i = 0; i < pendingMarkers.length; i++){
|
|
103
|
+
const line = buildExplanationLine(pendingMarkers[i], markerMids[i], markerMids.slice(i + 1), pendingMarkers.length === 1);
|
|
104
|
+
resultLines.push(line);
|
|
105
|
+
}
|
|
106
|
+
pendingMarkers = [];
|
|
107
|
+
currentLine = "";
|
|
108
|
+
currentLineVisualLen = 0;
|
|
109
|
+
};
|
|
110
|
+
for(let i = 0; i < text.length; i++){
|
|
111
|
+
const segment = text[i];
|
|
112
|
+
const lines = segment.split("\n");
|
|
113
|
+
for(let j = 0; j < lines.length; j++){
|
|
114
|
+
if (j > 0) {
|
|
115
|
+
flushLine();
|
|
116
|
+
}
|
|
117
|
+
currentLine += lines[j];
|
|
118
|
+
currentLineVisualLen += lines[j].length;
|
|
119
|
+
}
|
|
120
|
+
if (i < values.length) {
|
|
121
|
+
const val = values[i];
|
|
122
|
+
const value = !Array.isArray(val) ? val : {
|
|
123
|
+
text: val[0],
|
|
124
|
+
explain: val[1],
|
|
125
|
+
...val[2]
|
|
126
|
+
};
|
|
127
|
+
const startCol = currentLineVisualLen;
|
|
128
|
+
const colorFn = value.color ?? ((s)=>s);
|
|
129
|
+
currentLine += colorFn(value.text);
|
|
130
|
+
currentLineVisualLen += value.text.length;
|
|
131
|
+
const endCol = currentLineVisualLen;
|
|
132
|
+
pendingMarkers.push({
|
|
133
|
+
startCol,
|
|
134
|
+
endCol,
|
|
135
|
+
explain: value.explain,
|
|
136
|
+
color: value.color
|
|
137
|
+
});
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
if (currentLine || pendingMarkers.length > 0) {
|
|
141
|
+
flushLine();
|
|
142
|
+
}
|
|
143
|
+
return resultLines.join("\n");
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
export { code, frame, help, hint, inline, note };
|
package/package.json
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "balagan",
|
|
3
|
+
"version": "0.0.0",
|
|
4
|
+
"license": "MIT",
|
|
5
|
+
"author": "Gal Schlezinger",
|
|
6
|
+
"files": [
|
|
7
|
+
"dist",
|
|
8
|
+
"src"
|
|
9
|
+
],
|
|
10
|
+
"type": "module",
|
|
11
|
+
"main": "./dist/index.cjs",
|
|
12
|
+
"module": "./dist/index.js",
|
|
13
|
+
"types": "./dist/index.d.ts",
|
|
14
|
+
"exports": {
|
|
15
|
+
".": {
|
|
16
|
+
"import": {
|
|
17
|
+
"types": "./dist/index.d.ts",
|
|
18
|
+
"default": "./dist/index.js"
|
|
19
|
+
},
|
|
20
|
+
"require": {
|
|
21
|
+
"types": "./dist/index.d.cts",
|
|
22
|
+
"default": "./dist/index.cjs"
|
|
23
|
+
}
|
|
24
|
+
},
|
|
25
|
+
"./package.json": "./package.json"
|
|
26
|
+
},
|
|
27
|
+
"scripts": {
|
|
28
|
+
"dev": "tsdx dev",
|
|
29
|
+
"build": "tsdx build",
|
|
30
|
+
"test": "tsdx test",
|
|
31
|
+
"test:watch": "tsdx test --watch",
|
|
32
|
+
"lint": "tsdx lint",
|
|
33
|
+
"format": "tsdx format",
|
|
34
|
+
"format:check": "tsdx format --check",
|
|
35
|
+
"typecheck": "tsdx typecheck",
|
|
36
|
+
"prepublishOnly": "bun run build"
|
|
37
|
+
},
|
|
38
|
+
"dependencies": {
|
|
39
|
+
"bunchee": "^6.9.3",
|
|
40
|
+
"oxfmt": "^0.23.0",
|
|
41
|
+
"oxlint": "^1.38.0",
|
|
42
|
+
"picocolors": "^1.1.1",
|
|
43
|
+
"prettier": "^3.7.4",
|
|
44
|
+
"vitest": "^4.0.16"
|
|
45
|
+
},
|
|
46
|
+
"devDependencies": {
|
|
47
|
+
"@types/node": "^22.10.2",
|
|
48
|
+
"tsdx": "^2.0.0",
|
|
49
|
+
"typescript": "^5.7.2"
|
|
50
|
+
},
|
|
51
|
+
"engines": {
|
|
52
|
+
"node": ">=20"
|
|
53
|
+
}
|
|
54
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,196 @@
|
|
|
1
|
+
import pico from "picocolors";
|
|
2
|
+
|
|
3
|
+
const styles = {
|
|
4
|
+
info: pico.blue,
|
|
5
|
+
help: pico.cyan,
|
|
6
|
+
warn: pico.yellow,
|
|
7
|
+
error: pico.red,
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
export function help(messages: string | string[]) {
|
|
11
|
+
const message = Array.isArray(messages) ? messages.join("\n") : messages;
|
|
12
|
+
return styles.help(`${pico.bold("help:")} ${message}`);
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export function hint(messages: string | string[]) {
|
|
16
|
+
const message = Array.isArray(messages) ? messages.join("\n") : messages;
|
|
17
|
+
return styles.info(`${pico.bold("hint:")} ${message}`);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export function note(messages: string | string[]) {
|
|
21
|
+
const message = Array.isArray(messages) ? messages.join("\n") : messages;
|
|
22
|
+
return styles.info(`${pico.bold("note:")} ${message}`);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export function code(str: string) {
|
|
26
|
+
return pico.italic(`${pico.dim("`")}${str}${pico.dim("`")}`);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export function frame(text: string, contents: string[]): string {
|
|
30
|
+
const result = [text];
|
|
31
|
+
|
|
32
|
+
contents.forEach((content, index) => {
|
|
33
|
+
const lines = content.split("\n");
|
|
34
|
+
const isLastContent = index === contents.length - 1;
|
|
35
|
+
|
|
36
|
+
const firstLinePrefix = isLastContent ? "╰▶ " : "├▶ ";
|
|
37
|
+
const continuationPrefix = isLastContent ? " " : "│ ";
|
|
38
|
+
|
|
39
|
+
const framedLines = lines.map((line, lineIndex) => {
|
|
40
|
+
const prefix = lineIndex === 0 ? firstLinePrefix : continuationPrefix;
|
|
41
|
+
return `${prefix}${line}`;
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
result.push(...framedLines);
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
return result.join("\n");
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
interface Explain {
|
|
51
|
+
text: string;
|
|
52
|
+
explain: string;
|
|
53
|
+
/** adds ansi coloring */
|
|
54
|
+
color?: (s: string) => string;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
type Explainish = Explain | [text: string, explain: string, opts?: Pick<Explain, "color">];
|
|
58
|
+
|
|
59
|
+
type Marker = {
|
|
60
|
+
startCol: number;
|
|
61
|
+
endCol: number;
|
|
62
|
+
explain: string;
|
|
63
|
+
color?: (s: string) => string;
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
const identity = (s: string) => s;
|
|
67
|
+
|
|
68
|
+
function getMarkerMidpoint(marker: Marker): number {
|
|
69
|
+
const textLen = marker.endCol - marker.startCol;
|
|
70
|
+
return marker.startCol + Math.floor(textLen / 2);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
function buildUnderline(markers: Marker[]): string {
|
|
74
|
+
const parts: string[] = [];
|
|
75
|
+
let pos = 0;
|
|
76
|
+
for (const marker of markers) {
|
|
77
|
+
const textLen = marker.endCol - marker.startCol;
|
|
78
|
+
const midPoint = Math.floor(textLen / 2);
|
|
79
|
+
|
|
80
|
+
if (marker.startCol > pos) {
|
|
81
|
+
parts.push(" ".repeat(marker.startCol - pos));
|
|
82
|
+
pos = marker.startCol;
|
|
83
|
+
}
|
|
84
|
+
const segment = `${"─".repeat(midPoint)}┬${"─".repeat(textLen - midPoint - 1)}`;
|
|
85
|
+
const colorFn = marker.color ?? identity;
|
|
86
|
+
parts.push(colorFn(segment));
|
|
87
|
+
pos += textLen;
|
|
88
|
+
}
|
|
89
|
+
return parts.join("");
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
function buildExplanationLine(
|
|
93
|
+
marker: Marker,
|
|
94
|
+
midCol: number,
|
|
95
|
+
remainingMids: number[],
|
|
96
|
+
isOnlyMarker: boolean,
|
|
97
|
+
): string {
|
|
98
|
+
let line = "â•°";
|
|
99
|
+
let pos = midCol + 1;
|
|
100
|
+
|
|
101
|
+
for (const nextMid of remainingMids) {
|
|
102
|
+
while (pos < nextMid) {
|
|
103
|
+
line += "─";
|
|
104
|
+
pos++;
|
|
105
|
+
}
|
|
106
|
+
line += "┼";
|
|
107
|
+
pos++;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
const arrow = isOnlyMarker ? "▶ " : "─▶ ";
|
|
111
|
+
line += arrow + marker.explain;
|
|
112
|
+
|
|
113
|
+
const colorFn = marker.color ?? identity;
|
|
114
|
+
return " ".repeat(midCol) + colorFn(line);
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* @example
|
|
119
|
+
* inline`function ${{text: "hello", explain: "name not allowed bro"}}() {\n return 666\n}`;
|
|
120
|
+
* =>
|
|
121
|
+
* function hello() {
|
|
122
|
+
* ──┬──
|
|
123
|
+
* â•°â–¶ name not allowed bro
|
|
124
|
+
* return 666
|
|
125
|
+
* }
|
|
126
|
+
*/
|
|
127
|
+
export function inline(text: TemplateStringsArray, ...values: Explainish[]): string {
|
|
128
|
+
const resultLines: string[] = [];
|
|
129
|
+
let currentLine = "";
|
|
130
|
+
let currentLineVisualLen = 0;
|
|
131
|
+
let pendingMarkers: Marker[] = [];
|
|
132
|
+
|
|
133
|
+
const flushLine = () => {
|
|
134
|
+
resultLines.push(currentLine);
|
|
135
|
+
if (pendingMarkers.length === 0) {
|
|
136
|
+
currentLine = "";
|
|
137
|
+
currentLineVisualLen = 0;
|
|
138
|
+
return;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
const markerMids = pendingMarkers.map(getMarkerMidpoint);
|
|
142
|
+
|
|
143
|
+
resultLines.push(buildUnderline(pendingMarkers));
|
|
144
|
+
|
|
145
|
+
for (let i = 0; i < pendingMarkers.length; i++) {
|
|
146
|
+
const line = buildExplanationLine(
|
|
147
|
+
pendingMarkers[i],
|
|
148
|
+
markerMids[i],
|
|
149
|
+
markerMids.slice(i + 1),
|
|
150
|
+
pendingMarkers.length === 1,
|
|
151
|
+
);
|
|
152
|
+
resultLines.push(line);
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
pendingMarkers = [];
|
|
156
|
+
currentLine = "";
|
|
157
|
+
currentLineVisualLen = 0;
|
|
158
|
+
};
|
|
159
|
+
|
|
160
|
+
for (let i = 0; i < text.length; i++) {
|
|
161
|
+
const segment = text[i];
|
|
162
|
+
const lines = segment.split("\n");
|
|
163
|
+
|
|
164
|
+
for (let j = 0; j < lines.length; j++) {
|
|
165
|
+
if (j > 0) {
|
|
166
|
+
flushLine();
|
|
167
|
+
}
|
|
168
|
+
currentLine += lines[j];
|
|
169
|
+
currentLineVisualLen += lines[j].length;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
if (i < values.length) {
|
|
173
|
+
const val = values[i];
|
|
174
|
+
const value: Explain = !Array.isArray(val)
|
|
175
|
+
? val
|
|
176
|
+
: { text: val[0], explain: val[1], ...val[2] };
|
|
177
|
+
const startCol = currentLineVisualLen;
|
|
178
|
+
const colorFn = value.color ?? ((s: string) => s);
|
|
179
|
+
currentLine += colorFn(value.text);
|
|
180
|
+
currentLineVisualLen += value.text.length;
|
|
181
|
+
const endCol = currentLineVisualLen;
|
|
182
|
+
pendingMarkers.push({
|
|
183
|
+
startCol,
|
|
184
|
+
endCol,
|
|
185
|
+
explain: value.explain,
|
|
186
|
+
color: value.color,
|
|
187
|
+
});
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
if (currentLine || pendingMarkers.length > 0) {
|
|
192
|
+
flushLine();
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
return resultLines.join("\n");
|
|
196
|
+
}
|