-
-
Notifications
You must be signed in to change notification settings - Fork 545
Expand file tree
/
Copy pathColorMenu.tsx
More file actions
530 lines (498 loc) · 22.3 KB
/
Copy pathColorMenu.tsx
File metadata and controls
530 lines (498 loc) · 22.3 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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
import chalk from 'chalk';
import {
Box,
Text,
useInput
} from 'ink';
import SelectInput from 'ink-select-input';
import React, { useState } from 'react';
import { getColorLevelString } from '../../types/ColorLevel';
import type { Settings } from '../../types/Settings';
import type { WidgetItem } from '../../types/Widget';
import {
applyColors,
getAvailableBackgroundColorsForUI,
getAvailableColorsForUI
} from '../../utils/colors';
import { shouldInsertInput } from '../../utils/input-guards';
import { getWidget } from '../../utils/widgets';
import { ConfirmDialog } from './ConfirmDialog';
import {
clearAllWidgetStyling,
cycleWidgetColor,
resetWidgetStyling,
setWidgetColor,
toggleWidgetBold
} from './color-menu/mutations';
export interface ColorMenuProps {
widgets: WidgetItem[];
lineIndex?: number;
settings: Settings;
onUpdate: (widgets: WidgetItem[]) => void;
onBack: () => void;
onTabSwap?: () => void;
onWidgetHighlight?: (widgetId: string | null) => void;
initialWidgetId?: string | null;
}
export const ColorMenu: React.FC<ColorMenuProps> = ({ widgets, lineIndex, settings, onUpdate, onBack, onTabSwap, onWidgetHighlight, initialWidgetId }) => {
const [showSeparators, setShowSeparators] = useState(false);
const [hexInputMode, setHexInputMode] = useState(false);
const [hexInput, setHexInput] = useState('');
const [ansi256InputMode, setAnsi256InputMode] = useState(false);
const [ansi256Input, setAnsi256Input] = useState('');
const [showClearConfirm, setShowClearConfirm] = useState(false);
const powerlineEnabled = settings.powerline.enabled;
const colorableWidgets = widgets.filter((widget) => {
// Include separators only if showSeparators is true
if (widget.type === 'separator') {
return showSeparators;
}
// Use the widget's supportsColors method
const widgetInstance = getWidget(widget.type);
// Include unknown widgets (they might support colors, we just don't know)
return widgetInstance ? widgetInstance.supportsColors(widget) : true;
});
const [highlightedItemId, setHighlightedItemId] = useState(() => {
if (initialWidgetId) {
const match = colorableWidgets.find(w => w.id === initialWidgetId);
if (match) {
return match.id;
}
}
return colorableWidgets[0]?.id ?? null;
});
const [editingBackground, setEditingBackground] = useState(false);
// Handle keyboard input
const hasNoItems = colorableWidgets.length === 0;
useInput((input, key) => {
// If no items, any key goes back
if (hasNoItems) {
onBack();
return;
}
// Skip input handling when confirmation is active - let ConfirmDialog handle it
if (showClearConfirm) {
return;
}
// Handle hex input mode
if (hexInputMode) {
// Disable arrow keys in input mode
if (key.upArrow || key.downArrow) {
return;
}
if (key.escape) {
setHexInputMode(false);
setHexInput('');
} else if (key.return) {
// Validate and apply the hex color
if (hexInput.length === 6) {
const hexColor = `hex:${hexInput}`;
const selectedWidget = colorableWidgets.find(widget => widget.id === highlightedItemId);
if (selectedWidget) {
const newItems = setWidgetColor(widgets, selectedWidget.id, hexColor, editingBackground);
onUpdate(newItems);
}
setHexInputMode(false);
setHexInput('');
}
} else if (key.backspace || key.delete) {
setHexInput(hexInput.slice(0, -1));
} else if (shouldInsertInput(input, key) && hexInput.length < 6) {
// Only accept hex characters (0-9, A-F, a-f)
const upperInput = input.toUpperCase();
if (/^[0-9A-F]$/.test(upperInput)) {
setHexInput(hexInput + upperInput);
}
}
return;
}
// Handle ansi256 input mode
if (ansi256InputMode) {
// Disable arrow keys in input mode
if (key.upArrow || key.downArrow) {
return;
}
if (key.escape) {
setAnsi256InputMode(false);
setAnsi256Input('');
} else if (key.return) {
// Validate and apply the ansi256 color
const code = parseInt(ansi256Input, 10);
if (!isNaN(code) && code >= 0 && code <= 255) {
const ansiColor = `ansi256:${code}`;
const selectedWidget = colorableWidgets.find(widget => widget.id === highlightedItemId);
if (selectedWidget) {
const newItems = setWidgetColor(widgets, selectedWidget.id, ansiColor, editingBackground);
onUpdate(newItems);
setAnsi256InputMode(false);
setAnsi256Input('');
}
}
} else if (key.backspace || key.delete) {
setAnsi256Input(ansi256Input.slice(0, -1));
} else if (shouldInsertInput(input, key) && ansi256Input.length < 3) {
// Only accept numeric characters (0-9)
if (/^[0-9]$/.test(input)) {
const newInput = ansi256Input + input;
const code = parseInt(newInput, 10);
// Only allow if it won't exceed 255
if (code <= 255) {
setAnsi256Input(newInput);
}
}
}
return;
}
// Ignore number keys to prevent SelectInput numerical navigation
if (input && /^[0-9]$/.test(input)) {
return;
}
// Tab to swap to ItemsEditor (always available since all items are colorable)
if (key.tab && onTabSwap) {
onTabSwap();
return;
}
// Normal keyboard handling when there are items
if (key.escape) {
if (editingBackground) {
setEditingBackground(false);
} else {
onBack();
}
} else if (input === 'h' || input === 'H') {
// Enter hex input mode (only in truecolor mode)
if (highlightedItemId && highlightedItemId !== 'back' && settings.colorLevel === 3) {
setHexInputMode(true);
setHexInput('');
}
} else if (input === 'a' || input === 'A') {
// Enter ansi256 input mode (only in 256 color mode)
if (highlightedItemId && highlightedItemId !== 'back' && settings.colorLevel === 2) {
setAnsi256InputMode(true);
setAnsi256Input('');
}
} else if ((input === 's' || input === 'S') && !key.ctrl) {
// Toggle show separators (only if not in powerline mode and no default separator)
if (!settings.powerline.enabled && !settings.defaultSeparator) {
setShowSeparators(!showSeparators);
// The highlighted item ID will be maintained, and we'll recalculate
// the initial index when rendering the SelectInput
}
} else if (input === 'f' || input === 'F') {
if (colorableWidgets.length > 0) {
setEditingBackground(!editingBackground);
}
} else if (input === 'b' || input === 'B') {
if (highlightedItemId && highlightedItemId !== 'back') {
// Toggle bold for the highlighted item
const selectedWidget = colorableWidgets.find(widget => widget.id === highlightedItemId);
if (selectedWidget) {
const newItems = toggleWidgetBold(widgets, selectedWidget.id);
onUpdate(newItems);
}
}
} else if (input === 'r' || input === 'R') {
if (highlightedItemId && highlightedItemId !== 'back') {
// Reset all styling (color, background, and bold) for the highlighted item
const selectedWidget = colorableWidgets.find(widget => widget.id === highlightedItemId);
if (selectedWidget) {
const newItems = resetWidgetStyling(widgets, selectedWidget.id);
onUpdate(newItems);
}
}
} else if (input === 'c' || input === 'C') {
// Show clear all confirmation
setShowClearConfirm(true);
} else if (key.leftArrow || key.rightArrow) {
// Cycle through colors with arrow keys
if (highlightedItemId && highlightedItemId !== 'back') {
const selectedWidget = colorableWidgets.find(widget => widget.id === highlightedItemId);
if (selectedWidget) {
const newItems = cycleWidgetColor({
widgets,
widgetId: selectedWidget.id,
direction: key.rightArrow ? 'right' : 'left',
editingBackground,
colors,
backgroundColors: bgColors
});
onUpdate(newItems);
}
}
}
});
if (hasNoItems) {
return (
<Box flexDirection='column'>
<Text bold>
Configure Colors
{lineIndex !== undefined ? ` - Line ${lineIndex + 1}` : ''}
</Text>
<Box marginTop={1}><Text dimColor>No colorable widgets in the status line.</Text></Box>
<Text dimColor>Add a widget first to continue.</Text>
<Box marginTop={1}><Text>Press any key to go back...</Text></Box>
</Box>
);
}
const getItemLabel = (widget: WidgetItem) => {
if (widget.type === 'separator') {
const char = widget.character ?? '|';
return `Separator: ${char === ' ' ? 'space' : char}`;
}
if (widget.type === 'flex-separator') {
return 'Flex Separator';
}
const widgetImpl = getWidget(widget.type);
return widgetImpl ? widgetImpl.getDisplayName() : `Unknown: ${widget.type}`;
};
// Color list for cycling
// Get available colors from colors.ts
const colorOptions = getAvailableColorsForUI();
const colors = colorOptions.map(c => c.value || '');
// For background, get background colors
const bgColorOptions = getAvailableBackgroundColorsForUI();
const bgColors = bgColorOptions.map(c => c.value || '');
// Create menu items with colored labels
const menuItems = colorableWidgets.map((widget, index) => {
const label = `${index + 1}: ${getItemLabel(widget)}`;
// Apply both foreground and background colors
const level = getColorLevelString(settings.colorLevel);
let defaultColor = 'white';
if (widget.type !== 'separator' && widget.type !== 'flex-separator') {
const widgetImpl = getWidget(widget.type);
if (widgetImpl) {
defaultColor = widgetImpl.getDefaultColor();
}
}
const styledLabel = applyColors(label, widget.color ?? defaultColor, widget.backgroundColor, widget.bold, level);
return {
label: styledLabel,
value: widget.id
};
});
menuItems.push({ label: '← Back', value: 'back' });
const handleSelect = (selected: { value: string }) => {
if (selected.value === 'back') {
onBack();
}
// Enter no longer cycles colors - use left/right arrow keys instead
};
const handleHighlight = (item: { value: string }) => {
setHighlightedItemId(item.value);
if (onWidgetHighlight) {
onWidgetHighlight(item.value === 'back' ? null : item.value);
}
};
// Get current color for highlighted item
const selectedWidget = highlightedItemId && highlightedItemId !== 'back'
? colorableWidgets.find(widget => widget.id === highlightedItemId)
: null;
const currentColor = editingBackground
? (selectedWidget?.backgroundColor ?? '') // Empty string for 'none'
: (selectedWidget ? (selectedWidget.color ?? (() => {
if (selectedWidget.type !== 'separator' && selectedWidget.type !== 'flex-separator') {
const widgetImpl = getWidget(selectedWidget.type);
return widgetImpl ? widgetImpl.getDefaultColor() : 'white';
}
return 'white';
})()) : 'white');
const colorList = editingBackground ? bgColors : colors;
const colorIndex = colorList.indexOf(currentColor);
const colorNumber = colorIndex === -1 ? 'custom' : colorIndex + 1;
let colorDisplay;
if (editingBackground) {
if (!currentColor || currentColor === '') {
colorDisplay = chalk.gray('(no background)');
} else {
// Determine display name based on format
let displayName;
if (currentColor.startsWith('ansi256:')) {
displayName = `ANSI ${currentColor.substring(8)}`;
} else if (currentColor.startsWith('hex:')) {
displayName = `#${currentColor.substring(4)}`;
} else {
const colorOption = bgColorOptions.find(c => c.value === currentColor);
displayName = colorOption ? colorOption.name : currentColor;
}
// Apply the color using our applyColors function with the current colorLevel
const level = getColorLevelString(settings.colorLevel);
colorDisplay = applyColors(` ${displayName} `, undefined, currentColor, false, level);
}
} else {
if (!currentColor || currentColor === '') {
colorDisplay = chalk.gray('(default)');
} else {
// Determine display name based on format
let displayName;
if (currentColor.startsWith('ansi256:')) {
displayName = `ANSI ${currentColor.substring(8)}`;
} else if (currentColor.startsWith('hex:')) {
displayName = `#${currentColor.substring(4)}`;
} else {
const colorOption = colorOptions.find(c => c.value === currentColor);
displayName = colorOption ? colorOption.name : currentColor;
}
// Apply the color using our applyColors function with the current colorLevel
const level = getColorLevelString(settings.colorLevel);
colorDisplay = applyColors(displayName, currentColor, undefined, false, level);
}
}
// Show confirmation dialog if clearing all colors
if (showClearConfirm) {
return (
<Box flexDirection='column'>
<Text bold color='yellow'>⚠ Confirm Clear All Colors</Text>
<Box marginTop={1} flexDirection='column'>
<Text>This will reset all colors for all widgets to their defaults.</Text>
<Text color='red'>This action cannot be undone!</Text>
</Box>
<Box marginTop={2}>
<Text>Continue?</Text>
</Box>
<Box marginTop={1}>
<ConfirmDialog
inline={true}
onConfirm={() => {
const newItems = clearAllWidgetStyling(widgets);
onUpdate(newItems);
setShowClearConfirm(false);
}}
onCancel={() => {
setShowClearConfirm(false);
}}
/>
</Box>
</Box>
);
}
// Check for global overrides
// Note: When powerline is enabled, background override doesn't affect the display
// since powerline uses item-specific backgrounds for segments
const hasGlobalFgOverride = !!settings.overrideForegroundColor;
const hasGlobalBgOverride = !!settings.overrideBackgroundColor && !powerlineEnabled;
const globalOverrideMessage = hasGlobalFgOverride && hasGlobalBgOverride
? '⚠ Global override for FG and BG active'
: hasGlobalFgOverride
? '⚠ Global override for FG active'
: hasGlobalBgOverride
? '⚠ Global override for BG active'
: null;
return (
<Box flexDirection='column'>
<Box>
<Text bold>
Configure Colors
{lineIndex !== undefined ? ` - Line ${lineIndex + 1}` : ''}
{editingBackground && chalk.yellow(' [Background Mode]')}
</Text>
{globalOverrideMessage && (
<Text color='yellow' dimColor>
{'. '}
{globalOverrideMessage}
</Text>
)}
</Box>
{hexInputMode ? (
<Box flexDirection='column'>
<Text>Enter 6-digit hex color code (without #):</Text>
<Text>
#
{hexInput}
<Text dimColor>{hexInput.length < 6 ? '_'.repeat(6 - hexInput.length) : ''}</Text>
</Text>
<Text> </Text>
<Text dimColor>Press Enter when done, ESC to cancel</Text>
</Box>
) : ansi256InputMode ? (
<Box flexDirection='column'>
<Text>Enter ANSI 256 color code (0-255):</Text>
<Text>
{ansi256Input}
<Text dimColor>{ansi256Input.length === 0 ? '___' : ansi256Input.length === 1 ? '__' : ansi256Input.length === 2 ? '_' : ''}</Text>
</Text>
<Text> </Text>
<Text dimColor>Press Enter when done, ESC to cancel</Text>
</Box>
) : (
<>
<Text dimColor>
↑↓ to select, ←→ to cycle
{' '}
{editingBackground ? 'background' : 'foreground'}
, (f) to toggle bg/fg, (b)old,
{settings.colorLevel === 3 ? ' (h)ex,' : settings.colorLevel === 2 ? ' (a)nsi256,' : ''}
{' '}
(r)eset, (c)lear all,
{onTabSwap ? ' ⇥ edit items,' : ''}
{' '}
ESC to go back
</Text>
{!settings.powerline.enabled && !settings.defaultSeparator && (
<Text dimColor>
(s)how separators:
{showSeparators ? chalk.green('ON') : chalk.gray('OFF')}
</Text>
)}
{selectedWidget ? (
<Box marginTop={1}>
<Text>
Current
{' '}
{editingBackground ? 'background' : 'foreground'}
{' '}
(
{colorNumber === 'custom' ? 'custom' : `${colorNumber}/${colorList.length}`}
):
{' '}
{colorDisplay}
{selectedWidget.bold && chalk.bold(' [BOLD]')}
</Text>
</Box>
) : (
<Box marginTop={1}>
<Text> </Text>
</Box>
)}
</>
)}
<Box marginTop={1}>
{(hexInputMode || ansi256InputMode) ? (
// Static list when in input mode - no keyboard interaction
<Box flexDirection='column'>
{menuItems.map(item => (
<Text
key={item.value}
color={item.value === highlightedItemId ? 'cyan' : 'white'}
bold={item.value === highlightedItemId}
>
{item.value === highlightedItemId ? '▶ ' : ' '}
{item.label}
</Text>
))}
</Box>
) : (
// Interactive SelectInput when not in input mode
<SelectInput
key={`${showSeparators}-${highlightedItemId}`}
items={menuItems}
onSelect={handleSelect}
onHighlight={handleHighlight}
initialIndex={Math.max(0, menuItems.findIndex(item => item.value === highlightedItemId))}
indicatorComponent={({ isSelected }) => (
<Text>{isSelected ? '▶' : ' '}</Text>
)}
itemComponent={({ isSelected, label }) => (
// The label already has ANSI codes applied via applyColors()
// We need to pass it directly as a single Text child to preserve the codes
<Text>{` ${label}`}</Text>
)}
/>
)}
</Box>
<Box marginTop={1} flexDirection='column'>
<Text color='yellow'>⚠ VSCode Users: </Text>
<Text dimColor wrap='wrap'>If colors appear incorrect in the VSCode integrated terminal, the "Terminal › Integrated: Minimum Contrast Ratio" (`terminal.integrated.minimumContrastRatio`) setting is forcing a minimum contrast between foreground and background colors. You can adjust this setting to 1 to disable the contrast enforcement, or use a standalone terminal for accurate colors.</Text>
</Box>
</Box>
);
};