specsmd 0.1.57 → 0.1.59

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,145 @@
1
+ const fs = require('fs');
2
+ const { truncate, clampIndex } = require('./helpers');
3
+ const { colorizeMarkdownLine, sanitizeRenderLine } = require('./overlays');
4
+ const {
5
+ loadGitDiffPreview,
6
+ loadGitCommitPreview
7
+ } = require('../git/changes');
8
+
9
+ function buildPreviewLines(fileEntry, width, scrollOffset, options = {}) {
10
+ const fullDocument = options?.fullDocument === true;
11
+
12
+ if (!fileEntry || typeof fileEntry.path !== 'string') {
13
+ return [{ text: truncate('No file selected', width), color: 'gray', bold: false }];
14
+ }
15
+
16
+ const isGitFilePreview = fileEntry.previewType === 'git-diff';
17
+ const isGitCommitPreview = fileEntry.previewType === 'git-commit-diff';
18
+ const isGitPreview = isGitFilePreview || isGitCommitPreview;
19
+ let rawLines = [];
20
+ if (isGitPreview) {
21
+ const diffText = isGitCommitPreview
22
+ ? loadGitCommitPreview(fileEntry)
23
+ : loadGitDiffPreview(fileEntry);
24
+ rawLines = String(diffText || '').split(/\r?\n/);
25
+ } else {
26
+ let content;
27
+ try {
28
+ content = fs.readFileSync(fileEntry.path, 'utf8');
29
+ } catch (error) {
30
+ return [{
31
+ text: truncate(`Unable to read ${fileEntry.label || fileEntry.path}: ${error.message}`, width),
32
+ color: 'red',
33
+ bold: false
34
+ }];
35
+ }
36
+ rawLines = String(content).split(/\r?\n/);
37
+ }
38
+
39
+ const headLine = {
40
+ text: truncate(
41
+ isGitCommitPreview
42
+ ? `commit: ${fileEntry.commitHash || fileEntry.path}`
43
+ : `${isGitPreview ? 'diff' : 'file'}: ${fileEntry.path}`,
44
+ width
45
+ ),
46
+ color: 'cyan',
47
+ bold: true
48
+ };
49
+
50
+ const normalizedLines = rawLines.map((line) => sanitizeRenderLine(line));
51
+ const cappedLines = fullDocument ? normalizedLines : normalizedLines.slice(0, 300);
52
+ const hiddenLineCount = fullDocument ? 0 : Math.max(0, rawLines.length - cappedLines.length);
53
+ let inCodeBlock = false;
54
+
55
+ const highlighted = cappedLines.map((rawLine, index) => {
56
+ const prefixedLine = `${String(index + 1).padStart(4, ' ')} | ${rawLine}`;
57
+ let color;
58
+ let bold;
59
+ let togglesCodeBlock = false;
60
+
61
+ if (isGitPreview) {
62
+ if (rawLine.startsWith('+++ ') || rawLine.startsWith('--- ') || rawLine.startsWith('diff --git')) {
63
+ color = 'cyan';
64
+ bold = true;
65
+ } else if (rawLine.startsWith('@@')) {
66
+ color = 'magenta';
67
+ bold = true;
68
+ } else if (rawLine.startsWith('+')) {
69
+ color = 'green';
70
+ bold = false;
71
+ } else if (rawLine.startsWith('-')) {
72
+ color = 'red';
73
+ bold = false;
74
+ } else {
75
+ color = undefined;
76
+ bold = false;
77
+ }
78
+ } else {
79
+ const markdownStyle = colorizeMarkdownLine(rawLine, inCodeBlock);
80
+ color = markdownStyle.color;
81
+ bold = markdownStyle.bold;
82
+ togglesCodeBlock = markdownStyle.togglesCodeBlock;
83
+ }
84
+
85
+ if (togglesCodeBlock) {
86
+ inCodeBlock = !inCodeBlock;
87
+ }
88
+ return {
89
+ text: truncate(prefixedLine, width),
90
+ color,
91
+ bold
92
+ };
93
+ });
94
+
95
+ if (hiddenLineCount > 0) {
96
+ highlighted.push({
97
+ text: truncate(`... ${hiddenLineCount} additional lines hidden`, width),
98
+ color: 'gray',
99
+ bold: false
100
+ });
101
+ }
102
+
103
+ const clampedOffset = clampIndex(scrollOffset, highlighted.length);
104
+ const body = highlighted.slice(clampedOffset);
105
+
106
+ return [headLine, { text: '', color: undefined, bold: false }, ...body];
107
+ }
108
+
109
+ function allocateSingleColumnPanels(candidates, rowsBudget) {
110
+ const filtered = (candidates || []).filter(Boolean);
111
+ if (filtered.length === 0) {
112
+ return [];
113
+ }
114
+
115
+ const selected = [];
116
+ let remaining = Math.max(4, rowsBudget);
117
+
118
+ for (const panel of filtered) {
119
+ const margin = selected.length > 0 ? 1 : 0;
120
+ const minimumRows = 4 + margin;
121
+
122
+ if (remaining >= minimumRows || selected.length === 0) {
123
+ selected.push({
124
+ ...panel,
125
+ maxLines: 1
126
+ });
127
+ remaining -= minimumRows;
128
+ }
129
+ }
130
+
131
+ let index = 0;
132
+ while (remaining > 0 && selected.length > 0) {
133
+ const panelIndex = index % selected.length;
134
+ selected[panelIndex].maxLines += 1;
135
+ remaining -= 1;
136
+ index += 1;
137
+ }
138
+
139
+ return selected;
140
+ }
141
+
142
+ module.exports = {
143
+ buildPreviewLines,
144
+ allocateSingleColumnPanels
145
+ };