mirror of
https://github.com/google-gemini/gemini-cli.git
synced 2026-04-13 14:50:39 -07:00
fix(cli): Add delimiter before printing tool response in non-interactive mode (#11351)
This commit is contained in:
@@ -0,0 +1,23 @@
|
||||
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
|
||||
|
||||
exports[`TextOutput > should correctly handle ANSI escape codes when determining line breaks 1`] = `
|
||||
"[34mhello[39m
|
||||
[1mworld[22m[34m
|
||||
[39mnext"
|
||||
`;
|
||||
|
||||
exports[`TextOutput > should handle ANSI codes that do not end with a newline 1`] = `
|
||||
"hello[34m
|
||||
world"
|
||||
`;
|
||||
|
||||
exports[`TextOutput > should handle a sequence of calls correctly 1`] = `
|
||||
"first
|
||||
second part
|
||||
third"
|
||||
`;
|
||||
|
||||
exports[`TextOutput > should handle empty strings with ANSI codes 1`] = `
|
||||
"hello[34m[39m
|
||||
world"
|
||||
`;
|
||||
99
packages/cli/src/ui/utils/textOutput.test.ts
Normal file
99
packages/cli/src/ui/utils/textOutput.test.ts
Normal file
@@ -0,0 +1,99 @@
|
||||
/**
|
||||
* @license
|
||||
* Copyright 2025 Google LLC
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
/// <reference types="vitest/globals" />
|
||||
|
||||
import type { MockInstance } from 'vitest';
|
||||
import { vi } from 'vitest';
|
||||
import { TextOutput } from './textOutput.js';
|
||||
|
||||
describe('TextOutput', () => {
|
||||
let stdoutSpy: MockInstance<typeof process.stdout.write>;
|
||||
let textOutput: TextOutput;
|
||||
|
||||
beforeEach(() => {
|
||||
stdoutSpy = vi
|
||||
.spyOn(process.stdout, 'write')
|
||||
.mockImplementation(() => true);
|
||||
textOutput = new TextOutput();
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
stdoutSpy.mockRestore();
|
||||
});
|
||||
|
||||
const getWrittenOutput = () => stdoutSpy.mock.calls.map((c) => c[0]).join('');
|
||||
|
||||
it('write() should call process.stdout.write', () => {
|
||||
textOutput.write('hello');
|
||||
expect(stdoutSpy).toHaveBeenCalledWith('hello');
|
||||
});
|
||||
|
||||
it('write() should not call process.stdout.write for empty strings', () => {
|
||||
textOutput.write('');
|
||||
expect(stdoutSpy).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('writeOnNewLine() should not add a newline if the last char was a newline', () => {
|
||||
// Default state starts at the beginning of a line
|
||||
textOutput.writeOnNewLine('hello');
|
||||
expect(getWrittenOutput()).toBe('hello');
|
||||
});
|
||||
|
||||
it('writeOnNewLine() should add a newline if the last char was not a newline', () => {
|
||||
textOutput.write('previous');
|
||||
textOutput.writeOnNewLine('hello');
|
||||
expect(getWrittenOutput()).toBe('previous\nhello');
|
||||
});
|
||||
|
||||
it('ensureTrailingNewline() should add a newline if one is missing', () => {
|
||||
textOutput.write('hello');
|
||||
textOutput.ensureTrailingNewline();
|
||||
expect(getWrittenOutput()).toBe('hello\n');
|
||||
});
|
||||
|
||||
it('ensureTrailingNewline() should not add a newline if one already exists', () => {
|
||||
textOutput.write('hello\n');
|
||||
textOutput.ensureTrailingNewline();
|
||||
expect(getWrittenOutput()).toBe('hello\n');
|
||||
});
|
||||
|
||||
it('should handle a sequence of calls correctly', () => {
|
||||
textOutput.write('first');
|
||||
textOutput.writeOnNewLine('second');
|
||||
textOutput.write(' part');
|
||||
textOutput.ensureTrailingNewline();
|
||||
textOutput.ensureTrailingNewline(); // second call should do nothing
|
||||
textOutput.write('third');
|
||||
|
||||
expect(getWrittenOutput()).toMatchSnapshot();
|
||||
});
|
||||
|
||||
it('should correctly handle ANSI escape codes when determining line breaks', () => {
|
||||
const blue = (s: string) => `\u001b[34m${s}\u001b[39m`;
|
||||
const bold = (s: string) => `\u001b[1m${s}\u001b[22m`;
|
||||
|
||||
textOutput.write(blue('hello'));
|
||||
textOutput.writeOnNewLine(bold('world'));
|
||||
textOutput.write(blue('\n'));
|
||||
textOutput.writeOnNewLine('next');
|
||||
|
||||
expect(getWrittenOutput()).toMatchSnapshot();
|
||||
});
|
||||
|
||||
it('should handle empty strings with ANSI codes', () => {
|
||||
textOutput.write('hello');
|
||||
textOutput.write('\u001b[34m\u001b[39m'); // Empty blue string
|
||||
textOutput.writeOnNewLine('world');
|
||||
expect(getWrittenOutput()).toMatchSnapshot();
|
||||
});
|
||||
|
||||
it('should handle ANSI codes that do not end with a newline', () => {
|
||||
textOutput.write('hello\u001b[34m');
|
||||
textOutput.writeOnNewLine('world');
|
||||
expect(getWrittenOutput()).toMatchSnapshot();
|
||||
});
|
||||
});
|
||||
54
packages/cli/src/ui/utils/textOutput.ts
Normal file
54
packages/cli/src/ui/utils/textOutput.ts
Normal file
@@ -0,0 +1,54 @@
|
||||
/**
|
||||
* @license
|
||||
* Copyright 2025 Google LLC
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
/**
|
||||
* A utility to manage writing text to stdout, ensuring that newlines
|
||||
* are handled consistently and robustly across the application.
|
||||
*/
|
||||
|
||||
import stripAnsi from 'strip-ansi';
|
||||
|
||||
export class TextOutput {
|
||||
private atStartOfLine = true;
|
||||
|
||||
/**
|
||||
* Writes a string to stdout.
|
||||
* @param str The string to write.
|
||||
*/
|
||||
write(str: string): void {
|
||||
if (str.length === 0) {
|
||||
return;
|
||||
}
|
||||
process.stdout.write(str);
|
||||
const strippedStr = stripAnsi(str);
|
||||
if (strippedStr.length > 0) {
|
||||
this.atStartOfLine = strippedStr.endsWith('\n');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Writes a string to stdout, ensuring it starts on a new line.
|
||||
* If the previous output did not end with a newline, one will be added.
|
||||
* This prevents adding extra blank lines if a newline already exists.
|
||||
* @param str The string to write.
|
||||
*/
|
||||
writeOnNewLine(str: string): void {
|
||||
if (!this.atStartOfLine) {
|
||||
this.write('\n');
|
||||
}
|
||||
this.write(str);
|
||||
}
|
||||
|
||||
/**
|
||||
* Ensures that the output ends with a newline. If the last character
|
||||
* written was not a newline, one will be added.
|
||||
*/
|
||||
ensureTrailingNewline(): void {
|
||||
if (!this.atStartOfLine) {
|
||||
this.write('\n');
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user