|
| 1 | +// Copyright (c) 2026 Circle Internet Services, Inc. |
| 2 | +// |
| 3 | +// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 4 | +// of this software and associated documentation files (the "Software"), to deal |
| 5 | +// in the Software without restriction, including without limitation the rights |
| 6 | +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 7 | +// copies of the Software, and to permit persons to whom the Software is |
| 8 | +// furnished to do so, subject to the following conditions: |
| 9 | +// |
| 10 | +// The above copyright notice and this permission notice shall be included in |
| 11 | +// all copies or substantial portions of the Software. |
| 12 | +// |
| 13 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 14 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 15 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 16 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 17 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 18 | +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 19 | +// SOFTWARE. |
| 20 | +// |
| 21 | +// SPDX-License-Identifier: MIT |
| 22 | + |
| 23 | +//go:build linux |
| 24 | + |
| 25 | +package iostream |
| 26 | + |
| 27 | +import "golang.org/x/sys/unix" |
| 28 | + |
| 29 | +// drainStdin discards bytes pending in stdin's line discipline buffer. |
| 30 | +// |
| 31 | +// Bubbletea v2 writes capability queries (mode 2026 synchronized output, mode |
| 32 | +// 2027 unicode core) to the output at startup. The terminal responds on stdin. |
| 33 | +// Because the spinner uses WithInput(nil) the input loop never runs, so those |
| 34 | +// responses accumulate in the TTY line discipline buffer. A plain read() cannot |
| 35 | +// drain them in canonical mode (no newline, so the line discipline won't |
| 36 | +// deliver the data). TCFLSH/TCIFLUSH discards the buffer directly. |
| 37 | +// |
| 38 | +// We loop — flush then poll — to handle SSH round-trip latency where responses |
| 39 | +// may arrive after the first flush. On local terminals the poll returns |
| 40 | +// immediately with no data. See: charmbracelet/bubbletea#1590. |
| 41 | +func drainStdin() { |
| 42 | + fds := []unix.PollFd{{Fd: 0, Events: unix.POLLIN}} // stdin is always fd 0 |
| 43 | + for { |
| 44 | + _ = unix.IoctlSetInt(0, unix.TCFLSH, 0) // 0 = TCIFLUSH: discard received, unread data |
| 45 | + n, _ := unix.Poll(fds, 200) |
| 46 | + if n <= 0 { |
| 47 | + return |
| 48 | + } |
| 49 | + } |
| 50 | +} |
0 commit comments