From 89d9a8f1d903b4940d1dbbb5c386d47a67a3f83c Mon Sep 17 00:00:00 2001 From: Ayman Bagabas Date: Fri, 7 Mar 2025 11:51:50 -0500 Subject: [PATCH] fix: only read from stdin if there is data to read Always check if there is data to read from stdin before reading from it. Otherwise, the program will hang and wait for input or EOT. --- main.go | 2 +- mods.go | 2 +- term.go | 5 +++++ 3 files changed, 7 insertions(+), 2 deletions(-) diff --git a/main.go b/main.go index 253e135e..c1a0aa1a 100644 --- a/main.go +++ b/main.go @@ -412,7 +412,7 @@ func maybeWriteMemProfile() { func handleError(err error) { maybeWriteMemProfile() // exhaust stdin - if !isInputTTY() { + if !isInputTTY() && shouldReadInput() { _, _ = io.ReadAll(os.Stdin) } diff --git a/mods.go b/mods.go index 1162635b..7acaba28 100644 --- a/mods.go +++ b/mods.go @@ -608,7 +608,7 @@ func (m *Mods) findReadID(in string) (*Conversation, error) { } func (m *Mods) readStdinCmd() tea.Msg { - if !isInputTTY() { + if !isInputTTY() && shouldReadInput() { reader := bufio.NewReader(os.Stdin) stdinBytes, err := io.ReadAll(reader) if err != nil { diff --git a/term.go b/term.go index fd025c1b..b981136e 100644 --- a/term.go +++ b/term.go @@ -12,6 +12,11 @@ var isInputTTY = OnceValue(func() bool { return isatty.IsTerminal(os.Stdin.Fd()) }) +var shouldReadInput = OnceValue(func() bool { + stat, _ := os.Stdin.Stat() + return stat.Size() > 0 +}) + var isOutputTTY = OnceValue(func() bool { return isatty.IsTerminal(os.Stdout.Fd()) })