From eb474ddad257ecd9820876516880958c2cc21e5d Mon Sep 17 00:00:00 2001 From: Timothy Warren Date: Fri, 19 Mar 2021 17:08:19 -0400 Subject: [PATCH] check for valid terminal --- internal/terminal/terminal.go | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/internal/terminal/terminal.go b/internal/terminal/terminal.go index df66c86..5c6a817 100644 --- a/internal/terminal/terminal.go +++ b/internal/terminal/terminal.go @@ -2,12 +2,22 @@ package terminal import ( "fmt" - "golang.org/x/term" "os" + + "golang.org/x/term" ) +// Is this a valid interactive terminal? +func check() { + if !term.IsTerminal(int(os.Stdin.Fd())) { + panic("An interactive terminal is required to use a text editor!") + } +} + // Put the terminal in raw mode func RawOn() *term.State { + check() + oldState, err := term.MakeRaw(int(os.Stdin.Fd())) if err != nil { panic(err) @@ -29,4 +39,4 @@ func OutLn(format string, a ...interface{}) { formatted := fmt.Sprintf(format, a...) fmt.Printf("%s\r\n", formatted) -} \ No newline at end of file +}