-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtext.ts
More file actions
296 lines (255 loc) · 7.63 KB
/
Copy pathtext.ts
File metadata and controls
296 lines (255 loc) · 7.63 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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
// MARK: - Sanitization
export function sanitize(text: string): string {
let result = "";
for (let i = 0; i < text.length; i++) {
const ch = text[i];
const code = text.charCodeAt(i);
// Strip ANSI escape sequences
if (ch === "\x1b" && i + 1 < text.length && text[i + 1] === "[") {
let j = i + 2;
while (j < text.length) {
const c = text[j];
if ((c >= "A" && c <= "Z") || (c >= "a" && c <= "z")) {
i = j;
break;
}
j++;
}
if (j >= text.length) i = j - 1;
continue;
}
// Strip ASCII control chars (except \n and \t)
if (code < 0x20 && ch !== "\n" && ch !== "\t") continue;
// Strip C1 controls
if (code >= 0x7f && code <= 0x9f) continue;
result += ch;
}
return result;
}
// MARK: - Date formatting
export function formatDate(dateStr: string): string {
const date = new Date(dateStr);
if (isNaN(date.getTime())) return dateStr;
const now = new Date();
const today = new Date(now.getFullYear(), now.getMonth(), now.getDate());
const yesterday = new Date(today.getTime() - 86400000);
const msgDay = new Date(date.getFullYear(), date.getMonth(), date.getDate());
const time = date.toLocaleTimeString("en-US", {
hour: "numeric",
minute: "2-digit",
hour12: true,
});
if (msgDay.getTime() === today.getTime()) return `Today, ${time}`;
if (msgDay.getTime() === yesterday.getTime()) return `Yesterday, ${time}`;
const dateStr2 = date.toLocaleDateString("en-US", {
month: "short",
day: "numeric",
year: date.getFullYear() !== now.getFullYear() ? "numeric" : undefined,
});
return `${dateStr2}, ${time}`;
}
export function formatDayHeader(): string {
const now = new Date();
const options: Intl.DateTimeFormatOptions = {
weekday: "long",
month: "short",
day: "numeric",
year: "numeric",
};
return `Today, ${now.toLocaleDateString("en-US", options)}`;
}
// MARK: - Accounts text
interface AccountInfo {
name: string;
email: string;
enabled: boolean;
}
export function printAccountsText(accounts: AccountInfo[]): void {
if (accounts.length === 0) {
console.log("No accounts found.");
return;
}
const nameWidth = Math.max(7, ...accounts.map((a) => a.name.length));
console.log(
`${"ACCOUNT".padEnd(nameWidth)} EMAIL`,
);
for (const a of accounts) {
const enabled = a.enabled ? "" : " (disabled)";
console.log(`${sanitize(a.name).padEnd(nameWidth)} ${a.email}${enabled}`);
}
}
// MARK: - Mailboxes text
interface MailboxInfo {
name: string;
unreadCount: number;
}
export function printMailboxesText(mailboxes: MailboxInfo[]): void {
if (mailboxes.length === 0) {
console.log("No mailboxes found.");
return;
}
const nameWidth = Math.max(7, ...mailboxes.map((m) => m.name.length));
console.log(`${"MAILBOX".padEnd(nameWidth)} UNREAD`);
for (const m of mailboxes) {
console.log(
`${sanitize(m.name).padEnd(nameWidth)} ${m.unreadCount}`,
);
}
}
// MARK: - Message list text
interface MessageSummary {
id: number;
subject: string;
sender: string;
dateSent: string;
read: boolean;
flagged?: boolean;
mailbox?: string;
}
export function printMessagesText(
messages: MessageSummary[],
): void {
if (messages.length === 0) {
console.log("No messages found.");
return;
}
for (const m of messages) {
const subject = sanitize(m.subject || "(no subject)");
const tags: string[] = [];
if (!m.read) tags.push("unread");
if (m.flagged) tags.push("flagged");
const tagStr = tags.length > 0 ? ` ${tags.join(", ")}` : "";
console.log(subject);
let detail = ` ${sanitize(m.sender)} ${formatDate(m.dateSent)}`;
if (m.mailbox) detail += ` [${sanitize(m.mailbox)}]`;
detail += tagStr;
console.log(detail);
console.log(` ID: ${m.id}`);
}
}
// MARK: - Message detail text
interface MessageDetail {
id: number;
subject: string;
sender: string;
to: string[];
cc: string[];
dateSent: string;
dateReceived: string;
read: boolean;
flagged: boolean;
body: string;
html?: string;
}
export function printMessageDetailText(msg: MessageDetail): void {
console.log(`Subject: ${sanitize(msg.subject || "(no subject)")}`);
console.log(`From: ${sanitize(msg.sender)}`);
if (msg.to.length > 0) console.log(`To: ${msg.to.join(", ")}`);
if (msg.cc.length > 0) console.log(`CC: ${msg.cc.join(", ")}`);
console.log(`Date: ${formatDate(msg.dateSent)}`);
const tags: string[] = [];
if (!msg.read) tags.push("unread");
if (msg.flagged) tags.push("flagged");
if (tags.length > 0) console.log(`Status: ${tags.join(", ")}`);
console.log(`ID: ${msg.id}`);
if (msg.body) {
console.log("");
console.log(sanitize(msg.body));
}
}
// MARK: - Unread messages text
interface UnreadMessages {
account: string;
mailbox: string;
unreadCount: number;
messages: MessageSummary[];
}
export function printUnreadMessagesText(
data: UnreadMessages,
): void {
console.log(
`${sanitize(data.account)} — ${data.unreadCount} unread in ${data.mailbox}`,
);
console.log("");
printMessagesText(data.messages);
}
// MARK: - Attachment list text
interface AttachmentInfo {
name: string;
mimeType: string | null;
fileSize: number | null;
downloaded: boolean | null;
}
export function printAttachmentsText(attachments: AttachmentInfo[]): void {
if (attachments.length === 0) {
console.log("No attachments.");
return;
}
for (const a of attachments) {
let line = sanitize(a.name);
if (a.mimeType) line += ` ${a.mimeType}`;
if (a.fileSize != null) line += ` ${formatSize(a.fileSize)}`;
if (a.downloaded === false) line += " (not downloaded)";
console.log(line);
}
}
function formatSize(bytes: number): string {
if (bytes < 1024) return `${bytes} B`;
if (bytes < 1024 * 1024) return `${(bytes / 1024).toFixed(1)} KB`;
return `${(bytes / (1024 * 1024)).toFixed(1)} MB`;
}
interface SavedAttachment {
name: string;
path: string;
}
export function printSavedAttachmentsText(saved: SavedAttachment[]): void {
if (saved.length === 0) {
console.log("No attachments saved.");
return;
}
for (const s of saved) {
console.log(`Saved: ${sanitize(s.name)} → ${s.path}`);
}
}
// MARK: - Confirmation text
export function printSentText(
result: { sent: boolean; to: string; subject: string },
): void {
console.log(`Sent: ${sanitize(result.subject)}`);
console.log(` To: ${result.to}`);
}
export function printDraftText(
result: { drafted: boolean; to: string; subject: string },
): void {
console.log(`Draft saved: ${sanitize(result.subject)}`);
console.log(` To: ${result.to}`);
}
export function printMarkedText(
result: { marked: boolean; id: number; read: boolean; flagged: boolean },
): void {
const tags: string[] = [];
if (result.read) tags.push("read");
else tags.push("unread");
if (result.flagged) tags.push("flagged");
console.log(`Marked: message ${result.id} ${tags.join(", ")}`);
}
export function printMovedText(
result: { moved: boolean; id: number; to: string },
): void {
console.log(`Moved: message ${result.id} → ${sanitize(result.to)}`);
}
export function printJunkedText(result: { junked: boolean; id: number }): void {
console.log(`Junked: message ${result.id}`);
}
export function printRepliedText(
result: { replied: boolean; to: string; subject: string },
): void {
console.log(`Replied: ${sanitize(result.subject)}`);
console.log(` To: ${result.to}`);
}
export function printForwardedText(
result: { forwarded: boolean; to: string; subject: string },
): void {
console.log(`Forwarded: ${sanitize(result.subject)}`);
console.log(` To: ${result.to}`);
}