diff options
| author | Abhi <[email protected]> | 2025-06-15 22:09:30 -0400 |
|---|---|---|
| committer | GitHub <[email protected]> | 2025-06-15 22:09:30 -0400 |
| commit | bedff2ca7969cd7d4a6b7dca8c6cc0805b357356 (patch) | |
| tree | c0369a7e1088324173bbe27544c4f6b7815d93fd /packages/cli/src/ui/utils/textUtils.ts | |
| parent | 7f06ad40c562c22fa173855e934b8141b67fd92c (diff) | |
feat: Adds shell command context to gemini history (#1076)
Diffstat (limited to 'packages/cli/src/ui/utils/textUtils.ts')
| -rw-r--r-- | packages/cli/src/ui/utils/textUtils.ts | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/packages/cli/src/ui/utils/textUtils.ts b/packages/cli/src/ui/utils/textUtils.ts index 12852865..35e4c4a2 100644 --- a/packages/cli/src/ui/utils/textUtils.ts +++ b/packages/cli/src/ui/utils/textUtils.ts @@ -16,3 +16,32 @@ export const getAsciiArtWidth = (asciiArt: string): number => { const lines = asciiArt.split('\n'); return Math.max(...lines.map((line) => line.length)); }; + +/** + * Checks if a Buffer is likely binary by testing for the presence of a NULL byte. + * The presence of a NULL byte is a strong indicator that the data is not plain text. + * @param data The Buffer to check. + * @param sampleSize The number of bytes from the start of the buffer to test. + * @returns True if a NULL byte is found, false otherwise. + */ +export function isBinary( + data: Buffer | null | undefined, + sampleSize = 512, +): boolean { + if (!data) { + return false; + } + + const sample = data.length > sampleSize ? data.subarray(0, sampleSize) : data; + + for (let i = 0; i < sample.length; i++) { + // The presence of a NULL byte (0x00) is one of the most reliable + // indicators of a binary file. Text files should not contain them. + if (sample[i] === 0) { + return true; + } + } + + // If no NULL bytes were found in the sample, we assume it's text. + return false; +} |
