@nhatnguyet/cli 0.0.0-stage → 0.3.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 +94 -2
- package/dist/cli.d.ts +20 -0
- package/dist/cli.js +222 -0
- package/dist/render.d.ts +52 -0
- package/dist/render.js +498 -0
- package/package.json +65 -4
- package/src/cli.ts +243 -0
- package/src/node.d.ts +4 -0
- package/src/render.ts +526 -0
package/dist/render.js
ADDED
|
@@ -0,0 +1,498 @@
|
|
|
1
|
+
// Dựng kết quả của lớp API tiếng Anh thành chữ cho terminal: lá số Tử Vi dạng
|
|
2
|
+
// lưới 4x4 bằng ký tự khung, bảng Bát Tự, ngày âm, thần số học, lễ hội. Màu
|
|
3
|
+
// ANSI chỉ khi người gọi bật (cli.ts tắt khi không phải TTY hay có NO_COLOR).
|
|
4
|
+
//
|
|
5
|
+
// Renders results of the English API as terminal text.
|
|
6
|
+
import { term } from "@nhatnguyet/am-lich";
|
|
7
|
+
// ── Đo bề rộng hiển thị ─────────────────────────────────────────────────
|
|
8
|
+
/** Terminal columns taken by one code point: 2 for CJK and fullwidth, 0 for combining marks. */
|
|
9
|
+
export function charWidth(cp) {
|
|
10
|
+
if (cp === 0)
|
|
11
|
+
return 0;
|
|
12
|
+
if ((cp >= 0x300 && cp <= 0x36f) || (cp >= 0x200b && cp <= 0x200f) || cp === 0xfe0f)
|
|
13
|
+
return 0;
|
|
14
|
+
if ((cp >= 0x1100 && cp <= 0x115f) ||
|
|
15
|
+
(cp >= 0x2e80 && cp <= 0x303e) ||
|
|
16
|
+
(cp >= 0x3041 && cp <= 0x33ff) ||
|
|
17
|
+
(cp >= 0x3400 && cp <= 0x4dbf) ||
|
|
18
|
+
(cp >= 0x4e00 && cp <= 0x9fff) ||
|
|
19
|
+
(cp >= 0xa000 && cp <= 0xa4cf) ||
|
|
20
|
+
(cp >= 0xac00 && cp <= 0xd7a3) ||
|
|
21
|
+
(cp >= 0xf900 && cp <= 0xfaff) ||
|
|
22
|
+
(cp >= 0xfe30 && cp <= 0xfe4f) ||
|
|
23
|
+
(cp >= 0xff00 && cp <= 0xff60) ||
|
|
24
|
+
(cp >= 0xffe0 && cp <= 0xffe6) ||
|
|
25
|
+
(cp >= 0x20000 && cp <= 0x3fffd)) {
|
|
26
|
+
return 2;
|
|
27
|
+
}
|
|
28
|
+
return 1;
|
|
29
|
+
}
|
|
30
|
+
/** Display width of a string (no ANSI codes inside). */
|
|
31
|
+
export function displayWidth(s) {
|
|
32
|
+
let w = 0;
|
|
33
|
+
for (const ch of s.normalize("NFC"))
|
|
34
|
+
w += charWidth(ch.codePointAt(0));
|
|
35
|
+
return w;
|
|
36
|
+
}
|
|
37
|
+
/** Cut a string to at most `width` columns, ending with "…" when cut. */
|
|
38
|
+
export function truncate(s, width) {
|
|
39
|
+
s = s.normalize("NFC");
|
|
40
|
+
if (displayWidth(s) <= width)
|
|
41
|
+
return s;
|
|
42
|
+
let out = "";
|
|
43
|
+
let w = 0;
|
|
44
|
+
for (const ch of s) {
|
|
45
|
+
const cw = charWidth(ch.codePointAt(0));
|
|
46
|
+
if (w + cw > width - 1)
|
|
47
|
+
break;
|
|
48
|
+
out += ch;
|
|
49
|
+
w += cw;
|
|
50
|
+
}
|
|
51
|
+
return out + "…";
|
|
52
|
+
}
|
|
53
|
+
/** Wrap words into lines of at most `width` columns (CJK text breaks anywhere). */
|
|
54
|
+
export function wrap(s, width) {
|
|
55
|
+
const lines = [];
|
|
56
|
+
let line = "";
|
|
57
|
+
const tokens = s.normalize("NFC").split(/(\s+)/).filter((t) => t !== "");
|
|
58
|
+
for (const tok of tokens) {
|
|
59
|
+
if (/^\s+$/.test(tok)) {
|
|
60
|
+
if (line !== "")
|
|
61
|
+
line += " ";
|
|
62
|
+
continue;
|
|
63
|
+
}
|
|
64
|
+
if (displayWidth(line + tok) <= width) {
|
|
65
|
+
line += tok;
|
|
66
|
+
continue;
|
|
67
|
+
}
|
|
68
|
+
if (line.trim() !== "")
|
|
69
|
+
lines.push(line.trimEnd());
|
|
70
|
+
line = "";
|
|
71
|
+
// Một từ dài hơn cả dòng (chữ Hán viết liền): cắt theo ký tự.
|
|
72
|
+
let t = tok;
|
|
73
|
+
while (displayWidth(t) > width) {
|
|
74
|
+
let head = "";
|
|
75
|
+
for (const ch of t) {
|
|
76
|
+
if (displayWidth(head + ch) > width)
|
|
77
|
+
break;
|
|
78
|
+
head += ch;
|
|
79
|
+
}
|
|
80
|
+
lines.push(head);
|
|
81
|
+
t = t.slice(head.length);
|
|
82
|
+
}
|
|
83
|
+
line = t;
|
|
84
|
+
}
|
|
85
|
+
if (line.trim() !== "")
|
|
86
|
+
lines.push(line.trimEnd());
|
|
87
|
+
return lines;
|
|
88
|
+
}
|
|
89
|
+
/** Pack items into lines of at most `width` columns, never splitting an item (long items are cut). */
|
|
90
|
+
export function wrapItems(items, sep, width) {
|
|
91
|
+
const lines = [];
|
|
92
|
+
let line = "";
|
|
93
|
+
const duoi = displayWidth(sep.trimEnd());
|
|
94
|
+
for (const raw of items) {
|
|
95
|
+
const it = truncate(raw, width - duoi);
|
|
96
|
+
if (line === "")
|
|
97
|
+
line = it;
|
|
98
|
+
else if (displayWidth(line + sep + it) + duoi <= width)
|
|
99
|
+
line += sep + it;
|
|
100
|
+
else {
|
|
101
|
+
lines.push(line + sep.trimEnd());
|
|
102
|
+
line = it;
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
if (line !== "")
|
|
106
|
+
lines.push(line);
|
|
107
|
+
return lines;
|
|
108
|
+
}
|
|
109
|
+
const ANSI = {
|
|
110
|
+
bold: "\x1b[1m",
|
|
111
|
+
dim: "\x1b[2m",
|
|
112
|
+
red: "\x1b[31m",
|
|
113
|
+
green: "\x1b[32m",
|
|
114
|
+
yellow: "\x1b[33m",
|
|
115
|
+
blue: "\x1b[34m",
|
|
116
|
+
magenta: "\x1b[35m",
|
|
117
|
+
cyan: "\x1b[36m",
|
|
118
|
+
title: "\x1b[1;33m",
|
|
119
|
+
};
|
|
120
|
+
function paint(text, style, color) {
|
|
121
|
+
if (!color || style === "" || text === "")
|
|
122
|
+
return text;
|
|
123
|
+
return ANSI[style] + text + "\x1b[0m";
|
|
124
|
+
}
|
|
125
|
+
/** Fit a styled line into exactly `width` columns (cut or pad). */
|
|
126
|
+
function fitLine(line, width, color, align = "left") {
|
|
127
|
+
const plain = line.map((s) => s[0]).join("");
|
|
128
|
+
let out = "";
|
|
129
|
+
let used = 0;
|
|
130
|
+
if (displayWidth(plain) > width) {
|
|
131
|
+
// Cắt: giữ từng mẩu tới khi hết chỗ, mẩu cuối cắt kèm dấu "…".
|
|
132
|
+
for (const [t, st] of line) {
|
|
133
|
+
const con = width - used;
|
|
134
|
+
if (con <= 0)
|
|
135
|
+
break;
|
|
136
|
+
const w = displayWidth(t);
|
|
137
|
+
if (w < con) {
|
|
138
|
+
out += paint(t, st, color);
|
|
139
|
+
used += w;
|
|
140
|
+
continue;
|
|
141
|
+
}
|
|
142
|
+
const c = truncate(w === con ? t + " " : t, con);
|
|
143
|
+
out += paint(c, st, color);
|
|
144
|
+
used += displayWidth(c);
|
|
145
|
+
break;
|
|
146
|
+
}
|
|
147
|
+
return out + " ".repeat(Math.max(0, width - used));
|
|
148
|
+
}
|
|
149
|
+
const pad = width - displayWidth(plain);
|
|
150
|
+
const left = align === "center" ? Math.floor(pad / 2) : 0;
|
|
151
|
+
out = " ".repeat(left) + line.map(([t, st]) => paint(t, st, color)).join("") + " ".repeat(pad - left);
|
|
152
|
+
return out;
|
|
153
|
+
}
|
|
154
|
+
// ── Chữ giao diện ba thứ tiếng ──────────────────────────────────────────
|
|
155
|
+
const UI = {
|
|
156
|
+
vi: {
|
|
157
|
+
zwTitle: "LÁ SỐ TỬ VI", body: "Thân", life: "Mệnh", tuan: "Tuần", triet: "Triệt", born: "Sinh", lunar: "Âm lịch",
|
|
158
|
+
leap: "nhuận", year: "Năm", bureau: "Cục", naYin: "Bản mệnh", view: "Năm xem", age: "tuổi", decades: "Đại vận",
|
|
159
|
+
patterns: "Cách cục", notes: "Ghi chú", legend: "Sáng tối: M miếu, V vượng, Đ đắc, B bình, H hãm. Tuổi tính theo tuổi mụ.",
|
|
160
|
+
yearPalace: "Lưu niên", minorLimit: "Tiểu vận", decade: "Đại vận", none: "không",
|
|
161
|
+
bzTitle: "LÁ SỐ BÁT TỰ", pYear: "Năm", pMonth: "Tháng", pDay: "Ngày", pHour: "Giờ", tenGod: "Thập thần", stem: "Can",
|
|
162
|
+
branch: "Chi", hidden: "Tàng can", stage: "Trường sinh", stars: "Thần sát", dayMaster: "Nhật chủ", luck: "Đại vận",
|
|
163
|
+
forward: "thuận", backward: "nghịch", startAt: "khởi vận lúc", years: "năm", months: "tháng", term: "Tiết khí",
|
|
164
|
+
gregorian: "Dương lịch", month: "Tháng", day: "Ngày", zodiac: "Con giáp", solarTerm: "Tiết khí", country: "Lịch",
|
|
165
|
+
nmTitle: "THẦN SỐ HỌC", pinnacles: "Đỉnh cao", challenges: "Thách thức", until: "tới", fest: "LỄ HỘI",
|
|
166
|
+
terms: "TIẾT KHÍ", unknown: "chưa biết giờ", male: "Nam", female: "Nữ",
|
|
167
|
+
hrsTitle: "GIỜ HOÀNG ĐẠO", good: "hoàng đạo", bad: "hắc đạo",
|
|
168
|
+
},
|
|
169
|
+
en: {
|
|
170
|
+
zwTitle: "ZI WEI DOU SHU CHART", body: "Body", life: "Life", tuan: "Tuần", triet: "Triệt", born: "Born", lunar: "Lunar",
|
|
171
|
+
leap: "leap", year: "Year", bureau: "Bureau", naYin: "Na yin", view: "Year", age: "age", decades: "Ten-year periods",
|
|
172
|
+
patterns: "Patterns", notes: "Notes", legend: "Brightness: Ex exalted, Pr prosperous, Fa favorable, Ne neutral, We weak. Ages are East Asian ages (1 at birth).",
|
|
173
|
+
yearPalace: "Year palace", minorLimit: "Minor limit", decade: "Decade", none: "none",
|
|
174
|
+
bzTitle: "BAZI (FOUR PILLARS)", pYear: "Year", pMonth: "Month", pDay: "Day", pHour: "Hour", tenGod: "Ten God", stem: "Stem",
|
|
175
|
+
branch: "Branch", hidden: "Hidden stems", stage: "Stage", stars: "Stars", dayMaster: "Day Master", luck: "Luck pillars",
|
|
176
|
+
forward: "forward", backward: "backward", startAt: "starting at", years: "y", months: "m", term: "Solar term",
|
|
177
|
+
gregorian: "Gregorian", month: "Month", day: "Day", zodiac: "Zodiac", solarTerm: "Solar term", country: "Calendar",
|
|
178
|
+
nmTitle: "NUMEROLOGY", pinnacles: "Pinnacles", challenges: "Challenges", until: "to age", fest: "FESTIVALS",
|
|
179
|
+
terms: "SOLAR TERMS", unknown: "time unknown", male: "male", female: "female",
|
|
180
|
+
hrsTitle: "AUSPICIOUS HOURS", good: "auspicious", bad: "inauspicious",
|
|
181
|
+
},
|
|
182
|
+
zh: {
|
|
183
|
+
zwTitle: "紫微斗数命盘", body: "身", life: "命", tuan: "旬空", triet: "截空", born: "出生", lunar: "农历",
|
|
184
|
+
leap: "闰", year: "年", bureau: "五行局", naYin: "纳音", view: "流年", age: "虚岁", decades: "大限",
|
|
185
|
+
patterns: "格局", notes: "说明", legend: "亮度:庙、旺、得地、平、陷。年龄按虚岁计。",
|
|
186
|
+
yearPalace: "流年命宫", minorLimit: "小限", decade: "大限", none: "无",
|
|
187
|
+
bzTitle: "八字命盘", pYear: "年柱", pMonth: "月柱", pDay: "日柱", pHour: "时柱", tenGod: "十神", stem: "天干",
|
|
188
|
+
branch: "地支", hidden: "藏干", stage: "十二长生", stars: "神煞", dayMaster: "日主", luck: "大运",
|
|
189
|
+
forward: "顺排", backward: "逆排", startAt: "起运", years: "岁", months: "个月", term: "节气",
|
|
190
|
+
gregorian: "公历", month: "月", day: "日", zodiac: "生肖", solarTerm: "节气", country: "历法",
|
|
191
|
+
nmTitle: "生命灵数", pinnacles: "巅峰数", challenges: "挑战数", until: "至", fest: "节日",
|
|
192
|
+
terms: "二十四节气", unknown: "时辰未知", male: "男", female: "女",
|
|
193
|
+
hrsTitle: "黄道吉时", good: "黄道", bad: "黑道",
|
|
194
|
+
},
|
|
195
|
+
};
|
|
196
|
+
function ui(lang) {
|
|
197
|
+
return UI[lang] ?? UI.en;
|
|
198
|
+
}
|
|
199
|
+
/** Viết tắt độ sáng của chính tinh để vừa ô. */
|
|
200
|
+
function doSangNgan(b, lang) {
|
|
201
|
+
if (lang === "zh")
|
|
202
|
+
return b;
|
|
203
|
+
if (lang === "vi")
|
|
204
|
+
return b.charAt(0);
|
|
205
|
+
return b.slice(0, 2);
|
|
206
|
+
}
|
|
207
|
+
/** Viết tắt Tứ Hóa: bỏ chữ "Hóa", "Hua" hay 化 ("Hua Ji" ra "Ji", 化忌 ra 忌). */
|
|
208
|
+
function hoaNgan(t, lang) {
|
|
209
|
+
return lang === "zh" || t.startsWith("化") ? t.replace("化", "") : t.replace(/^(?:Hóa|Hua)\s+/, "");
|
|
210
|
+
}
|
|
211
|
+
/** Tuần, Triệt theo cách viết của lá số (bản Anh: pinyin, chữ Hán hay Hán Việt). */
|
|
212
|
+
function chuKhong(chart, u) {
|
|
213
|
+
if (chart.lang !== "en" || chart.terms === "vi")
|
|
214
|
+
return [u.tuan, u.triet];
|
|
215
|
+
return chart.terms === "han" ? ["旬空", "截空"] : ["Xun Kong", "Jie Kong"];
|
|
216
|
+
}
|
|
217
|
+
// ── Lưới Tử Vi ──────────────────────────────────────────────────────────
|
|
218
|
+
/** Vị trí 12 cung trên lưới 4x4 (hàng, cột), chi 0 Tý tới 11 Hợi, Tỵ ở góc trên trái. */
|
|
219
|
+
const VI_TRI_LUOI = [
|
|
220
|
+
[3, 2], [3, 1], [3, 0], [2, 0], [1, 0], [0, 0], [0, 1], [0, 2], [0, 3], [1, 3], [2, 3], [3, 3],
|
|
221
|
+
];
|
|
222
|
+
function dongCung(p, lang, W, khong) {
|
|
223
|
+
const u = ui(lang);
|
|
224
|
+
const lines = [];
|
|
225
|
+
const dau = [[p.name, p.isLife ? "title" : "bold"]];
|
|
226
|
+
if (p.isBody)
|
|
227
|
+
dau.push([" · " + u.body, "yellow"]);
|
|
228
|
+
lines.push(dau);
|
|
229
|
+
const dau2 = [[p.stemBranch, "dim"]];
|
|
230
|
+
if (p.tuan)
|
|
231
|
+
dau2.push([" " + khong[0], "blue"]);
|
|
232
|
+
if (p.triet)
|
|
233
|
+
dau2.push([" " + khong[1], "blue"]);
|
|
234
|
+
lines.push(dau2);
|
|
235
|
+
for (const s of p.stars) {
|
|
236
|
+
if (s.kind !== "major")
|
|
237
|
+
continue;
|
|
238
|
+
const dau = [];
|
|
239
|
+
if (s.brightness)
|
|
240
|
+
dau.push([" " + doSangNgan(s.brightness, lang), "dim"]);
|
|
241
|
+
if (s.transformation)
|
|
242
|
+
dau.push([" " + hoaNgan(s.transformation, lang), "green"]);
|
|
243
|
+
const rong = displayWidth(s.name) + dau.reduce((n, x) => n + displayWidth(x[0]), 0);
|
|
244
|
+
if (rong <= W)
|
|
245
|
+
lines.push([[s.name, "red"], ...dau]);
|
|
246
|
+
else {
|
|
247
|
+
// Tên dài: dấu sáng tối và Tứ Hóa xuống dòng dưới, thụt vào.
|
|
248
|
+
lines.push([[s.name, "red"]]);
|
|
249
|
+
if (dau.length)
|
|
250
|
+
lines.push([[" ", ""], ...dau]);
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
const phu = p.stars.filter((s) => s.kind === "minor").map((s) => (s.transformation ? `${s.name} ${hoaNgan(s.transformation, lang)}` : s.name));
|
|
254
|
+
for (const w of wrapItems(phu, lang === "zh" ? "、" : ", ", W))
|
|
255
|
+
lines.push([[w, "cyan"]]);
|
|
256
|
+
const ts = p.stars.find((s) => s.kind === "growth");
|
|
257
|
+
if (ts)
|
|
258
|
+
lines.push([[ts.name, "dim"]]);
|
|
259
|
+
return lines;
|
|
260
|
+
}
|
|
261
|
+
/**
|
|
262
|
+
* The Zi Wei chart as a 4x4 grid: the twelve palaces around the edge (Tỵ at
|
|
263
|
+
* the top left, as in printed charts), birth data in the middle.
|
|
264
|
+
*/
|
|
265
|
+
export function renderZiWeiGrid(chart, opts = {}) {
|
|
266
|
+
const color = opts.color === true;
|
|
267
|
+
const lang = chart.lang;
|
|
268
|
+
const u = ui(lang);
|
|
269
|
+
const width = opts.width ?? 88;
|
|
270
|
+
const W = Math.max(16, Math.min(30, Math.floor((width - 5) / 4)));
|
|
271
|
+
const C = 2 * W + 1;
|
|
272
|
+
const o = [[], [], [], []];
|
|
273
|
+
for (let r = 0; r < 4; r++)
|
|
274
|
+
o[r] = [[], [], [], []];
|
|
275
|
+
for (const p of chart.palaces) {
|
|
276
|
+
const [r, c] = VI_TRI_LUOI[p.index];
|
|
277
|
+
o[r][c] = dongCung(p, lang, W - 2, chuKhong(chart, u));
|
|
278
|
+
}
|
|
279
|
+
const h = [0, 0, 0, 0];
|
|
280
|
+
for (let r = 0; r < 4; r++)
|
|
281
|
+
for (let c = 0; c < 4; c++)
|
|
282
|
+
h[r] = Math.max(h[r], o[r][c].length);
|
|
283
|
+
// Phần giữa: thông tin người sinh, canh giữa.
|
|
284
|
+
const l = chart.lunar;
|
|
285
|
+
const giua = [
|
|
286
|
+
[[u.zwTitle, "title"]],
|
|
287
|
+
[[chart.name || "", "bold"]],
|
|
288
|
+
[[`${u.born} ${chart.solarDate} ${chart.time ?? ""} (${chart.hour.name})`.replace(/\s+/g, " "), ""]],
|
|
289
|
+
[[`${u.lunar} ${l.year}-${String(l.month).padStart(2, "0")}${l.leap ? " " + u.leap : ""}-${String(l.day).padStart(2, "0")} · ${chart.yearPillar}`, ""]],
|
|
290
|
+
[[chart.yinYang, "yellow"]],
|
|
291
|
+
[[`${u.bureau}: ${chart.bureau.name}`, ""]],
|
|
292
|
+
[[`${u.naYin}: ${chart.naYin}`, ""]],
|
|
293
|
+
[[`${u.life}: ${chart.palaces[chart.lifePalace].branch} · ${u.body}: ${chart.palaces[chart.bodyPalace].branch}`, "dim"]],
|
|
294
|
+
].filter((x) => x[0][0] !== "");
|
|
295
|
+
const giuaH = h[1] + 1 + h[2];
|
|
296
|
+
if (giua.length > giuaH)
|
|
297
|
+
h[2] += giua.length - giuaH;
|
|
298
|
+
const tong = h[1] + 1 + h[2];
|
|
299
|
+
const lech = Math.floor((tong - giua.length) / 2);
|
|
300
|
+
const giuaDong = (i) => fitLine(giua[i - lech] ?? [["", ""]], C, color, "center");
|
|
301
|
+
const ngang = (a, b, z) => a + b.map((x) => (x === "─" ? "─".repeat(W) : x)).join("") + z;
|
|
302
|
+
const k = (s) => paint(s, "dim", color);
|
|
303
|
+
const o1 = (r, c, i) => " " + fitLine(o[r][c][i] ?? [], W - 1, color);
|
|
304
|
+
const out = [];
|
|
305
|
+
out.push(k(ngang("┌", ["─", "┬", "─", "┬", "─", "┬", "─"], "┐")));
|
|
306
|
+
for (let i = 0; i < h[0]; i++)
|
|
307
|
+
out.push(k("│") + [0, 1, 2, 3].map((c) => o1(0, c, i)).join(k("│")) + k("│"));
|
|
308
|
+
out.push(k(ngang("├", ["─", "┼", "─", "┴", "─", "┼", "─"], "┤")));
|
|
309
|
+
for (let i = 0; i < h[1]; i++)
|
|
310
|
+
out.push(k("│") + o1(1, 0, i) + k("│") + giuaDong(i) + k("│") + o1(1, 3, i) + k("│"));
|
|
311
|
+
out.push(k("├" + "─".repeat(W) + "┤") + giuaDong(h[1]) + k("├" + "─".repeat(W) + "┤"));
|
|
312
|
+
for (let i = 0; i < h[2]; i++)
|
|
313
|
+
out.push(k("│") + o1(2, 0, i) + k("│") + giuaDong(h[1] + 1 + i) + k("│") + o1(2, 3, i) + k("│"));
|
|
314
|
+
out.push(k(ngang("├", ["─", "┼", "─", "┬", "─", "┼", "─"], "┤")));
|
|
315
|
+
for (let i = 0; i < h[3]; i++)
|
|
316
|
+
out.push(k("│") + [0, 1, 2, 3].map((c) => o1(3, c, i)).join(k("│")) + k("│"));
|
|
317
|
+
out.push(k(ngang("└", ["─", "┴", "─", "┴", "─", "┴", "─"], "┘")));
|
|
318
|
+
return out.join("\n");
|
|
319
|
+
}
|
|
320
|
+
/** Grid plus the viewing year, ten-year periods, patterns and notes. */
|
|
321
|
+
export function renderZiWei(chart, extra, opts = {}) {
|
|
322
|
+
const color = opts.color === true;
|
|
323
|
+
const u = ui(chart.lang);
|
|
324
|
+
const width = opts.width ?? 88;
|
|
325
|
+
const out = [renderZiWeiGrid(chart, opts), ...wrap(u.legend, width).map((x) => paint(x, "dim", color))];
|
|
326
|
+
if (extra.year) {
|
|
327
|
+
const y = extra.year;
|
|
328
|
+
out.push("", paint(`${u.view} ${y.year} (${y.yearPillar})`, "bold", color));
|
|
329
|
+
const dong = [
|
|
330
|
+
`${u.age} ${y.age}`,
|
|
331
|
+
`${u.yearPalace}: ${y.yearPalace.palace}`,
|
|
332
|
+
`${u.minorLimit}: ${y.minorLimit.palace}`,
|
|
333
|
+
...(y.decade ? [`${u.decade}: ${y.decade.palace} (${y.decade.fromAge}-${y.decade.toAge})`] : []),
|
|
334
|
+
...y.transformations.map((t) => `${t.type}: ${t.star}${t.palace ? " → " + t.palace : ""}`),
|
|
335
|
+
];
|
|
336
|
+
for (const x of wrapItems(dong, " · ", width - 2))
|
|
337
|
+
out.push(" " + x);
|
|
338
|
+
}
|
|
339
|
+
if (extra.decades && extra.decades.length) {
|
|
340
|
+
out.push("", paint(u.decades, "bold", color));
|
|
341
|
+
for (const d of extra.decades) {
|
|
342
|
+
const tuoi = `${String(d.fromAge).padStart(3)}-${String(d.toAge).padEnd(3)}`;
|
|
343
|
+
const nam = `${d.fromYear}-${d.toYear}`;
|
|
344
|
+
out.push(truncate(` ${tuoi} ${nam} ${d.palace} (${d.branch}) ${d.majorStars.join(", ") || "-"}`, width));
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
if (extra.patterns && extra.patterns.length) {
|
|
348
|
+
out.push("", paint(u.patterns, "bold", color));
|
|
349
|
+
for (const p of extra.patterns)
|
|
350
|
+
out.push(truncate(` ${p.name}${p.nameHan && chart.lang !== "zh" ? " " + p.nameHan : ""}${p.palace ? " · " + p.palace : ""}`, width));
|
|
351
|
+
}
|
|
352
|
+
if (chart.notes.length) {
|
|
353
|
+
out.push("");
|
|
354
|
+
for (const n of chart.notes)
|
|
355
|
+
for (const w of wrap(n, width))
|
|
356
|
+
out.push(paint(w, "dim", color));
|
|
357
|
+
}
|
|
358
|
+
return out.join("\n");
|
|
359
|
+
}
|
|
360
|
+
// ── Bảng chung ──────────────────────────────────────────────────────────
|
|
361
|
+
/** A boxed table; each cell may hold several lines. */
|
|
362
|
+
function bang(header, rows, color, widths) {
|
|
363
|
+
const n = header ? header.length : rows[0]?.length ?? 0;
|
|
364
|
+
const w = widths ??
|
|
365
|
+
Array.from({ length: n }, (_, c) => Math.max(header ? displayWidth(header[c]) : 0, ...rows.map((r) => Math.max(0, ...(r[c] ?? []).map(displayWidth)))));
|
|
366
|
+
const k = (s) => paint(s, "dim", color);
|
|
367
|
+
const vien = (a, m, z) => k(a + w.map((x) => "─".repeat(x + 2)).join(m) + z);
|
|
368
|
+
const out = [vien("┌", "┬", "┐")];
|
|
369
|
+
if (header) {
|
|
370
|
+
out.push(k("│") + header.map((hd, c) => " " + fitLine([[hd, "bold"]], w[c], color) + " ").join(k("│")) + k("│"));
|
|
371
|
+
out.push(vien("├", "┼", "┤"));
|
|
372
|
+
}
|
|
373
|
+
for (const r of rows) {
|
|
374
|
+
const hgt = Math.max(1, ...r.map((x) => x.length));
|
|
375
|
+
for (let i = 0; i < hgt; i++) {
|
|
376
|
+
out.push(k("│") + Array.from({ length: n }, (_, c) => " " + fitLine([[r[c]?.[i] ?? "", c === 0 ? "dim" : ""]], w[c], color) + " ").join(k("│")) + k("│"));
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
out.push(vien("└", "┴", "┘"));
|
|
380
|
+
return out.join("\n");
|
|
381
|
+
}
|
|
382
|
+
// ── Bát Tự ──────────────────────────────────────────────────────────────
|
|
383
|
+
/** The four pillars as a table, then the luck pillars. */
|
|
384
|
+
export function renderBaZi(chart, opts = {}) {
|
|
385
|
+
const color = opts.color === true;
|
|
386
|
+
const lang = chart.lang;
|
|
387
|
+
const u = ui(lang);
|
|
388
|
+
const width = opts.width ?? 88;
|
|
389
|
+
const tru = [chart.pillars.year, chart.pillars.month, chart.pillars.day, chart.pillars.hour];
|
|
390
|
+
const cot = (f) => tru.map((p) => (p ? f(p) : ["-"]));
|
|
391
|
+
const hang = [
|
|
392
|
+
[u.tenGod, cot((p) => [p.tenGod])],
|
|
393
|
+
[u.stem, cot((p) => [`${p.stem} ${p.stemElement}`])],
|
|
394
|
+
[u.branch, cot((p) => [`${p.branch} ${p.branchElement}`])],
|
|
395
|
+
[u.hidden, cot((p) => p.hiddenStems.map((x) => `${x.stem} ${x.tenGod}`))],
|
|
396
|
+
[u.naYin, cot((p) => [p.naYin])],
|
|
397
|
+
[u.stage, cot((p) => [p.stage])],
|
|
398
|
+
[u.stars, cot((p) => (p.stars.length ? p.stars : ["-"]))],
|
|
399
|
+
];
|
|
400
|
+
const nhan = Math.max(...hang.map((x) => displayWidth(x[0])));
|
|
401
|
+
const cw = Math.max(12, Math.min(24, Math.floor((width - nhan - 3 - 4 * 3 - 1) / 4)));
|
|
402
|
+
const rows = hang.map(([nh, cells]) => [[nh], ...cells.map((c) => c.flatMap((x) => wrap(x, cw)))]);
|
|
403
|
+
const head = ["", `${u.pYear} ${tru[0]?.label ?? ""}`, `${u.pMonth} ${tru[1]?.label ?? ""}`, `${u.pDay} ${tru[2]?.label ?? ""}`, `${u.pHour} ${tru[3]?.label ?? u.unknown}`];
|
|
404
|
+
const out = [];
|
|
405
|
+
out.push(paint(`${u.bzTitle}`, "title", color) + ` ${chart.solarDate} ${chart.time ?? u.unknown} · ${u[chart.gender]}`);
|
|
406
|
+
out.push(`${u.dayMaster}: ${paint(`${chart.dayMaster.stem} (${chart.dayMaster.yinYang} ${chart.dayMaster.element})`, "yellow", color)} · ${u.term}: ${chart.solarTerm}`);
|
|
407
|
+
out.push(bang(head, rows, color, [nhan, cw, cw, cw, cw]));
|
|
408
|
+
const lk = chart.luck;
|
|
409
|
+
out.push("");
|
|
410
|
+
out.push(paint(u.luck, "bold", color) +
|
|
411
|
+
` (${lk.direction === "forward" ? u.forward : u.backward}, ${u.startAt} ${lk.startAge.years}${u.years} ${lk.startAge.months}${u.months}, ${lk.startDate})`);
|
|
412
|
+
for (const v of lk.pillars) {
|
|
413
|
+
out.push(truncate(` ${String(v.fromAge).padStart(3)} ${v.fromYear}-${v.toYear} ${v.label} ${v.tenGod} · ${v.stage}`, width));
|
|
414
|
+
}
|
|
415
|
+
out.push("");
|
|
416
|
+
for (const n of [lk.explanation, ...chart.notes])
|
|
417
|
+
for (const w of wrap(n, width))
|
|
418
|
+
out.push(paint(w, "dim", color));
|
|
419
|
+
return out.join("\n");
|
|
420
|
+
}
|
|
421
|
+
// ── Âm lịch ─────────────────────────────────────────────────────────────
|
|
422
|
+
/** A lunar date as a small key-value box. */
|
|
423
|
+
export function renderLunar(r, opts = {}) {
|
|
424
|
+
const color = opts.color === true;
|
|
425
|
+
const u = ui(r.lang);
|
|
426
|
+
const p2 = (n) => String(n).padStart(2, "0");
|
|
427
|
+
const mon = r.leap ? `${p2(r.month)} (${u.leap})` : p2(r.month);
|
|
428
|
+
const rows = [
|
|
429
|
+
[[u.gregorian], [r.solarDate]],
|
|
430
|
+
[[u.lunar], [`${r.year}-${mon}-${p2(r.day)}`]],
|
|
431
|
+
[[u.year], [`${r.yearName} · ${r.zodiac}`]],
|
|
432
|
+
[[u.month], [r.monthName]],
|
|
433
|
+
[[u.day], [r.dayName]],
|
|
434
|
+
[[u.solarTerm], [r.solarTerm]],
|
|
435
|
+
[[u.country], [`${r.country} (UTC${r.timeZone >= 0 ? "+" : ""}${r.timeZone})`]],
|
|
436
|
+
];
|
|
437
|
+
return bang(null, rows, color);
|
|
438
|
+
}
|
|
439
|
+
/**
|
|
440
|
+
* The 24 solar terms of a year as a table. The name is in `lang` only; in
|
|
441
|
+
* English, `nameTerms` "han" or "vi" adds the Chinese or Vietnamese name in
|
|
442
|
+
* brackets ("Minor Cold (小寒)", "Minor Cold (Tiểu Hàn)"), "pinyin" (default)
|
|
443
|
+
* adds nothing.
|
|
444
|
+
*/
|
|
445
|
+
export function renderSolarTerms(year, terms, lang, opts = {}, nameTerms = "pinyin") {
|
|
446
|
+
const color = opts.color === true;
|
|
447
|
+
const u = ui(lang);
|
|
448
|
+
const kem = (t) => (lang !== "en" || nameTerms === "pinyin" ? "" : ` (${nameTerms === "vi" ? t.nameVi : term(t.nameVi, "zh")})`);
|
|
449
|
+
const rows = terms.map((t) => [[t.date], [t.time], [t.name + kem(t)], [String(t.longitude) + "°"]]);
|
|
450
|
+
return paint(`${u.terms} ${year}`, "title", color) + "\n" + bang(null, rows, color);
|
|
451
|
+
}
|
|
452
|
+
/** The twelve double hours of a day: branch, clock range, auspicious or not, spirit. */
|
|
453
|
+
export function renderHours(r, opts = {}) {
|
|
454
|
+
const color = opts.color === true;
|
|
455
|
+
const u = ui(r.lang);
|
|
456
|
+
const rows = r.hours.map((h) => [[`${h.branch} (${h.animal})`], [h.range], [h.auspicious ? u.good : u.bad], [h.spirit]]);
|
|
457
|
+
return paint(u.hrsTitle, "title", color) + ` ${r.date} · ${u.day} ${r.day}` + "\n" + bang(null, rows, color);
|
|
458
|
+
}
|
|
459
|
+
// ── Thần số học ─────────────────────────────────────────────────────────
|
|
460
|
+
/** Numerology numbers, the 3x3 birth-date grid, pinnacles and challenges. */
|
|
461
|
+
export function renderNumerology(r, opts = {}) {
|
|
462
|
+
const color = opts.color === true;
|
|
463
|
+
const L = r.labels;
|
|
464
|
+
const u = ui(r.lang);
|
|
465
|
+
const khoa = ["lifePath", "expression", "soulUrge", "personality", "attitude", "birthday", "maturity", "personalYear", "personalMonth", "personalDay"];
|
|
466
|
+
const nhan = Math.max(...khoa.map((k) => displayWidth(L[k])));
|
|
467
|
+
const out = [paint(u.nmTitle, "title", color) + ` ${r.name} · ${r.birthDate}`];
|
|
468
|
+
for (const k of khoa) {
|
|
469
|
+
const v = r[k];
|
|
470
|
+
const so = v === 11 || v === 22 || v === 33 ? paint(String(v), "magenta", color) : paint(String(v), "bold", color);
|
|
471
|
+
out.push(` ${L[k]}${" ".repeat(nhan - displayWidth(L[k]))} ${so}`);
|
|
472
|
+
}
|
|
473
|
+
if (r.karmicDebt)
|
|
474
|
+
out.push(` ${L.karmicDebt}${" ".repeat(Math.max(1, nhan - displayWidth(L.karmicDebt)))} ${r.karmicDebt}`);
|
|
475
|
+
out.push(` ${L.pinnacles}: ` + r.pinnacles.map((p) => `${p.number}${p.toAge ? ` (${u.until} ${p.toAge})` : ""}`).join(", "));
|
|
476
|
+
out.push(` ${L.challenges}: ${r.challenges.join(", ")}`);
|
|
477
|
+
// Lưới Pythagoras: 3 6 9 / 2 5 8 / 1 4 7.
|
|
478
|
+
const o = (d) => {
|
|
479
|
+
const n = r.grid[String(d)];
|
|
480
|
+
return n ? String(d).repeat(n) : "-";
|
|
481
|
+
};
|
|
482
|
+
out.push("", ` ${L.grid}`);
|
|
483
|
+
out.push(bang(null, [[[o(3)], [o(6)], [o(9)]], [[o(2)], [o(5)], [o(8)]], [[o(1)], [o(4)], [o(7)]]], color, [5, 5, 5])
|
|
484
|
+
.split("\n")
|
|
485
|
+
.map((x) => " " + x)
|
|
486
|
+
.join("\n"));
|
|
487
|
+
return out.join("\n");
|
|
488
|
+
}
|
|
489
|
+
// ── Lễ hội ──────────────────────────────────────────────────────────────
|
|
490
|
+
/** Festivals of a year as a table: date, fixed date, name. */
|
|
491
|
+
export function renderFestivals(year, list, lang, opts = {}) {
|
|
492
|
+
const color = opts.color === true;
|
|
493
|
+
const u = ui(lang);
|
|
494
|
+
const width = opts.width ?? 88;
|
|
495
|
+
const ten = Math.max(20, width - 12 - 14 - 8);
|
|
496
|
+
const rows = list.map((f) => [[f.date], [`${f.fixedDate} ${f.calendar === "lunar" ? (lang === "en" ? "lunar" : u.lunar) : ""}`.trim()], [truncate(f.name, ten)]]);
|
|
497
|
+
return paint(`${u.fest} ${year} (${list.length})`, "title", color) + "\n" + bang(null, rows, color);
|
|
498
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,67 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nhatnguyet/cli",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.3.0",
|
|
4
|
+
"description": "Command line for Nhat Nguyet: Zi Wei Dou Shu chart as a 4x4 grid, BaZi, Vietnamese/Chinese/Korean/Japanese lunar calendar, numerology, Vietnamese festivals. English, Vietnamese, Chinese. Nhat Nguyet (nhatnguyet.org)",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"author": "Nhat Nguyet (https://nhatnguyet.org)",
|
|
7
|
+
"homepage": "https://nhatnguyet.org/en/ma-nguon-mo",
|
|
8
|
+
"keywords": [
|
|
9
|
+
"cli",
|
|
10
|
+
"zi wei dou shu",
|
|
11
|
+
"ziwei",
|
|
12
|
+
"tu vi",
|
|
13
|
+
"bazi",
|
|
14
|
+
"four pillars",
|
|
15
|
+
"lunar calendar",
|
|
16
|
+
"vietnamese lunar calendar",
|
|
17
|
+
"chinese calendar",
|
|
18
|
+
"numerology",
|
|
19
|
+
"terminal"
|
|
20
|
+
],
|
|
21
|
+
"type": "module",
|
|
22
|
+
"bin": {
|
|
23
|
+
"nhatnguyet": "./dist/cli.js"
|
|
24
|
+
},
|
|
25
|
+
"main": "./dist/render.js",
|
|
26
|
+
"types": "./dist/render.d.ts",
|
|
27
|
+
"exports": {
|
|
28
|
+
".": {
|
|
29
|
+
"source": "./src/render.ts",
|
|
30
|
+
"types": "./dist/render.d.ts",
|
|
31
|
+
"default": "./dist/render.js"
|
|
32
|
+
}
|
|
33
|
+
},
|
|
34
|
+
"files": [
|
|
35
|
+
"dist",
|
|
36
|
+
"src"
|
|
37
|
+
],
|
|
38
|
+
"sideEffects": false,
|
|
39
|
+
"scripts": {
|
|
40
|
+
"build": "tsc -p tsconfig.json && chmod +x dist/cli.js",
|
|
41
|
+
"test": "node --conditions=source --test \"test/**/*.test.ts\""
|
|
42
|
+
},
|
|
43
|
+
"dependencies": {
|
|
44
|
+
"@nhatnguyet/am-lich": "^0.3.0",
|
|
45
|
+
"@nhatnguyet/bat-tu": "^0.3.0",
|
|
46
|
+
"@nhatnguyet/le-hoi": "^0.3.0",
|
|
47
|
+
"@nhatnguyet/than-so-hoc": "^0.3.0",
|
|
48
|
+
"@nhatnguyet/tu-vi": "^0.3.0"
|
|
49
|
+
},
|
|
50
|
+
"devDependencies": {
|
|
51
|
+
"typescript": "^5.9.3"
|
|
52
|
+
},
|
|
53
|
+
"engines": {
|
|
54
|
+
"node": ">=18"
|
|
55
|
+
},
|
|
56
|
+
"repository": {
|
|
57
|
+
"type": "git",
|
|
58
|
+
"url": "git+https://github.com/Nhat-Nguyet/nhatnguyet-lunar-ziwei-bazi-numerology.git",
|
|
59
|
+
"directory": "js/packages/cli"
|
|
60
|
+
},
|
|
61
|
+
"bugs": {
|
|
62
|
+
"url": "https://github.com/Nhat-Nguyet/nhatnguyet-lunar-ziwei-bazi-numerology/issues"
|
|
63
|
+
},
|
|
64
|
+
"publishConfig": {
|
|
65
|
+
"access": "public"
|
|
66
|
+
}
|
|
67
|
+
}
|