@visulima/ansi 3.0.2 → 3.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,96 @@
1
+ import type { IITerm2Payload, ITerm2FileProperties } from "./iterm2-properties.d.ts";
2
+ /**
3
+ * Represents the payload for a complete iTerm2 file transfer or an inline image display command.
4
+ * This class is used to construct the part of the OSC 1337 sequence that follows `File=`.
5
+ * The generated payload can be either:
6
+ * - `File=[PROPERTIES]:[BASE64_CONTENT]` (for inline content)
7
+ * - `File=[PROPERTIES]` (if content is not provided directly, e.g., for a download announcement)
8
+ *
9
+ * Implements {@link IITerm2Payload} for use with the generic `iTerm2` function.
10
+ * @see {@link ITerm2FileProps} for property details.
11
+ * @see {@link iTerm2} for the function that wraps this payload into a full escape sequence.
12
+ */
13
+ export declare class ITerm2File implements IITerm2Payload {
14
+ private readonly fileProps;
15
+ /**
16
+ * Constructs an `ITerm2File` payload object.
17
+ * @param properties An object containing properties for the file/image, as defined by {@link ITerm2FileProps}.
18
+ * The `name` property within `props` should be pre-Base64 encoded by the caller if it might
19
+ * contain special characters (like `;`, `=`, or non-ASCII characters).
20
+ * If `fileData` is provided, `props.content` will be overridden, and `props.size` will be
21
+ * set from `fileData.byteLength` if not already present in `props`.
22
+ * @param fileData (Optional) A `Uint8Array` containing the raw file data. If provided, this data will be
23
+ * Base64 encoded and used as the `content` of the file transfer. The `size` property
24
+ * will also be automatically set from `fileData.byteLength` if not specified in `props`.
25
+ */
26
+ constructor(properties: ITerm2FileProperties, fileData?: Uint8Array);
27
+ /**
28
+ * Converts the file properties and its content (if any) into the string payload
29
+ * suitable for the iTerm2 `File=` command.
30
+ * @returns The string payload (e.g., `"File=name=...;size=...:BASE64_CONTENT"` or `"File=name=...;size=..."`).
31
+ */
32
+ toString(): string;
33
+ }
34
+ /**
35
+ * Represents the payload for ending an iTerm2 multipart file transfer.
36
+ * This class is used to construct the part of the OSC 1337 sequence that is simply `FileEnd`.
37
+ *
38
+ * Implements {@link IITerm2Payload} for use with the generic `iTerm2` function.
39
+ * @see {@link ITerm2MultipartFileStart} to initiate the transfer.
40
+ * @see {@link ITerm2FilePart} for sending file chunks.
41
+ */
42
+ export declare class ITerm2FileEnd implements IITerm2Payload {
43
+ /**
44
+ * Generates the string payload for the iTerm2 `FileEnd` command.
45
+ * @returns The string `"FileEnd"`.
46
+ */
47
+ toString(): string;
48
+ }
49
+ /**
50
+ * Represents the payload for a part (chunk) of an iTerm2 multipart file transfer.
51
+ * This class is used to construct the part of the OSC 1337 sequence that follows `FilePart=`.
52
+ * The provided chunk must already be Base64 encoded.
53
+ *
54
+ * Implements {@link IITerm2Payload} for use with the generic `iTerm2` function.
55
+ * @see {@link ITerm2MultipartFileStart} to initiate the transfer.
56
+ * @see {@link ITerm2FileEnd} to finalize the transfer.
57
+ */
58
+ export declare class ITerm2FilePart implements IITerm2Payload {
59
+ private readonly base64Chunk;
60
+ /**
61
+ * Constructs an `ITerm2FilePart` payload object.
62
+ * @param base64Chunk A string containing a Base64 encoded chunk of the file data.
63
+ * The caller is responsible for chunking the file and Base64 encoding each chunk.
64
+ */
65
+ constructor(base64Chunk: string);
66
+ /**
67
+ * Converts the Base64 encoded chunk into the string payload suitable for the iTerm2 `FilePart=` command.
68
+ * @returns The string payload (e.g., `"FilePart=U09NRURBVEE="`).
69
+ */
70
+ toString(): string;
71
+ }
72
+ /**
73
+ * Represents the payload for starting an iTerm2 multipart file transfer.
74
+ * This class is used to construct the part of the OSC 1337 sequence that follows `MultipartFile=`.
75
+ * This command initiates a transfer; the actual file data is sent in subsequent `FilePart` commands.
76
+ *
77
+ * Implements {@link IITerm2Payload} for use with the generic `iTerm2` function.
78
+ * @see {@link ITerm2FileProps} for property details (omitting `content`).
79
+ * @see {@link ITerm2FilePart} for sending file chunks.
80
+ * @see {@link ITerm2FileEnd} for finalizing the transfer.
81
+ */
82
+ export declare class ITerm2MultipartFileStart implements IITerm2Payload {
83
+ private readonly properties;
84
+ /**
85
+ * Constructs an `ITerm2MultipartFileStart` payload object.
86
+ * @param properties Properties for the multipart file (e.g., `name`, `size`). Content is not part of this command.
87
+ * The `name` property within `props` should be pre-Base64 encoded by the caller if it might
88
+ * contain special characters.
89
+ */
90
+ constructor(properties: Omit<ITerm2FileProperties, "content">);
91
+ /**
92
+ * Converts the file properties into the string payload suitable for the iTerm2 `MultipartFile=` command.
93
+ * @returns The string payload (e.g., `"MultipartFile=name=...;size=..."`).
94
+ */
95
+ toString(): string;
96
+ }
package/dist/iterm2.d.ts CHANGED
@@ -1,43 +1,58 @@
1
- import { LiteralUnion } from 'type-fest';
2
-
3
- declare const IT2_AUTO: string;
4
- declare const it2Cells: (n: number) => string;
5
- declare const it2Pixels: (n: number) => string;
6
- declare const it2Percent: (n: number) => string;
7
- interface IITerm2Payload {
8
- toString: () => string;
9
- }
10
- interface ITerm2FileProperties {
11
- content?: string;
12
- doNotMoveCursor?: boolean;
13
- height?: LiteralUnion<typeof IT2_AUTO, number | string>;
14
- ignoreAspectRatio?: boolean;
15
- inline?: boolean;
16
- name?: string;
17
- size?: number;
18
- width?: LiteralUnion<typeof IT2_AUTO, number | string>;
19
- }
20
-
21
- declare class ITerm2File implements IITerm2Payload {
22
- private readonly fileProps;
23
- constructor(properties: ITerm2FileProperties, fileData?: Uint8Array);
24
- toString(): string;
25
- }
26
- declare class ITerm2FileEnd implements IITerm2Payload {
27
- toString(): string;
28
- }
29
- declare class ITerm2FilePart implements IITerm2Payload {
30
- private readonly base64Chunk;
31
- constructor(base64Chunk: string);
32
- toString(): string;
33
- }
34
- declare class ITerm2MultipartFileStart implements IITerm2Payload {
35
- private readonly properties;
36
- constructor(properties: Omit<ITerm2FileProperties, "content">);
37
- toString(): string;
38
- }
39
-
40
- declare const iTerm2: (payload: IITerm2Payload) => string;
41
-
42
- export { IT2_AUTO, ITerm2File, ITerm2FileEnd, ITerm2FilePart, ITerm2MultipartFileStart, iTerm2, it2Cells, it2Percent, it2Pixels };
43
- export type { IITerm2Payload, ITerm2FileProperties };
1
+ import type { IITerm2Payload } from './iterm2/iterm2-properties.d.ts';
2
+ export type { IITerm2Payload, ITerm2FileProperties } from './iterm2/iterm2-properties.d.ts';
3
+ export { IT2_AUTO, it2Cells, it2Percent, it2Pixels } from './iterm2/iterm2-properties.d.ts';
4
+ export { ITerm2File, ITerm2FileEnd, ITerm2FilePart, ITerm2MultipartFileStart } from './iterm2/iterm2-sequences.d.ts';
5
+ /**
6
+ * Generates a complete iTerm2 proprietary escape sequence (OSC 1337).
7
+ *
8
+ * This function serves as a general-purpose constructor for iTerm2 escape codes.
9
+ * It takes a payload object that conforms to the {@link IITerm2Payload} interface.
10
+ * The `toString()` method of this payload object is responsible for generating the
11
+ * specific command and arguments part of the sequence (e.g., `File=...`, `ShellIntegrationVersion=...`).
12
+ *
13
+ * The overall structure of the generated sequence is: `OSC 1337 ; &lt;PAYLOAD_STRING> BEL`
14
+ * (`OSC` is `\x1b]`, `BEL` is `\x07`).
15
+ * @param payload An object that implements the {@link IITerm2Payload} interface.
16
+ * This object must have a `toString()` method that returns the string representation
17
+ * of the iTerm2 command-specific payload.
18
+ * Examples include instances of `ITerm2File`, `ITerm2MultipartFileStart`, etc.
19
+ * @returns The fully formed ANSI escape sequence for the iTerm2 command.
20
+ * Returns an empty string if the provided `payload` is invalid (e.g., null, undefined,
21
+ * lacks a proper `toString` method, or its `toString` method is the generic `Object.prototype.toString`).
22
+ * @see {@link https://iterm2.com/documentation-escape-codes.html iTerm2 Escape Codes Documentation}
23
+ * for a comprehensive list of supported commands and their payloads.
24
+ * @see {@link IITerm2Payload} for the interface requirement.
25
+ * @see Classes like {@link ITerm2File}, {@link ITerm2MultipartFileStart}, {@link ITerm2FilePart}, {@link ITerm2FileEnd}
26
+ * for concrete examples of payload objects.
27
+ * @example
28
+ * ```typescript
29
+ * import { iTerm2, ITerm2File, ITerm2FileProps } from '@visulima/ansi/iterm2'; // ITerm2FileProps can be used for options
30
+ * import { Buffer } from 'node:buffer';
31
+ *
32
+ * // Example 1: Sending a file inline (like an image)
33
+ * const imageName = "my_image.png";
34
+ * const imageData = Buffer.from("dummyimagecontent"); // Replace with actual Uint8Array image data
35
+ * const imageFileProps: ITerm2FileProps = { // Use ITerm2FileProps for broader options
36
+ * name: Buffer.from(imageName).toString("base64"), // Name should be base64 encoded
37
+ * inline: true,
38
+ * width: "50%",
39
+ * height: "auto",
40
+ * ignoreAspectRatio: false, // Equivalent to preserveAspectRatio: true
41
+ * };
42
+ * const filePayload = new ITerm2File(imageFileProps, imageData);
43
+ * const imageSequence = iTerm2(filePayload);
44
+ * console.log(imageSequence);
45
+ * // Expected output (simplified, actual base64 will be longer):
46
+ * // OSC1337;File=name=bXlfaW1hZ2UucG5n;inline=1;width=50%;height=auto:ZHVtbXlpbWFnZWNvbnRlbnQ=BEL
47
+ * // Note: if ignoreAspectRatio was true, preserveAspectRatio=0 would be in the sequence.
48
+ *
49
+ * // Example 2: A hypothetical simple command (e.g., shell integration handshake)
50
+ * const shellIntegrationPayload: IITerm2Payload = {
51
+ * toString: () => "ShellIntegrationVersion=15;Shell=zsh"
52
+ * };
53
+ * const shellSequence = iTerm2(shellIntegrationPayload);
54
+ * console.log(shellSequence);
55
+ * // Output: OSC1337;ShellIntegrationVersion=15;Shell=zshBEL
56
+ * ```
57
+ */
58
+ export declare const iTerm2: (payload: IITerm2Payload) => string;