Skip to content

Commit 9c8326f

Browse files
committed
cleanup
1 parent 336c703 commit 9c8326f

2 files changed

Lines changed: 24 additions & 27 deletions

File tree

packages/core/src/prompts/date.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,9 @@ export default class DatePrompt extends Prompt<Date> {
272272
);
273273

274274
const newNum = isBlank
275-
? (direction === 1 ? bounds.min : bounds.max)
275+
? direction === 1
276+
? bounds.min
277+
: bounds.max
276278
: clamp(bounds.min, num + direction, bounds.max);
277279

278280
const newSegmentValue = String(newNum).padStart(segment.len, '0');
@@ -323,9 +325,7 @@ export default class DatePrompt extends Prompt<Date> {
323325

324326
const firstBlank = segmentDisplay.indexOf('_');
325327
const pos =
326-
firstBlank >= 0
327-
? firstBlank
328-
: Math.min(this.#cursor.positionInSegment, segment.len - 1);
328+
firstBlank >= 0 ? firstBlank : Math.min(this.#cursor.positionInSegment, segment.len - 1);
329329
if (pos < 0 || pos >= segment.len) return;
330330

331331
const newSegmentVal = segmentDisplay.slice(0, pos) + char + segmentDisplay.slice(pos + 1);

packages/prompts/src/date.ts

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1+
import { styleText } from 'node:util';
12
import type { DateFormatConfig, DateParts } from '@clack/core';
23
import { DatePrompt, settings } from '@clack/core';
3-
import color from 'picocolors';
44
import { type CommonOptions, S_BAR, S_BAR_END, symbol } from './common.js';
55

66
export type DateFormat = 'YYYY/MM/DD' | 'MM/DD/YYYY' | 'DD/MM/YYYY';
@@ -35,17 +35,19 @@ function renderSegment(
3535
if (isBlank) {
3636
if (cursorInThis) {
3737
for (let j = 0; j < label.length; j++) {
38-
parts.push(j === cursor.positionInSegment ? color.inverse(' ') : color.dim(label[j]));
38+
parts.push(
39+
j === cursor.positionInSegment ? styleText('inverse', ' ') : styleText('dim', label[j])
40+
);
3941
}
4042
} else {
41-
parts.push(color.dim(label));
43+
parts.push(styleText('dim', label));
4244
}
4345
} else {
4446
for (let j = 0; j < value.length; j++) {
4547
if (cursorInThis && j === cursor.positionInSegment) {
46-
parts.push(value[j] === '_' ? color.inverse(' ') : color.inverse(value[j]));
48+
parts.push(value[j] === '_' ? styleText('inverse', ' ') : styleText('inverse', value[j]));
4749
} else {
48-
parts.push(value[j] === '_' ? color.dim(' ') : value[j]);
50+
parts.push(value[j] === '_' ? styleText('dim', ' ') : value[j]);
4951
}
5052
}
5153
}
@@ -64,12 +66,11 @@ function renderDateFormat(
6466
return config.format(parts);
6567
}
6668
const labels = config.segmentLabels ?? DEFAULT_SEGMENT_LABELS;
67-
const sep = color.gray('/');
69+
const sep = styleText('gray', '/');
6870
const rendered = config.segments.map((seg, i) =>
6971
renderSegment(parts[seg.type], i, cursor, labels[seg.type], state)
7072
);
7173
let result = rendered.join(sep);
72-
// Add cursor block if beyond last segment
7374
const lastSeg = config.segments[config.segments.length - 1];
7475
if (
7576
cursor.segmentIndex >= config.segments.length ||
@@ -146,14 +147,12 @@ export const date = (opts: DateOptions) => {
146147
output: opts.output,
147148
render() {
148149
const hasGuide = (opts?.withGuide ?? settings.withGuide) !== false;
149-
const titlePrefix = `${hasGuide ? `${color.gray(S_BAR)}\n` : ''}${symbol(this.state)} `;
150+
const titlePrefix = `${hasGuide ? `${styleText('gray', S_BAR)}\n` : ''}${symbol(this.state)} `;
150151
const title = `${titlePrefix}${opts.message}\n`;
151152

152-
// Get segment values and cursor from core
153153
const segmentValues = this.segmentValues;
154154
const segmentCursor = this.segmentCursor;
155155

156-
// Determine render state
157156
const renderState: RenderState =
158157
this.state === 'submit'
159158
? 'submit'
@@ -163,7 +162,6 @@ export const date = (opts: DateOptions) => {
163162
? 'error'
164163
: 'active';
165164

166-
// Render using generic data-driven renderer
167165
const userInput = renderDateFormat(segmentValues, segmentCursor, renderState, formatConfig);
168166

169167
const value =
@@ -177,28 +175,27 @@ export const date = (opts: DateOptions) => {
177175

178176
switch (this.state) {
179177
case 'error': {
180-
const errorText = this.error ? ` ${color.yellow(this.error)}` : '';
181-
const errorPrefix = hasGuide ? `${color.yellow(S_BAR)} ` : '';
182-
const errorPrefixEnd = hasGuide ? color.yellow(S_BAR_END) : '';
178+
const errorText = this.error ? ` ${styleText('yellow', this.error)}` : '';
179+
const errorPrefix = hasGuide ? `${styleText('yellow', S_BAR)} ` : '';
180+
const errorPrefixEnd = hasGuide ? styleText('yellow', S_BAR_END) : '';
183181
return `${title.trim()}\n${errorPrefix}${userInput}\n${errorPrefixEnd}${errorText}\n`;
184182
}
185183
case 'submit': {
186-
const valueText = value ? ` ${color.dim(value)}` : '';
187-
const submitPrefix = hasGuide ? color.gray(S_BAR) : '';
184+
const valueText = value ? ` ${styleText('dim', value)}` : '';
185+
const submitPrefix = hasGuide ? styleText('gray', S_BAR) : '';
188186
return `${title}${submitPrefix}${valueText}`;
189187
}
190188
case 'cancel': {
191-
const valueText = value ? ` ${color.strikethrough(color.dim(value))}` : '';
192-
const cancelPrefix = hasGuide ? color.gray(S_BAR) : '';
189+
const valueText = value ? ` ${styleText(['strikethrough', 'dim'], value)}` : '';
190+
const cancelPrefix = hasGuide ? styleText('gray', S_BAR) : '';
193191
return `${title}${cancelPrefix}${valueText}${value.trim() ? `\n${cancelPrefix}` : ''}`;
194192
}
195193
default: {
196-
const defaultPrefix = hasGuide ? `${color.cyan(S_BAR)} ` : '';
197-
const defaultPrefixEnd = hasGuide ? color.cyan(S_BAR_END) : '';
198-
// Inline validation: extra bar (│) below date, bar end (└) only at the end
199-
const inlineErrorBar = hasGuide ? `${color.cyan(S_BAR)} ` : '';
194+
const defaultPrefix = hasGuide ? `${styleText('cyan', S_BAR)} ` : '';
195+
const defaultPrefixEnd = hasGuide ? styleText('cyan', S_BAR_END) : '';
196+
const inlineErrorBar = hasGuide ? `${styleText('cyan', S_BAR)} ` : '';
200197
const inlineError = (this as { inlineError?: string }).inlineError
201-
? `\n${inlineErrorBar}${color.yellow((this as { inlineError: string }).inlineError)}`
198+
? `\n${inlineErrorBar}${styleText('yellow', (this as { inlineError: string }).inlineError)}`
202199
: '';
203200
return `${title}${defaultPrefix}${userInput}${inlineError}\n${defaultPrefixEnd}\n`;
204201
}

0 commit comments

Comments
 (0)