Skip to content

Commit 3978594

Browse files
authored
Merge pull request #42 from bombshell-dev/refactor-alignment
refactor: alignment to use string literals instead of magic numbers
2 parents 9c1f6cd + 7025bdc commit 3978594

5 files changed

Lines changed: 37 additions & 18 deletions

File tree

examples/inline-regions/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,7 @@ function box(msg: string, fg: number, border: number): Op[] {
333333
height: grow(),
334334
direction: "ttb",
335335
padding: { left: 1 },
336-
alignY: 2,
336+
alignY: "center",
337337
},
338338
border: {
339339
color: border,

examples/keyboard/index.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -225,8 +225,8 @@ function key(ops: Op[], k: KeyDef, ctx: AppContext): void {
225225
width: fixed(w),
226226
height: grow(),
227227
padding: { left: 1, right: 1 },
228-
alignX: 2,
229-
alignY: 2,
228+
alignX: "center",
229+
alignY: "center",
230230
},
231231
bg,
232232
border: hover
@@ -565,8 +565,8 @@ function keyboard(ctx: AppContext): Op[] {
565565
width: grow(),
566566
height: grow(),
567567
direction: "ttb",
568-
alignX: 2,
569-
alignY: 2,
568+
alignX: "center",
569+
alignY: "center",
570570
padding: { left: 2, top: 1 },
571571
},
572572
}),
@@ -583,7 +583,7 @@ function keyboard(ctx: AppContext): Op[] {
583583
layout: {
584584
width: grow(),
585585
direction: "ltr",
586-
alignY: 0,
586+
alignY: "top",
587587
padding: { bottom: 1 },
588588
},
589589
}),
@@ -634,7 +634,7 @@ function keyboard(ctx: AppContext): Op[] {
634634

635635
// config panel (right)
636636
ops.push(
637-
open("", { layout: { width: grow(), direction: "ltr", alignX: 1 } }),
637+
open("", { layout: { width: grow(), direction: "ltr", alignX: "right" } }),
638638
);
639639
configPanel(ops, ctx);
640640
ops.push(close());
@@ -648,7 +648,7 @@ function keyboard(ctx: AppContext): Op[] {
648648
layout: {
649649
direction: "ltr",
650650
gap: 3,
651-
alignY: 1,
651+
alignY: "bottom",
652652
padding: { left: 1, right: 1, top: 1, bottom: 1 },
653653
},
654654
border: { color: kbColor, left: 1, right: 1, top: 1, bottom: 1 },

ops.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,15 @@ export function pack(
132132
);
133133
o += 4;
134134

135-
view.setUint32(o, (l.alignX ?? 0) | ((l.alignY ?? 0) << 8), true);
135+
let alignX = l.alignX === "right" ? 1 : l.alignX === "center" ? 2 : 0;
136+
137+
let alignY = l.alignY === "bottom"
138+
? 1
139+
: l.alignY === "center"
140+
? 2
141+
: 0;
142+
143+
view.setUint32(o, alignX | (alignY << 8), true);
136144
o += 4;
137145
}
138146

@@ -283,8 +291,8 @@ export interface OpenElement {
283291
padding?: { left?: number; right?: number; top?: number; bottom?: number };
284292
gap?: number;
285293
direction?: "ltr" | "ttb";
286-
alignX?: number;
287-
alignY?: number;
294+
alignX?: "left" | "center" | "right";
295+
alignY?: "top" | "center" | "bottom";
288296
};
289297
bg?: number;
290298
cornerRadius?: { tl?: number; tr?: number; bl?: number; br?: number };

specs/renderer-spec.md

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -641,9 +641,9 @@ The `open()` constructor currently accepts the following property groups in its
641641
`props` parameter:
642642

643643
- **`layout`** — sizing (width and height, specified via sizing helpers),
644-
padding (per-side), alignment (currently numeric enum values, with a planned
645-
transition to string literals), direction (top-to-bottom or left-to-right),
646-
and gap
644+
padding (per-side), alignment (`alignX`: `"left"` | `"center"` | `"right"`;
645+
`alignY`: `"top"` | `"center"` | `"bottom"`, defaulting to left/top when
646+
omitted), direction (top-to-bottom or left-to-right), and gap
647647
- **`border`** — per-side border widths and border color
648648
- **`cornerRadius`** — per-corner radius values, producing rounded box-drawing
649649
characters
@@ -657,8 +657,7 @@ The `text()` constructor accepts: `color`, `bg`, `fontSize`, `letterSpacing`,
657657
`strikethrough`).
658658

659659
These property groups represent the current implementation surface. New groups
660-
and fields have been added incrementally and more may follow. Alignment values
661-
are expected to transition from numeric to string-literal form.
660+
and fields have been added incrementally and more may follow.
662661

663662
**Border width and layout interaction.** In the underlying layout engine (Clay),
664663
border configuration does not affect layout computation. This is Clay's intended

validate.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,20 @@ const Layout = Type.Object({
5656
direction: Type.Optional(
5757
Type.Union([Type.Literal("ltr"), Type.Literal("ttb")]),
5858
),
59-
alignX: Type.Optional(u8),
60-
alignY: Type.Optional(u8),
59+
alignX: Type.Optional(
60+
Type.Union([
61+
Type.Literal("left"),
62+
Type.Literal("center"),
63+
Type.Literal("right"),
64+
]),
65+
),
66+
alignY: Type.Optional(
67+
Type.Union([
68+
Type.Literal("top"),
69+
Type.Literal("center"),
70+
Type.Literal("bottom"),
71+
]),
72+
),
6173
});
6274

6375
const CornerRadius = Type.Object({

0 commit comments

Comments
 (0)