Repository navigation
Expand file tree
/
Copy pathutils.ts
More file actions
165 lines (154 loc) · 4.93 KB
/
Copy pathutils.ts
File metadata and controls
165 lines (154 loc) · 4.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import fs from "node:fs/promises";
import path from "node:path";
import { QUOTES, type Quote } from "./quotes";
export type QuoteSource = "builtin" | "custom" | "both";
export function parseQuoteInput(input: string): Quote | null {
const trimmed = input.trim();
const match = trimmed.match(/^"(.+?)"\s*-\s*(.+)$/);
if (!match) return null;
const quote = match[1]!.trim();
const author = match[2]!.trim();
if (!quote || !author) return null;
return { quote, author };
}
export async function saveCustomQuote(configDir: string, quote: Quote): Promise<boolean> {
try {
const filePath = path.join(configDir, "quotes.json");
let existing: Quote[] = [];
try {
const raw = await fs.readFile(filePath, "utf-8");
const parsed: unknown = JSON.parse(raw);
if (Array.isArray(parsed)) {
existing = parsed.filter(
(item): item is Quote =>
typeof item === "object"
&& item !== null
&& typeof (item as Quote).quote === "string"
&& typeof (item as Quote).author === "string",
);
}
} catch {
// File doesn't exist or is invalid; start fresh
}
existing.push(quote);
await fs.writeFile(filePath, JSON.stringify(existing, null, 2) + "\n", "utf-8");
return true;
} catch {
return false;
}
}
export async function removeCustomQuote(configDir: string, target: Quote): Promise<boolean> {
try {
const filePath = path.join(configDir, "quotes.json");
const raw = await fs.readFile(filePath, "utf-8");
const parsed: unknown = JSON.parse(raw);
if (!Array.isArray(parsed)) return false;
const existing = parsed.filter(
(item): item is Quote =>
typeof item === "object"
&& item !== null
&& typeof (item as Quote).quote === "string"
&& typeof (item as Quote).author === "string",
);
const filtered = existing.filter(q => q.quote !== target.quote || q.author !== target.author);
if (filtered.length === existing.length) return false;
await fs.writeFile(filePath, JSON.stringify(filtered, null, 2) + "\n", "utf-8");
return true;
} catch {
return false;
}
}
export function deduplicateQuotes(quotes: Quote[]): Quote[] {
const seen = new Map<string, Quote>();
for (const q of quotes) {
const key = `${q.quote} - ${q.author}`;
if (!seen.has(key)) {
seen.set(key, q);
}
}
return [...seen.values()];
}
export async function loadCustomQuotes(configDir: string): Promise<Quote[]> {
try {
const filePath = path.join(configDir, "quotes.json");
const raw = await fs.readFile(filePath, "utf-8");
const parsed: unknown = JSON.parse(raw);
if (!Array.isArray(parsed)) return [];
const valid: Quote[] = [];
for (const item of parsed) {
if (
typeof item === "object"
&& item !== null
&& typeof (item as Quote).quote === "string"
&& typeof (item as Quote).author === "string"
) {
valid.push({ quote: (item as Quote).quote, author: (item as Quote).author });
}
}
return valid;
} catch {
return [];
}
}
export function getQuotesForSource(
source: QuoteSource,
customQuotes: Quote[],
): Quote[] {
if (source === "custom") {
return customQuotes;
}
if (source === "both") {
return deduplicateQuotes([...QUOTES, ...customQuotes]);
}
return QUOTES;
}
/**
* Handcrafted heuristic for splitting block of text into semi-equi-length lines
*/
export function wordWrap(text: string, maxWidth: number): string[] {
const words = text.split(" ");
if (words.length === 0) {
return [];
}
// Reserve 1 character for trailing space on non-terminal lines.
// This balances visual weight when lines are centered, preventing
// text from appearing skewed when punctuation (e.g. ".") has low visual weight.
maxWidth = maxWidth - 1;
const totalLen = text.length;
const targetCharsPerLine = Math.ceil(
totalLen / Math.ceil(totalLen / maxWidth),
);
const lines: string[] = [];
let wordIndex = 0;
// Accumulated width: targetCharsPerLine will not be filled perfectly; excess
// width should be accumulated in future lines to accommodate.
//
// For visual beauty, prefer funnel shaped quotes: long in beginning, becoming
// shorter in subsequent lines. To support, add extra 4 chars to capacity of
// 1st line.
let accumulatedWidth = 4;
while (wordIndex < words.length) {
let capacity = Math.min(
targetCharsPerLine + accumulatedWidth,
maxWidth,
);
let line = words[wordIndex]!;
wordIndex++;
while (wordIndex < words.length) {
const test = line + " " + words[wordIndex]!;
if (test.length <= capacity) {
line = test;
wordIndex++;
} else {
break;
}
}
lines.push(line);
accumulatedWidth = capacity - line.length;
}
// Add trailing space to non-terminal lines for visual balance
for (let i = 0; i < lines.length - 1; i++) {
lines[i] = lines[i] + " ";
}
return lines;
}