Skip to content

Commit c641c39

Browse files
committed
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 <[email protected]>
1 parent 8f6c4e4 commit c641c39

File tree

2 files changed

+7
-7
lines changed

2 files changed

+7
-7
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ Golang package for dealing with consoles. Light on deps and a simple API.
99
## Modifying the current process
1010

1111
```go
12-
current := console.Current()
12+
current, err := console.Current()
13+
if err != nil {
14+
}
1315
defer current.Reset()
1416

1517
if err := current.SetRaw(); err != nil {

console.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,19 +65,17 @@ type WinSize struct {
6565
}
6666

6767
// Current returns the current process' console
68-
func Current() (c Console) {
69-
var err error
68+
func Current() (c Console, err error) {
7069
// Usually all three streams (stdin, stdout, and stderr)
7170
// are open to the same console, but some might be redirected,
7271
// so try all three.
7372
for _, s := range []*os.File{os.Stderr, os.Stdout, os.Stdin} {
7473
if c, err = ConsoleFromFile(s); err == nil {
75-
return c
74+
return c, nil
7675
}
7776
}
78-
// One of the std streams should always be a console
79-
// for the design of this function.
80-
panic(err)
77+
78+
return nil, err
8179
}
8280

8381
// ConsoleFromFile returns a console using the provided file

0 commit comments

Comments
 (0)