UNPKG

@visulima/ansi

Version:

ANSI escape codes for some terminal swag.

131 lines (128 loc) 4.57 kB
import { Buffer } from 'node:buffer'; var __defProp = Object.defineProperty; var __name = (target, value) => __defProp(target, "name", { value, configurable: true }); const formatITerm2FileProperties = /* @__PURE__ */ __name((properties) => { const options = []; if (properties.name) { options.push(`name=${properties.name}`); } if (properties.size !== void 0) { options.push(`size=${properties.size}`); } if (properties.width !== void 0) { options.push(`width=${properties.width.toString()}`); } if (properties.height !== void 0) { options.push(`height=${properties.height.toString()}`); } if (properties.ignoreAspectRatio) { options.push("preserveAspectRatio=0"); } if (properties.inline) { options.push("inline=1"); } if (properties.doNotMoveCursor) { options.push("doNotMoveCursor=1"); } return options.join(";"); }, "formatITerm2FileProperties"); class ITerm2File { static { __name(this, "ITerm2File"); } fileProps; /** * Constructs an `ITerm2File` payload object. * @param properties An object containing properties for the file/image, as defined by {@link ITerm2FileProps}. * The `name` property within `props` should be pre-Base64 encoded by the caller if it might * contain special characters (like `;`, `=`, or non-ASCII characters). * If `fileData` is provided, `props.content` will be overridden, and `props.size` will be * set from `fileData.byteLength` if not already present in `props`. * @param fileData (Optional) A `Uint8Array` containing the raw file data. If provided, this data will be * Base64 encoded and used as the `content` of the file transfer. The `size` property * will also be automatically set from `fileData.byteLength` if not specified in `props`. */ constructor(properties, fileData) { this.fileProps = { ...properties }; if (fileData) { if (fileData.byteLength > 10 * 1024 * 1024) { throw new Error("File size exceeds maximum limit of 10MB"); } this.fileProps.content = Buffer.from(fileData).toString("base64"); if (this.fileProps.size === void 0) { this.fileProps.size = fileData.byteLength; } if (this.fileProps.size !== fileData.byteLength) { throw new Error("File size property doesn't match actual data length"); } } } /** * Converts the file properties and its content (if any) into the string payload * suitable for the iTerm2 `File=` command. * @returns The string payload (e.g., `"File=name=...;size=...:BASE64_CONTENT"` or `"File=name=...;size=..."`). */ toString() { let s = "File="; s += formatITerm2FileProperties(this.fileProps); if (this.fileProps.content !== void 0) { s += `:${this.fileProps.content}`; } return s; } } class ITerm2FileEnd { static { __name(this, "ITerm2FileEnd"); } /** * Generates the string payload for the iTerm2 `FileEnd` command. * @returns The string `"FileEnd"`. */ // eslint-disable-next-line class-methods-use-this toString() { return "FileEnd"; } } class ITerm2FilePart { /** * Constructs an `ITerm2FilePart` payload object. * @param base64Chunk A string containing a Base64 encoded chunk of the file data. * The caller is responsible for chunking the file and Base64 encoding each chunk. */ constructor(base64Chunk) { this.base64Chunk = base64Chunk; } static { __name(this, "ITerm2FilePart"); } /** * Converts the Base64 encoded chunk into the string payload suitable for the iTerm2 `FilePart=` command. * @returns The string payload (e.g., `"FilePart=U09NRURBVEE="`). */ toString() { return `FilePart=${this.base64Chunk}`; } } class ITerm2MultipartFileStart { /** * Constructs an `ITerm2MultipartFileStart` payload object. * @param properties Properties for the multipart file (e.g., `name`, `size`). Content is not part of this command. * The `name` property within `props` should be pre-Base64 encoded by the caller if it might * contain special characters. */ constructor(properties) { this.properties = properties; } static { __name(this, "ITerm2MultipartFileStart"); } /** * Converts the file properties into the string payload suitable for the iTerm2 `MultipartFile=` command. * @returns The string payload (e.g., `"MultipartFile=name=...;size=..."`). */ toString() { return `MultipartFile=${formatITerm2FileProperties(this.properties)}`; } } export { ITerm2File, ITerm2FileEnd, ITerm2FilePart, ITerm2MultipartFileStart };