Skip to content

Commit 7ba7a3f

Browse files
committed
fix: ellipsing nowrap text, cropping of elements in layouts
This commit fixes text not being properly ellipsed when wrapping is disabled. Now the `text/overflow.nt.ts` test passes. Additionally fixes: - issue where layout blocks cutting of parts of their children would render an ellipse - cases where cutting of element would leave out a gap
1 parent 4700769 commit 7ba7a3f

File tree

6 files changed

+26
-14
lines changed

6 files changed

+26
-14
lines changed

deno.lock

Lines changed: 10 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/demo.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ const color = () => crayon.bgHsl(((++h) * 40) % 360, 60, 40);
1212
const style = new Style({
1313
string: crayon.bgMagenta,
1414
text: {
15+
overflow: "ellipsis",
1516
horizontalAlign: "center",
1617
},
1718
padding: { all: 1 },
@@ -50,7 +51,7 @@ export function render() {
5051
{ width: calc("50% - 1"), gap: 2, string: crayon.bgLightBlack, x: "50%" },
5152
style.create(
5253
"Nice 🔥\n(╯°□°)╯︵┻━┻\ndevanagari आआॠऋॲपॉ\nハハハThis text should get wrapped because widthəəə is explicit as日本verylongstringthaəə💩twillwrapnomatterwhat\nwowə\nلعربيةا",
53-
{ string: color() },
54+
{ height: 15, string: color() },
5455
),
5556
new HorizontalBlock(
5657
{ gap: 2, string: color(), y: "50%" },

src/layout/horizontal_block.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ export class HorizontalBlock extends LayoutBlock {
135135
for (let i = 0; i < this.computedHeight; ++i) {
136136
this.lines[i] ??= "";
137137
const line = child.lines[i - offsetY] ?? emptyLine;
138-
this.lines[i] += gapString + cropEnd(line, freeSpace) + "\x1b[0m";
138+
this.lines[i] += gapString + cropEnd(line, freeSpace, " ") + "\x1b[0m";
139139
}
140140
this.#occupiedWidth += freeSpace;
141141
}
@@ -147,7 +147,7 @@ export class HorizontalBlock extends LayoutBlock {
147147
const croppedLineWidth = this.computedWidth + this.computedX;
148148

149149
for (let i = 0; i < this.lines.length; ++i) {
150-
const paddedLine = cropStart(this.lines[i], croppedLineWidth) + padRight;
150+
const paddedLine = cropStart(this.lines[i], croppedLineWidth, " ") + padRight;
151151
this.lines[i] = this.string ? this.string(paddedLine) : paddedLine;
152152
}
153153
return;

src/layout/overlay_block.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,15 +108,16 @@ export class OverlayBlock extends LayoutBlock {
108108
const fgLine = fg.lines[fgLinePos];
109109

110110
if (computedX < 0) {
111-
const line = cropStart(fgLine, fgWidth + computedX) + cropStart(bgLine, bgWidth - fgWidth - computedX);
111+
const line = cropStart(fgLine, fgWidth + computedX, " ") +
112+
cropStart(bgLine, bgWidth - fgWidth - computedX, " ");
112113
this.lines.push(string ? string(line) : line);
113114
} else if (fgWidth === bgWidth) {
114115
this.lines.push(fgLine);
115116
} else {
116117
// TODO: Just inlining insert and using computedWidths instead of recalculating them
117118
// is an easy way to improve perf, but maybe @tui/strings should add a way to
118119
// set widths if they are known instead
119-
const line = insert(bgLine, fgLine, computedX, true);
120+
const line = insert(bgLine, fgLine, computedX, true, " ");
120121

121122
this.lines.push(string ? string(line) : line);
122123
}

src/layout/vertical_block.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ export class VerticalBlock extends LayoutBlock {
127127
const croppedLineWidth = child.computedWidth + computedX;
128128

129129
for (let i = 0; i < childLinesInBounds; ++i) {
130-
const line = cropStart(child.lines[i], croppedLineWidth);
130+
const line = cropStart(child.lines[i], croppedLineWidth, " ");
131131
const paddedLine = line + padRight;
132132
this.lines.push(this.string ? this.string(paddedLine) : paddedLine);
133133
}
@@ -148,7 +148,7 @@ export class VerticalBlock extends LayoutBlock {
148148
} else if (child.computedWidth > this.computedWidth) {
149149
for (let i = 0; i < childLinesInBounds; ++i) {
150150
const line = child.lines[i];
151-
const croppedLine = cropEnd(line, this.computedWidth);
151+
const croppedLine = cropEnd(line, this.computedWidth, " ");
152152
this.lines.push(this.string ? this.string(croppedLine) : croppedLine);
153153
}
154154
} else {

src/text/resize_horizontal.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,17 @@ export function resizeLineHorizontally(
1919
{ ellipsisString, overflow }: NormalizedTextDefinition,
2020
): string {
2121
const lineWidth = textWidth(line);
22-
if (lineWidth < desiredWidth) {
22+
if (lineWidth <= desiredWidth) {
2323
return line;
2424
}
2525

2626
switch (overflow) {
2727
case "clip":
28-
return cropEnd(line, desiredWidth, "");
29-
case "ellipsis":
30-
return cropEnd(line, desiredWidth, ellipsisString);
28+
return cropEnd(line, desiredWidth, " ");
29+
case "ellipsis": {
30+
const ellipsisWidth = textWidth(ellipsisString);
31+
const cropped = cropEnd(line, desiredWidth, ellipsisString);
32+
return cropped.slice(0, -ellipsisWidth) + ellipsisString;
33+
}
3134
}
3235
}

0 commit comments

Comments
 (0)