From c641c399ff5ce41e5e57b1aaee9aacf3930fe843 Mon Sep 17 00:00:00 2001 From: Marat Radchenko Date: Mon, 24 Jul 2023 17:54:24 +0300 Subject: [PATCH] Stop panicking in `Current()` Instead, just return an error, so it's up to the calling code how to handle it. This is an API-breaking change. Signed-off-by: Marat Radchenko --- README.md | 4 +++- console.go | 10 ++++------ 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index a849a72..906233b 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,9 @@ Golang package for dealing with consoles. Light on deps and a simple API. ## Modifying the current process ```go -current := console.Current() +current, err := console.Current() +if err != nil { +} defer current.Reset() if err := current.SetRaw(); err != nil { diff --git a/console.go b/console.go index dd587d8..5e63773 100644 --- a/console.go +++ b/console.go @@ -65,19 +65,17 @@ type WinSize struct { } // Current returns the current process' console -func Current() (c Console) { - var err error +func Current() (c Console, err error) { // Usually all three streams (stdin, stdout, and stderr) // are open to the same console, but some might be redirected, // so try all three. for _, s := range []*os.File{os.Stderr, os.Stdout, os.Stdin} { if c, err = ConsoleFromFile(s); err == nil { - return c + return c, nil } } - // One of the std streams should always be a console - // for the design of this function. - panic(err) + + return nil, err } // ConsoleFromFile returns a console using the provided file