UNPKG

trigger.dev

Version:

A Command-Line Interface for Trigger.dev projects

98 lines 3.39 kB
import { log, spinner as clackSpinner } from "@clack/prompts"; import { isWindows as stdEnvIsWindows } from "std-env"; export const isWindows = stdEnvIsWindows; export function escapeImportPath(path) { return isWindows ? path.replaceAll("\\", "\\\\") : path; } // Removes ANSI escape sequences to get actual visible length function getVisibleLength(str) { return (str // Remove terminal hyperlinks: \u001b]8;;URL\u0007TEXT\u001b]8;;\u0007 .replace(/\u001b]8;;[^\u0007]*\u0007/g, "") // Remove standard ANSI escape sequences (colors, cursor movement, etc.) .replace(/\x1b\[[0-9;]*[a-zA-Z]/g, "").length); } function truncateMessage(msg, maxLength) { const terminalWidth = maxLength ?? process.stdout.columns ?? 80; const availableWidth = terminalWidth - 5; // Reserve some space for the spinner and padding const visibleLength = getVisibleLength(msg); if (visibleLength <= availableWidth) { return msg; } // We need to truncate based on visible characters, but preserve ANSI sequences // Simple approach: truncate character by character until we fit let truncated = msg; while (getVisibleLength(truncated) > availableWidth - 3) { truncated = truncated.slice(0, -1); } return truncated + "..."; } const wrappedClackSpinner = () => { let currentMessage = ""; let isActive = false; const handleResize = () => { if (isActive && currentMessage) { spinner.message(truncateMessage(currentMessage)); } }; const spinner = clackSpinner(); return { start: (msg) => { currentMessage = msg ?? ""; isActive = true; process.stdout.on("resize", handleResize); spinner.start(truncateMessage(currentMessage)); }, stop: (msg, code) => { process.stdout.off("resize", handleResize); if (!isActive) { // Spinner was never started, just display the message if (msg) { log.message(msg); } return; } isActive = false; spinner.stop(truncateMessage(msg ?? ""), code); }, message: (msg) => { currentMessage = msg ?? ""; if (!isActive) { // Spinner was never started, just display the message if (msg) { log.message(msg); } return; } spinner.message(truncateMessage(currentMessage)); }, }; }; const ballmerSpinner = () => ({ start: (msg) => { log.step(msg ?? ""); }, stop: (msg, code) => { log.message(msg ?? ""); }, message: (msg) => { log.message(msg ?? ""); }, }); const plainSpinner = () => ({ start: (msg) => { console.log(msg ?? ""); }, stop: (msg, code) => { if (msg) console.log(msg ?? ""); }, message: (msg) => { if (msg) console.log(msg ?? ""); }, }); // This will become unecessary with the next clack release, the bug was fixed here: // https://github.com/natemoo-re/clack/pull/182 export const spinner = (options = { plain: false }) => options.plain ? plainSpinner() : isWindows ? ballmerSpinner() : wrappedClackSpinner(); //# sourceMappingURL=windows.js.map