@boba-cli/chapstick 0.1.0-alpha.2

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,348 @@
1
+ import { EnvironmentAdapter, ColorSupport, TerminalBackground, StyleFn } from '@boba-cli/machine';
2
+ export { ColorSupport, TerminalBackground } from '@boba-cli/machine';
3
+
4
+ /**
5
+ * Horizontal alignment options for rendered content.
6
+ * @public
7
+ */
8
+ type HAlign = 'left' | 'center' | 'right';
9
+ /**
10
+ * Vertical alignment options for rendered content.
11
+ * @public
12
+ */
13
+ type VAlign = 'top' | 'center' | 'bottom';
14
+ /**
15
+ * Alias for horizontal alignment (backwards compatibility).
16
+ * @public
17
+ */
18
+ type Align = HAlign;
19
+ /**
20
+ * Spacing descriptor for padding or margin.
21
+ * @public
22
+ */
23
+ interface Spacing {
24
+ top: number;
25
+ right: number;
26
+ bottom: number;
27
+ left: number;
28
+ }
29
+ /**
30
+ * Characters used to render borders around content.
31
+ * @public
32
+ */
33
+ interface BorderStyle {
34
+ top: string;
35
+ bottom: string;
36
+ left: string;
37
+ right: string;
38
+ topLeft: string;
39
+ topRight: string;
40
+ bottomLeft: string;
41
+ bottomRight: string;
42
+ }
43
+ /**
44
+ * A color string or adaptive light/dark color choice.
45
+ * Accepts hex colors (#ff0000), named colors (red), or adaptive objects.
46
+ * @public
47
+ */
48
+ type ColorInput = string | {
49
+ light?: string;
50
+ dark?: string;
51
+ };
52
+ /**
53
+ * Options used internally to represent a Style.
54
+ * @public
55
+ */
56
+ interface StyleOptions {
57
+ foreground?: ColorInput;
58
+ background?: ColorInput;
59
+ bold?: boolean;
60
+ italic?: boolean;
61
+ underline?: boolean;
62
+ strikethrough?: boolean;
63
+ width?: number;
64
+ height?: number;
65
+ maxWidth?: number;
66
+ maxHeight?: number;
67
+ padding?: Partial<Spacing>;
68
+ margin?: Partial<Spacing>;
69
+ borderStyle?: BorderStyle;
70
+ borderColor?: ColorInput;
71
+ alignHorizontal?: HAlign;
72
+ alignVertical?: VAlign;
73
+ inline?: boolean;
74
+ }
75
+
76
+ /**
77
+ * Name of a border style.
78
+ * @public
79
+ */
80
+ type BorderStyleName = 'normal' | 'rounded' | 'bold' | 'double';
81
+ /**
82
+ * Predefined border styles matching common terminal box characters.
83
+ * @public
84
+ */
85
+ declare const borderStyles: Record<BorderStyleName, BorderStyle>;
86
+ /**
87
+ * Default border style (single line).
88
+ * @public
89
+ */
90
+ declare const defaultBorderStyle: BorderStyle;
91
+
92
+ /**
93
+ * Detect terminal color support levels using an environment adapter.
94
+ * @param env - Environment adapter to query color support
95
+ * @returns Color support information
96
+ * @public
97
+ */
98
+ declare function getColorSupport(env: EnvironmentAdapter): ColorSupport;
99
+ /**
100
+ * Detect whether the terminal is using a dark or light background.
101
+ * @param env - Environment adapter to query terminal background
102
+ * @returns Terminal background mode
103
+ * @public
104
+ */
105
+ declare function getTerminalBackground(env: EnvironmentAdapter): TerminalBackground;
106
+ /**
107
+ * Resolve a color input (string or adaptive light/dark) to a hex string when available.
108
+ * @param input - Color input to resolve
109
+ * @param env - Environment adapter for detecting terminal background
110
+ * @returns Resolved color string or undefined
111
+ * @public
112
+ */
113
+ declare function resolveColor(input: ColorInput | undefined, env: EnvironmentAdapter): string | undefined;
114
+
115
+ /**
116
+ * Compute ANSI-aware display width.
117
+ * @public
118
+ */
119
+ declare function width(text: string): number;
120
+ /**
121
+ * Truncate text to a maximum width per line, ANSI-aware.
122
+ * @public
123
+ */
124
+ declare function clampWidth(text: string, maxWidth?: number): string;
125
+ /**
126
+ * Wrap text to a maximum width, ANSI-aware.
127
+ * @public
128
+ */
129
+ declare function wrapWidth(text: string, maxWidth?: number): string;
130
+ /**
131
+ * Pad every line with left/right spaces.
132
+ * @public
133
+ */
134
+ declare function padLines(text: string, left?: number, right?: number): string;
135
+
136
+ /**
137
+ * Keys that can be explicitly set on a style.
138
+ * Used to track which properties have been set vs using defaults.
139
+ * @public
140
+ */
141
+ type StyleKey = keyof StyleOptions;
142
+ /**
143
+ * Context required for rendering styles.
144
+ * @public
145
+ */
146
+ interface StyleContext {
147
+ /** Environment adapter for detecting terminal capabilities. */
148
+ readonly env: EnvironmentAdapter;
149
+ /** Style function for applying ANSI styling. */
150
+ readonly styleFn: StyleFn;
151
+ }
152
+ /**
153
+ * Create a default StyleContext for layout-only styling (no ANSI colors).
154
+ * This is used internally when no context is provided.
155
+ * @public
156
+ */
157
+ declare function createDefaultContext(): StyleContext;
158
+ /**
159
+ * Set the default StyleContext used when no context is explicitly provided.
160
+ * This is useful for browser environments (xterm.js) where colors should always be enabled.
161
+ * @param context - The context to use as default, or undefined to reset to no-colors default
162
+ * @public
163
+ */
164
+ declare function setDefaultContext(context: StyleContext | undefined): void;
165
+ /**
166
+ * Fluent style builder for terminal strings.
167
+ * @public
168
+ */
169
+ declare class Style {
170
+ private readonly options;
171
+ /** Track which properties have been explicitly set */
172
+ private readonly setKeys;
173
+ /** Optional context for rendering - if not set, rendering will throw */
174
+ private readonly context?;
175
+ constructor(options?: StyleOptions, setKeys?: Set<StyleKey>, context?: StyleContext);
176
+ /**
177
+ * Create a deep copy of this style.
178
+ */
179
+ copy(): Style;
180
+ /**
181
+ * Create a copy of this style with a new context.
182
+ * @param context - The new context to use for rendering
183
+ */
184
+ withContext(context: StyleContext): Style;
185
+ /**
186
+ * Inherit unset properties from another style.
187
+ * Only copies properties that are set in `other` but not set in `this`.
188
+ * Margins and padding are NOT inherited (matching Go Lip Gloss behavior).
189
+ */
190
+ inherit(other: Style): Style;
191
+ /**
192
+ * Check if a property has been explicitly set.
193
+ */
194
+ isSet(key: StyleKey): boolean;
195
+ /**
196
+ * Unset a property, reverting to default behavior.
197
+ */
198
+ unset(...keys: StyleKey[]): Style;
199
+ foreground(color: ColorInput): Style;
200
+ background(color: ColorInput): Style;
201
+ bold(value?: boolean): Style;
202
+ italic(value?: boolean): Style;
203
+ underline(value?: boolean): Style;
204
+ strikethrough(value?: boolean): Style;
205
+ padding(all: number): Style;
206
+ padding(vertical: number, horizontal: number): Style;
207
+ padding(top: number, right: number, bottom: number, left: number): Style;
208
+ margin(all: number): Style;
209
+ margin(vertical: number, horizontal: number): Style;
210
+ margin(top: number, right: number, bottom: number, left: number): Style;
211
+ width(value: number): Style;
212
+ height(value: number): Style;
213
+ maxWidth(value: number): Style;
214
+ maxHeight(value: number): Style;
215
+ /**
216
+ * Enable borders with the default or specified style.
217
+ * Use borderStyle() to change the border characters without re-enabling.
218
+ */
219
+ border(enabled: boolean): Style;
220
+ border(style: BorderStyle): Style;
221
+ /**
222
+ * Set the border style characters.
223
+ */
224
+ borderStyle(style: BorderStyle): Style;
225
+ /**
226
+ * Set the border foreground color.
227
+ */
228
+ borderForeground(color: ColorInput): Style;
229
+ /**
230
+ * Set horizontal alignment.
231
+ * @deprecated Use alignHorizontal() instead.
232
+ */
233
+ align(value: HAlign): Style;
234
+ /**
235
+ * Set horizontal alignment (left, center, right).
236
+ */
237
+ alignHorizontal(value: HAlign): Style;
238
+ /**
239
+ * Set vertical alignment (top, center, bottom).
240
+ * Only applies when height is set.
241
+ */
242
+ alignVertical(value: VAlign): Style;
243
+ /**
244
+ * Enable inline mode. When true:
245
+ * - Newlines are stripped from the input
246
+ * - Padding and margins are not applied
247
+ */
248
+ inline(value?: boolean): Style;
249
+ /**
250
+ * Render the style to a string.
251
+ * Uses the default no-op context if none was provided.
252
+ */
253
+ render(text: string): string;
254
+ /**
255
+ * Render the style to a string with an explicit context.
256
+ * @param text - Text to render
257
+ * @param ctx - Context for rendering
258
+ */
259
+ renderWithContext(text: string, ctx: StyleContext): string;
260
+ private with;
261
+ }
262
+
263
+ /**
264
+ * Common semantic styles for terminal output.
265
+ * @public
266
+ */
267
+ interface SemanticStyles {
268
+ success: Style;
269
+ error: Style;
270
+ warning: Style;
271
+ info: Style;
272
+ muted: Style;
273
+ highlight: Style;
274
+ header: Style;
275
+ }
276
+ /**
277
+ * Provider interface for creating styles.
278
+ * Enables dependency injection for better testability.
279
+ * @public
280
+ */
281
+ interface StyleProvider {
282
+ /**
283
+ * Create a new style instance.
284
+ */
285
+ createStyle(): Style;
286
+ /**
287
+ * Get semantic styles for common use cases.
288
+ */
289
+ readonly semanticStyles: SemanticStyles;
290
+ /**
291
+ * Get the style context used by this provider.
292
+ */
293
+ readonly context: StyleContext;
294
+ }
295
+ /**
296
+ * Implementation using Chapstick's Style class with injected adapters.
297
+ * @public
298
+ */
299
+ declare class ChapstickStyleProvider implements StyleProvider {
300
+ readonly context: StyleContext;
301
+ /**
302
+ * Create a new style provider.
303
+ * @param env - Environment adapter for detecting terminal capabilities
304
+ * @param styleFn - Style function for applying ANSI styling
305
+ */
306
+ constructor(env: EnvironmentAdapter, styleFn: StyleFn);
307
+ createStyle(): Style;
308
+ get semanticStyles(): SemanticStyles;
309
+ }
310
+
311
+ /**
312
+ * Join blocks side-by-side, padding heights and inserting spacing columns.
313
+ * @param spacing - The spacing between blocks.
314
+ * @param blocks - The blocks to join.
315
+ * @public
316
+ */
317
+ declare function joinHorizontal(spacing: number, ...blocks: string[]): string;
318
+ /**
319
+ * Join blocks side-by-side, padding heights and inserting spacing columns.
320
+ * @param blocks - The blocks to join.
321
+ * @public
322
+ */
323
+ declare function joinHorizontal(...blocks: string[]): string;
324
+ /**
325
+ * Join blocks vertically with optional blank line spacing.
326
+ * @param spacing - The spacing between blocks.
327
+ * @param blocks - The blocks to join.
328
+ * @public
329
+ */
330
+ declare function joinVertical(spacing: number, ...blocks: string[]): string;
331
+ /**
332
+ * Join blocks vertically with optional blank line spacing.
333
+ * @param blocks - The blocks to join.
334
+ * @public
335
+ */
336
+ declare function joinVertical(...blocks: string[]): string;
337
+ /**
338
+ * Place content inside a fixed rectangle with alignment.
339
+ * @param width - The width of the rectangle.
340
+ * @param height - The height of the rectangle.
341
+ * @param hAlign - Horizontal alignment (left, center, right).
342
+ * @param vAlign - Vertical alignment (top, center, bottom).
343
+ * @param content - The content to place.
344
+ * @public
345
+ */
346
+ declare function place(width: number, height: number, hAlign: HAlign, vAlign: VAlign, content: string): string;
347
+
348
+ export { type Align, type BorderStyle, ChapstickStyleProvider, type ColorInput, type HAlign, type SemanticStyles, type Spacing, Style, type StyleContext, type StyleKey, type StyleOptions, type StyleProvider, type VAlign, borderStyles, clampWidth, createDefaultContext, defaultBorderStyle, getColorSupport, getTerminalBackground, joinHorizontal, joinVertical, padLines, place, resolveColor, setDefaultContext, width, wrapWidth };