Repository navigation
Expand file tree
/
Copy pathutils.js
More file actions
85 lines (78 loc) · 2.18 KB
/
Copy pathutils.js
File metadata and controls
85 lines (78 loc) · 2.18 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
// /utils.js
// Formats milliseconds into a human-readable duration string.
export function formatDuration(ms) {
if (ms < 1000) {
return `${Math.round(ms)}ms`;
}
const seconds = ms / 1000;
if (seconds < 60) {
return `${seconds.toFixed(1)}s`;
}
const minutes = Math.floor(seconds / 60);
const remainingSeconds = (seconds % 60).toFixed(0);
return `${minutes}m ${remainingSeconds}s`;
}
// Formats milliseconds into compact elapsed time for checkpoints.
export function formatElapsed(ms) {
const seconds = Math.round(ms / 1000);
if (seconds < 60) {
return `${seconds}s`;
}
const minutes = Math.floor(seconds / 60);
const remainingSeconds = seconds % 60;
if (remainingSeconds === 0) {
return `${minutes}m`;
}
return `${minutes}m ${remainingSeconds}s`;
}
// Truncates text to a max length with ellipsis.
export function truncateText(text, maxLength = 50) {
if (text.length <= maxLength) {
return text;
}
return text.slice(0, maxLength - 3) + '...';
}
// Copies text to clipboard.
export async function copyToClipboard(text) {
try {
await navigator.clipboard.writeText(text);
return true;
} catch (err) {
console.error('Failed to copy to clipboard:', err);
return false;
}
}
// Downloads content as a file.
export function downloadFile(content, filename, mimeType = 'text/plain') {
const blob = new Blob([content], { type: mimeType });
const url = URL.createObjectURL(blob);
const link = document.createElement('a');
link.href = url;
link.download = filename;
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
URL.revokeObjectURL(url);
}
// Gets the file extension for an export format.
export function getExportExtension(format) {
const extensions = {
plaintext: 'txt',
json: 'json',
xml: 'xml',
xmltags: 'xml',
markdown: 'md'
};
return extensions[format] || 'txt';
}
// Gets the MIME type for an export format.
export function getExportMimeType(format) {
const mimeTypes = {
plaintext: 'text/plain',
json: 'application/json',
xml: 'application/xml',
xmltags: 'text/plain',
markdown: 'text/markdown'
};
return mimeTypes[format] || 'text/plain';
}