Function to drain controlling tty

This commit is contained in:
Kovid Goyal
2023-01-23 16:07:00 +05:30
parent a009d6b258
commit cc1f0bc3fe
4 changed files with 29 additions and 3 deletions

View File

@@ -315,3 +315,25 @@ func DebugPrintln(a ...any) {
term.DebugPrintln(a...)
}
}
func DrainControllingTTY(wait_for time.Duration) {
tty, err := OpenControllingTerm(SetRaw, SetNoEcho)
if err != nil {
return
}
sel := utils.CreateSelect(1)
sel.RegisterRead(tty.Fd())
for {
n, err := sel.Wait(wait_for)
if err == unix.EINTR {
continue
}
if err != nil {
return
}
if n > 0 && sel.IsReadyToRead(tty.Fd()) {
tty.Read(make([]byte, 256))
}
break
}
}