Skip to content

Commit 901f665

Browse files
committed
feat: initial commit
0 parents  commit 901f665

File tree

9 files changed

+861
-0
lines changed

9 files changed

+861
-0
lines changed

.vscode/settings.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"deno.enable": true,
3+
"deno.unstable": true,
4+
"deno.config": "./deno.jsonc"
5+
}

LICENSE.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# The MIT License (MIT)
2+
3+
## Copyright © 2023 Im-Beast
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
6+
documentation files (the “Software”), to deal in the Software without restriction, including without limitation the
7+
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit
8+
persons to whom the Software is furnished to do so, subject to the following conditions:
9+
10+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
11+
Software.
12+
13+
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
14+
WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
15+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
16+
OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# 🪁 Nice
2+
3+
Module for creating style definitions for terminal, similiar to [lipgloss](https://github.com/charmbracelet/lipgloss).
4+
5+
# 🚧 WIP
6+
7+
This project is currently under WIP and isn't at all ready for usage in production API and other changes are guaranteed
8+
to happen.
9+
10+
## 🤝 Contributing
11+
12+
**Nice** is open for any contributions.
13+
<br /> If you feel like you can enhance this project - please open an issue and/or pull request.
14+
<br /> Code should be well document and easy to follow what's going on.
15+
16+
This project follows [conventional commits](https://www.conventionalcommits.org/en/v1.0.0/) spec.
17+
<br /> If your pull request's code can be hard to understand, please add comments to it.
18+
19+
## 📝 Licensing
20+
21+
This project is available under **MIT** License conditions.

api.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
const Text = Nice({
2+
bg: "red",
3+
fg: "blue",
4+
padding: [2, 1, 2, 1],
5+
bold: true,
6+
});
7+
8+
Tui(
9+
GridLayout([
10+
[Text("1"), Text("2"), Text("3")],
11+
[Text("4"), Text("5"), Text("6")],
12+
[Text("7"), Text("8"), Text("9")],
13+
]),
14+
);

deno.jsonc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"fmt": {
3+
"lineWidth": 120
4+
},
5+
"lock": false
6+
}

examples/demo.ts

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
import { crayon } from "https://deno.land/x/crayon@3.3.3/mod.ts";
2+
import { Nice } from "../src/nice.ts";
3+
4+
const a = new Nice({
5+
style: crayon.bgLightBlue.lightWhite.bold,
6+
text: {
7+
horizontalAlign: "justify",
8+
verticalAlign: "middle",
9+
overflow: "ellipsis",
10+
},
11+
border: {
12+
type: "thick",
13+
style: crayon.white.bold,
14+
},
15+
padding: {
16+
bottom: 1,
17+
top: 1,
18+
right: 2,
19+
left: 2,
20+
},
21+
margin: {
22+
top: 0,
23+
bottom: 0,
24+
left: 0,
25+
right: 0,
26+
},
27+
});
28+
29+
const b = a.clone();
30+
b.style = crayon.bgRed;
31+
b.width = 25;
32+
b.height = 13;
33+
b.text.horizontalAlign = "center";
34+
35+
const c = a.clone();
36+
37+
c.style = crayon.bgGreen;
38+
39+
const d = c.clone();
40+
d.width = 5;
41+
d.height = 2;
42+
d.style = crayon.bgMagenta;
43+
44+
const e = d.clone();
45+
e.width = 10;
46+
e.height = 5;
47+
e.style = crayon.bgYellow;
48+
e.text.overflow = "ellipsis";
49+
50+
const f = e.clone();
51+
f.style = crayon.bgLightGreen;
52+
f.width = 20;
53+
f.height = 3;
54+
f.text.horizontalAlign = "right";
55+
56+
const popup = a.clone();
57+
popup.style = crayon.bgLightYellow.red.bold;
58+
popup.margin = {
59+
top: 1,
60+
bottom: 1,
61+
left: 2,
62+
right: 2,
63+
};
64+
popup.width = 17;
65+
popup.height = 1;
66+
popup.text.horizontalAlign = "center";
67+
68+
const popup2 = popup.clone();
69+
popup2.style = crayon.bgBlue.lightWhite;
70+
popup2.height = 1;
71+
popup2.width = 4;
72+
popup2.margin = {
73+
top: 0,
74+
bottom: 0,
75+
left: 0,
76+
right: 0,
77+
};
78+
popup2.padding = {
79+
top: 0,
80+
bottom: 0,
81+
left: 0,
82+
right: 0,
83+
};
84+
85+
console.clear();
86+
87+
const objects: Nice[] = [];
88+
for (let i = 0; i < 100; ++i) {
89+
const n = a.clone();
90+
n.style = crayon.bgAnsi8(~~(i * 2.55));
91+
objects.push(n);
92+
}
93+
94+
export function render() {
95+
const SCREEN_BG = Nice.layoutHorizontally(
96+
Nice.layoutVertically(
97+
a.render(
98+
"This gets justified\nAlone\none two three four five six\nlonger words come here\nbig spacing now",
99+
),
100+
Nice.layoutHorizontally(
101+
c.render("Hello"),
102+
c.render("there"),
103+
d.render("This should get clipped"),
104+
),
105+
),
106+
b.render(
107+
"(╯°□°)╯︵┻━┻\ndevanagari आआॠऋॲपॉ\nハハハThis text should get wrapped because widthəəə is explicit verylongstringthaəə💩twillwrapnomatterwhat\nwowə",
108+
),
109+
Nice.layoutVertically(
110+
e.render(
111+
"very long text that will wrap and will fit",
112+
),
113+
e.render(
114+
"very long text that will wrap and totally won't fit",
115+
),
116+
),
117+
f.render(`ISBN: 978-0-1234-5678-7\n\nCSS: הרפתקה חדשה!`),
118+
);
119+
120+
const SCREEN_FG = popup.render("Hello");
121+
const SCREEN_FG2 = popup2.render("Hi");
122+
123+
return Nice.overlay(
124+
SCREEN_FG2,
125+
Nice.overlay(
126+
SCREEN_FG,
127+
SCREEN_BG,
128+
),
129+
);
130+
}
131+
132+
if (import.meta.main) {
133+
// warmup
134+
render();
135+
render();
136+
render();
137+
138+
console.clear();
139+
const start = performance.now();
140+
console.log(render());
141+
console.log("It took", performance.now() - start, "ms to render this frame");
142+
}

src/border.ts

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
import { Style } from "./nice.ts";
2+
3+
export interface Border {
4+
top: string;
5+
bottom: string;
6+
left: string;
7+
right: string;
8+
topLeft: string;
9+
topRight: string;
10+
bottomLeft: string;
11+
bottomRight: string;
12+
}
13+
14+
export type BorderType = "sharp" | "rounded" | "thick" | "double" | "block";
15+
export const Borders: Record<BorderType, Border> = {
16+
sharp: {
17+
top: "─",
18+
bottom: "─",
19+
left: "│",
20+
right: "│",
21+
topLeft: "┌",
22+
topRight: "┐",
23+
bottomLeft: "└",
24+
bottomRight: "┘",
25+
},
26+
rounded: {
27+
top: "─",
28+
bottom: "─",
29+
left: "│",
30+
right: "│",
31+
topLeft: "╭",
32+
topRight: "╮",
33+
bottomLeft: "╰",
34+
bottomRight: "╯",
35+
},
36+
thick: {
37+
top: "━",
38+
bottom: "━",
39+
left: "┃",
40+
right: "┃",
41+
topLeft: "┏",
42+
topRight: "┓",
43+
bottomLeft: "┗",
44+
bottomRight: "┛",
45+
},
46+
double: {
47+
top: "═",
48+
bottom: "═",
49+
left: "║",
50+
right: "║",
51+
topLeft: "╔",
52+
topRight: "╗",
53+
bottomLeft: "╚",
54+
bottomRight: "╝",
55+
},
56+
block: {
57+
top: "█",
58+
bottom: "█",
59+
left: "█",
60+
right: "█",
61+
topLeft: "█",
62+
topRight: "█",
63+
bottomLeft: "█",
64+
bottomRight: "█",
65+
},
66+
};
67+
68+
export function stylePieces(pieces: Border, style: Style): Border {
69+
return {
70+
top: style(pieces.top),
71+
bottom: style(pieces.bottom),
72+
left: style(pieces.left),
73+
right: style(pieces.right),
74+
topLeft: style(pieces.topLeft),
75+
topRight: style(pieces.topRight),
76+
bottomLeft: style(pieces.bottomLeft),
77+
bottomRight: style(pieces.bottomRight),
78+
};
79+
}

0 commit comments

Comments
 (0)