feat: Allow combining -p and stdin for prompt input (#4406)
Co-authored-by: Allen Hutchison <adh@google.com>
This commit is contained in:
parent
798c4d1311
commit
ec7b84191f
|
@ -297,8 +297,11 @@ export async function main() {
|
||||||
}
|
}
|
||||||
// If not a TTY, read from stdin
|
// If not a TTY, read from stdin
|
||||||
// This is for cases where the user pipes input directly into the command
|
// This is for cases where the user pipes input directly into the command
|
||||||
if (!process.stdin.isTTY && !input) {
|
if (!process.stdin.isTTY) {
|
||||||
input += await readStdin();
|
const stdinData = await readStdin();
|
||||||
|
if (stdinData) {
|
||||||
|
input = `${stdinData}\n\n${input}`;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if (!input) {
|
if (!input) {
|
||||||
console.error('No input provided via stdin.');
|
console.error('No input provided via stdin.');
|
||||||
|
|
|
@ -5,14 +5,26 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
export async function readStdin(): Promise<string> {
|
export async function readStdin(): Promise<string> {
|
||||||
|
const MAX_STDIN_SIZE = 8 * 1024 * 1024; // 8MB
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
let data = '';
|
let data = '';
|
||||||
|
let totalSize = 0;
|
||||||
process.stdin.setEncoding('utf8');
|
process.stdin.setEncoding('utf8');
|
||||||
|
|
||||||
const onReadable = () => {
|
const onReadable = () => {
|
||||||
let chunk;
|
let chunk;
|
||||||
while ((chunk = process.stdin.read()) !== null) {
|
while ((chunk = process.stdin.read()) !== null) {
|
||||||
|
if (totalSize + chunk.length > MAX_STDIN_SIZE) {
|
||||||
|
const remainingSize = MAX_STDIN_SIZE - totalSize;
|
||||||
|
data += chunk.slice(0, remainingSize);
|
||||||
|
console.warn(
|
||||||
|
`Warning: stdin input truncated to ${MAX_STDIN_SIZE} bytes.`,
|
||||||
|
);
|
||||||
|
process.stdin.destroy(); // Stop reading further
|
||||||
|
break;
|
||||||
|
}
|
||||||
data += chunk;
|
data += chunk;
|
||||||
|
totalSize += chunk.length;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue