Refactor to remove some duplicated functiosn

This commit is contained in:
James Babcock committed 2026-07-26 11:33:16 -07:00
1 parent 797682a386
commit 205d393415
17 files changed
+57 -223

No files matched your search

+2 -27
View File
@@ -3,6 +3,7 @@ import { getDefaultSmallFont, type BdfFont } from "../../graphics/bdffont";
import { GESTURE_DOUBLE_CLICK } from "../gestures";
import { drawSelectionHighlight } from "../menu";
import { Layer, type DashboardInputEvent, type LayerActions, type LayerContext } from "../layers";
import { wrapText, truncateText } from "../../graphics/textwrap";
const DIALOG_X = 40;
const DIALOG_Y = 40;
@@ -90,7 +91,7 @@ export class AssistantLayer implements Layer {
image.drawText(font, left, DIALOG_Y + 12, this.phase === "thinking" ? "Assistant ●" : "Assistant", 220);
if (this.status) {
const statusValue = this.phase === "error" ? 200 : 130;
image.drawText(font, left, DIALOG_Y + 30, truncate(font, this.status, TEXT_MAX_WIDTH), statusValue);
image.drawText(font, left, DIALOG_Y + 30, truncateText(font, this.status, TEXT_MAX_WIDTH), statusValue);
}
const inMenu = this.phase !== "thinking";
@@ -162,29 +163,3 @@ export class AssistantLayer implements Layer {
this.callbacks.onRemoved?.();
}
}
function wrapText(font: BdfFont, text: string, maxWidth: number): string[] {
const words = text.split(/\s+/).filter(Boolean);
const lines: string[] = [];
let line = "";
for (const word of words) {
const candidate = line ? `${line} ${word}` : word;
if (line && font.measureText(candidate) > maxWidth) {
lines.push(line);
line = word;
} else {
line = candidate;
}
}
if (line) lines.push(line);
return lines.length ? lines : [""];
}
function truncate(font: BdfFont, text: string, maxWidth: number): string {
if (font.measureText(text) <= maxWidth) return text;
let out = text;
while (out.length > 1 && font.measureText(`${out}...`) > maxWidth) {
out = out.slice(0, -1);
}
return `${out}...`;
}
+2 -11
View File
@@ -1,6 +1,6 @@
import { getDefaultSmallFont, type BdfFont } from "../../graphics/bdffont";
import { GrayImage } from "../../graphics/image";
import { wrapText } from "../../graphics/textwrap";
import { wrapText, truncateText } from "../../graphics/textwrap";
import { clamp } from "../../util/numeric-util";
import { readUpcomingEvents, type CalendarEvent } from "../../native/calendar";
import { timeFormatSetting } from "../dashboard-settings";
@@ -147,7 +147,7 @@ function drawEventRow(
const maxTextWidth = width - 2 * x;
for (let index = 0; index < row.lines.length; index++) {
const value = index === 0 ? (selected ? 235 : 205) : 160;
image.drawText(font, x, cursorY + 3 + index * LINE_HEIGHT, truncateToWidth(font, row.lines[index]!, maxTextWidth), value);
image.drawText(font, x, cursorY + 3 + index * LINE_HEIGHT, truncateText(font, row.lines[index]!, maxTextWidth), value);
}
}
@@ -189,12 +189,3 @@ function formatEventTime(timestampMs: number): string {
}
return `${String(hour24).padStart(2, "0")}:${minutes}`;
}
function truncateToWidth(font: BdfFont, text: string, width: number): string {
if (font.measureText(text) <= width) return text;
let out = text;
while (out.length > 1 && font.measureText(`${out}...`) > width) {
out = out.slice(0, -1);
}
return `${out}...`;
}
+4 -21
View File
@@ -1,4 +1,5 @@
import { getDefaultSmallFont, type BdfFont } from "../../graphics/bdffont";
import { truncateText, truncateLeft } from "../../graphics/textwrap";
import { GrayImage } from "../../graphics/image";
import { renderIcon, type IconName } from "../../graphics/icons";
import { clamp } from "../../util/numeric-util";
@@ -163,7 +164,7 @@ export class FileBrowserLayer implements Layer {
drawSelectionHighlight(image, LIST_X - 6, y - 1, width - 2 * LIST_X + 12, ROW_HEIGHT - 1, ctx.stack.isFocused(), 4);
}
const value = itemValue(row, selected);
image.drawText(font, LIST_X, y + 1, truncateRight(font, row.label, width - 2 * LIST_X), value);
image.drawText(font, LIST_X, y + 1, truncateText(font, row.label, width - 2 * LIST_X), value);
}
image.drawText(font, 20, height - 16, `${GESTURE_CLICK} open ${GESTURE_DOUBLE_CLICK} up / back`, 110);
@@ -192,7 +193,7 @@ export class FileBrowserLayer implements Layer {
if (selected) {
drawSelectionHighlight(image, LIST_X - 6, textY - 2, width - 2 * LIST_X + 12, font.lineHeight + 4, focused, 4);
}
image.drawText(font, LIST_X, textY, truncateRight(font, row.item.label, width - 2 * LIST_X), itemValue(row.item, selected));
image.drawText(font, LIST_X, textY, truncateText(font, row.item.label, width - 2 * LIST_X), itemValue(row.item, selected));
continue;
}
@@ -215,7 +216,7 @@ export class FileBrowserLayer implements Layer {
transparentZero: true,
});
}
const label = truncateRight(font, item.label, colW - 8);
const label = truncateText(font, item.label, colW - 8);
const labelY = Math.round(blockTop + ICON_SIZE + LABEL_GAP);
image.drawText(font, Math.round(centerX - font.measureText(label) / 2), labelY, label, item.supported ? 210 : 100);
}
@@ -573,21 +574,3 @@ function basename(path: string): string {
function truncateChars(text: string, maxLength: number): string {
return text.length <= maxLength ? text : `${text.slice(0, Math.max(0, maxLength - 3))}...`;
}
function truncateRight(font: BdfFont, text: string, maxWidth: number): string {
if (font.measureText(text) <= maxWidth) return text;
let out = text;
while (out.length > 1 && font.measureText(`${out}...`) > maxWidth) {
out = out.slice(0, -1);
}
return `${out}...`;
}
function truncateLeft(font: BdfFont, text: string, maxWidth: number): string {
if (font.measureText(text) <= maxWidth) return text;
let out = text;
while (out.length > 1 && font.measureText(`...${out}`) > maxWidth) {
out = out.slice(1);
}
return `...${out}`;
}
+2 -10
View File
@@ -1,4 +1,5 @@
import { getDefaultSmallFont, type BdfFont } from "../../graphics/bdffont";
import { truncateText } from "../../graphics/textwrap";
import { GrayImage } from "../../graphics/image";
import { loadImageFileAsGray } from "../../native/image-files";
import { GESTURE_DOUBLE_CLICK } from "../gestures";
@@ -28,7 +29,7 @@ export class ImageViewerLayer implements Layer {
const font = getDefaultSmallFont();
const { width, height } = ctx.stack.getBaseSize();
const out = new GrayImage(width, height, 0);
out.drawText(font, MARGIN_X, TITLE_Y, truncateToWidth(font, this.title, width - 2 * MARGIN_X), 220);
out.drawText(font, MARGIN_X, TITLE_Y, truncateText(font, this.title, width - 2 * MARGIN_X), 220);
const bodyHeight = height - BODY_TOP - BODY_MARGIN;
if (!this.loadAttempted) {
@@ -54,12 +55,3 @@ export class ImageViewerLayer implements Layer {
}
}
}
function truncateToWidth(font: BdfFont, text: string, maxWidth: number): string {
if (font.measureText(text) <= maxWidth) return text;
let out = text;
while (out.length > 1 && font.measureText(`${out}...`) > maxWidth) {
out = out.slice(0, -1);
}
return `${out}...`;
}
+2 -10
View File
@@ -1,4 +1,4 @@
import { wrapText } from "../../graphics/textwrap";
import { wrapText, truncateText } from "../../graphics/textwrap";
import { getDefaultSmallFont, type BdfFont } from "../../graphics/bdffont";
import { GrayImage } from "../../graphics/image";
import { Layer, type DashboardInputEvent, type LayerContext } from "../layers";
@@ -31,7 +31,7 @@ export class TextViewerLayer implements Layer {
const image = new GrayImage(width, height, 0);
const footerY = height - FOOTER_MARGIN;
this.bodyLineCount = Math.max(1, Math.floor((footerY - BODY_Y) / LINE_STEP));
image.drawText(font, MARGIN_X + 4, TITLE_Y, truncateToWidth(font, this.title, width - 2 * MARGIN_X - 8), 220);
image.drawText(font, MARGIN_X + 4, TITLE_Y, truncateText(font, this.title, width - 2 * MARGIN_X - 8), 220);
const lines = this.getLines(font, width);
const visibleLines = lines.slice(this.firstLine, this.firstLine + this.bodyLineCount);
@@ -94,11 +94,3 @@ export class TextViewerLayer implements Layer {
}
}
function truncateToWidth(font: BdfFont, text: string, maxWidth: number): string {
if (font.measureText(text) <= maxWidth) return text;
let out = text;
while (out.length > 1 && font.measureText(`${out}...`) > maxWidth) {
out = out.slice(0, -1);
}
return `${out}...`;
}
+2 -27
View File
@@ -1,4 +1,5 @@
import { G2_LENS_HEIGHT, G2_LENS_WIDTH, GrayImage } from "../../graphics/image";
import { wrapText, truncateText } from "../../graphics/textwrap";
import { getDefaultSmallFont, type BdfFont } from "../../graphics/bdffont";
import { voiceControlBridge, type VoiceTranscriptEvent } from "../../native/voice-control";
import { refineDictation, type AnthropicStreamHandle } from "../../native/anthropic";
@@ -228,7 +229,7 @@ export class VoiceInputLayer implements Layer {
const left = DIALOG_X + 16;
image.drawText(font, left, DIALOG_Y + 12, this.capturing ? "Voice ●" : "Voice", 220);
image.drawText(font, left, DIALOG_Y + 30, truncate(font, this.status, TEXT_MAX_WIDTH), 130);
image.drawText(font, left, DIALOG_Y + 30, truncateText(font, this.status, TEXT_MAX_WIDTH), 130);
const inMenu = this.phase === "menu";
const rows = inMenu ? this.menuRows() : [];
@@ -480,29 +481,3 @@ export class VoiceInputLayer implements Layer {
this.actions.requestRender();
}
}
function wrapText(font: BdfFont, text: string, maxWidth: number): string[] {
const words = text.split(/\s+/).filter(Boolean);
const lines: string[] = [];
let line = "";
for (const word of words) {
const candidate = line ? `${line} ${word}` : word;
if (line && font.measureText(candidate) > maxWidth) {
lines.push(line);
line = word;
} else {
line = candidate;
}
}
if (line) lines.push(line);
return lines.length ? lines : [""];
}
function truncate(font: BdfFont, text: string, maxWidth: number): string {
if (font.measureText(text) <= maxWidth) return text;
let out = text;
while (out.length > 1 && font.measureText(`${out}...`) > maxWidth) {
out = out.slice(0, -1);
}
return `${out}...`;
}
+6 -12
View File
@@ -1,6 +1,6 @@
import { getDefaultLargeFont, getDefaultMediumFont, getDefaultSmallFont, type BdfFont } from "../../graphics/bdffont";
import { GrayImage } from "../../graphics/image";
import { wrapText } from "../../graphics/textwrap";
import { wrapText, truncateText } from "../../graphics/textwrap";
import { clamp } from "../../util/numeric-util";
import { type ForecastPeriod, type WeatherState } from "../../native/weather";
import { GESTURE_CLICK, GESTURE_DOUBLE_CLICK, GESTURE_SCROLL } from "../gestures";
@@ -39,9 +39,9 @@ export class WeatherLayer implements Layer {
if (weather.locationName) {
image.drawText(
font,
width - PAGE_X - font.measureText(truncate(font, weather.locationName, 220)),
width - PAGE_X - font.measureText(truncateText(font, weather.locationName, 220)),
HEADER_Y,
truncate(font, weather.locationName, 220),
truncateText(font, weather.locationName, 220),
150,
);
}
@@ -104,7 +104,7 @@ export class WeatherLayer implements Layer {
medium,
CURRENT_TEXT_X,
CURRENT_TOP + 5,
truncate(medium, current.description || "Current conditions", width - CURRENT_TEXT_X - PAGE_X),
truncateText(medium, current.description || "Current conditions", width - CURRENT_TEXT_X - PAGE_X),
220,
);
@@ -150,10 +150,10 @@ export class WeatherLayer implements Layer {
drawSelectionHighlight(image, PAGE_X - 7, y - 2, width - 2 * (PAGE_X - 7), FORECAST_ROW_HEIGHT - 2, ctx.stack.isFocused(), 5);
}
const shade = selected ? 245 : 190;
image.drawText(font, PAGE_X, y + 2, truncate(font, period.name, FORECAST_NAME_WIDTH), shade);
image.drawText(font, PAGE_X, y + 2, truncateText(font, period.name, FORECAST_NAME_WIDTH), shade);
image.drawText(font, FORECAST_TEMP_X, y + 2, period.temperatureF === null ? "--" : `${Math.round(period.temperatureF)}°F`, shade);
image.drawText(font, FORECAST_PRECIP_X, y + 2, period.precipitationPercent === null ? "--" : `${Math.round(period.precipitationPercent)}%`, selected ? 220 : 155);
image.drawText(font, FORECAST_SUMMARY_X, y + 2, truncate(font, period.shortForecast, width - FORECAST_SUMMARY_X - PAGE_X), selected ? 220 : 165);
image.drawText(font, FORECAST_SUMMARY_X, y + 2, truncateText(font, period.shortForecast, width - FORECAST_SUMMARY_X - PAGE_X), selected ? 220 : 165);
}
image.drawText(
@@ -189,9 +189,3 @@ function formatAge(ageMs: number): string {
return `${Math.round(minutes / 60)}h ago`;
}
function truncate(font: BdfFont, text: string, maxWidth: number): string {
if (font.measureText(text) <= maxWidth) return text;
let out = text;
while (out.length > 1 && font.measureText(`${out}...`) > maxWidth) out = out.slice(0, -1);
return `${out}...`;
}