Fix shebang viewing of short scripts not working

This commit is contained in:
Kovid Goyal
2025-03-20 12:49:28 +05:30
parent f3448cbbee
commit 0afa6d5b3d
2 changed files with 38 additions and 2 deletions

View File

@@ -78,6 +78,10 @@ func SetReadTimeout(d time.Duration) TermiosOperation {
var SetBlockingRead TermiosOperation = SetReadTimeout(0)
var SetNoCanonical TermiosOperation = func(t *unix.Termios) {
t.Lflag &^= unix.ICANON
}
var SetRaw TermiosOperation = func(t *unix.Termios) {
// This attempts to replicate the behaviour documented for cfmakeraw in
// the termios(3) manpage, as Go doesn't wrap cfmakeraw probably because its not in POSIX
@@ -380,3 +384,21 @@ func DebugPrintln(a ...any) {
term.DebugPrintln(a...)
}
}
func ReadSingleByteFromTerminal() (b byte, err error) {
term, err := OpenControllingTerm(SetBlockingRead, SetNoCanonical)
if err != nil {
return 0, err
}
defer term.Close()
ans := []byte{b}
for {
n, err := term.Read(ans)
if err != nil {
return 0, err
}
if n > 0 {
return ans[0], nil
}
}
}