summaryrefslogtreecommitdiff
path: root/src/utils/grid.ts
blob: 1a2aa29d59b202a1d9694998a1556a6ca9c18779 (plain)
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
import type { AnsiTermColor, Grid } from '@/types/grid';
import { getAnsiColorEscape, getAnsiEscapeCodeFromDiff, parseAnsiColorCode } from './ansi';

const defaultColor: AnsiTermColor = { foreground: null, background: null };

export const gridFromAnsi = (ansiText: string): Grid => {
    const lines = ansiText.split('\n');
    const grid: Grid = [];
    
    for (let y = 0; y < lines.length; y++) {
        const line = lines[y];
        const row: Grid[0] = [];
        let x = 0;
        let currentColor: AnsiTermColor = { ...defaultColor };
        
        // Regex to match ANSI escape codes
        const ansiRegex = /\x1b\[([0-9;]+)m/g;
        let lastIndex = 0;
        let match;
        
        while ((match = ansiRegex.exec(line)) !== null) {
            // Add characters before this escape code
            const text = line.slice(lastIndex, match.index);
            for (const char of text) {
                row.push({ char, color: { ...currentColor }, x: x++, y });
            }
            
            // Parse escape code
            const codes = match[1].split(';').map(Number);
            let i = 0;
            while (i < codes.length) {
                const code = codes[i];
                
                if (code === 38 && codes[i + 1] === 5) {
                    // Foreground color: ESC[38;5;{code}m
                    const colorCode = codes[i + 2];
                    currentColor.foreground = parseAnsiColorCode(colorCode);
                    i += 3;
                } else if (code === 48 && codes[i + 1] === 5) {
                    // Background color: ESC[48;5;{code}m
                    const colorCode = codes[i + 2];
                    currentColor.background = parseAnsiColorCode(colorCode);
                    i += 3;
                } else if (code === 39) {
                    // Reset foreground
                    currentColor.foreground = null;
                    i++;
                } else if (code === 49) {
                    // Reset background
                    currentColor.background = null;
                    i++;
                } else {
                    i++;
                }
            }
            
            lastIndex = ansiRegex.lastIndex;
        }
        
        // Add remaining characters
        const remainingText = line.slice(lastIndex);
        for (const char of remainingText) {
            row.push({ char, color: { ...currentColor }, x: x++, y });
        }
        
        grid.push(row);
    }
    
    // Normalize grid width
    const maxWidth = Math.max(...grid.map(row => row.length));
    for (let y = 0; y < grid.length; y++) {
        while (grid[y].length < maxWidth) {
            const x = grid[y].length;
            grid[y].push({
                char: ' ',
                color: { ...defaultColor },
                x,
                y,
            });
        }
    }
    
    return grid;
};

export const gridFromAscii = (
    ascii: string,
    color: AnsiTermColor = defaultColor,
): Grid => {
    const lineWidth = Math.max(...ascii.split('\n').map((line) => line.length));
    return ascii.split('\n').map((line, y) =>
        line
            .split('')
            .map((char, x) => ({
                char,
                color,
                x,
                y,
            }))
            .concat(
                Array(lineWidth - line.length)
                    .fill(0)
                    .map((_, x) => ({
                        char: ' ',
                        color,
                        x: x + line.length,
                        y,
                    })),
            ),
    );
};

export const gridToAnsi = (grid: Grid) => {
    const reset: AnsiTermColor = { foreground: null, background: null };
    const { fg, bg } = getAnsiColorEscape(reset);
    const resetCode = `${fg}${bg}`;

    const rows = [];
    for (let y = 0; y < grid.length; y++) {
        let row = '';
        for (let x = 0; x < grid[y].length; x++) {
            const cell = grid[y][x];
            const previousColor = x > 0 ? grid[y][x - 1].color : reset;
            row +=
                getAnsiEscapeCodeFromDiff(previousColor, cell.color) +
                cell.char;
        }
        rows.push(row);
    }
    return resetCode + rows.join(resetCode + '\n');
};