Repository navigation
Expand file tree
/
Copy patheditor-hud.ts
More file actions
58 lines (54 loc) · 2.82 KB
/
Copy patheditor-hud.ts
File metadata and controls
58 lines (54 loc) · 2.82 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
import { graphemes } from './viewport.ts';
import { fitLine } from './terminal-format.ts';
type Measure = (text: string) => number;
type Cursor = { lines: string[]; row: number; column: number };
export function editorLines(text: string, measure?: Measure) {
return fitLine(text, measure);
}
let cachedCursor: { text: string; offset: number; measure?: Measure; value: Cursor } | undefined;
export function editorCursor(text: string, offset: number, measure?: Measure) {
if (cachedCursor?.text === text && cachedCursor.offset === offset && cachedCursor.measure === measure) return cachedCursor.value;
const rows = cachedCursor?.text === text && cachedCursor.measure === measure ? cachedCursor.value.lines : fitLine(text, measure, { reserveCursor: true });
const result = (row: number, column: number) => {
const value = { lines: rows, row, column };
cachedCursor = { text, offset, measure, value };
return value;
};
const target = Math.max(0, Math.min(text.length, offset));
let start = 0;
for (let row = 0; row < rows.length; row++) {
const end = start + rows[row].length;
const hardBreak = text[end] === '\n';
if (target < end || (target === end && (hardBreak || row === rows.length - 1))) {
let column = 0;
for (const part of graphemes(rows[row])) {
if (column + part.length > target - start) break;
column += part.length;
}
return result(row, column);
}
start = end + (hardBreak ? 1 : 0);
}
return result(rows.length - 1, (rows.at(-1)?.length ?? 0));
}
export function editorDisplay(text: string, offset: number, showCursor: boolean, measure?: Measure) {
if (!text) return [{ text: showCursor ? '| Start typing…' : 'Start typing…', dim: true }];
const { lines, row, column } = editorCursor(text, offset, measure);
return lines.map((line, index) => ({ text: showCursor && index === row ? line.slice(0, column) + '|' + line.slice(column) : line, dim: false }));
}
let cachedFooter: { name: string; text: string; measure: Measure; value: string } | undefined;
export function editorFooter(name: string, text: string, measure: Measure) {
if (cachedFooter?.name === name && cachedFooter.text === text && cachedFooter.measure === measure) return cachedFooter.value;
const counts = `${graphemes(text).length} chars · ${text.trim() ? text.trim().split(/\s+/u).length : 0} words`;
const chars = graphemes((name || 'UNTITLED.md').replace(/\s+/gu, ' '));
const encoder = new TextEncoder();
const fits = (value: string) => measure(value) <= 505 && encoder.encode(value).length <= 150;
let label = chars.join('');
if (!fits(`${label} ${counts}`)) {
while (chars.length && !fits(`${chars.join('')}… ${counts}`)) chars.pop();
label = chars.join('') + '…';
}
const value = `${label} ${counts}`;
cachedFooter = { name, text, measure, value };
return value;
}