Adds visible error handling for bad exits and improves exit code use

This commit is contained in:
sloumdrone 2019-12-05 21:46:31 -08:00
parent c64d06610e
commit 6f0be3b4e4
3 changed files with 17 additions and 11 deletions

View File

@ -47,8 +47,8 @@ func (c *client) GetSizeOnce() {
cmd.Stdin = os.Stdin
out, err := cmd.Output()
if err != nil {
fmt.Println("Fatal error: Unable to retrieve terminal size")
os.Exit(5)
cui.ExitMessage = "Fatal error: Unable to retrieve terminal size"
cui.Exit(5)
}
var h, w int
_, _ = fmt.Sscan(string(out), &h, &w)
@ -66,8 +66,8 @@ func (c *client) GetSize() {
cmd.Stdin = os.Stdin
out, err := cmd.Output()
if err != nil {
fmt.Println("Fatal error: Unable to retrieve terminal size")
os.Exit(5)
cui.ExitMessage = "Fatal error: Unable to retrieve terminal size"
cui.Exit(5)
}
var h, w int
_, _ = fmt.Sscan(string(out), &h, &w)
@ -157,7 +157,7 @@ func (c *client) TakeControlInput() {
c.Scroll(-1)
case 'q', 'Q':
// quit bombadillo
cui.Exit()
cui.Exit(0)
case 'g':
// scroll to top
c.ClearMessage()
@ -278,7 +278,7 @@ func (c *client) simpleCommand(action string) {
action = strings.ToUpper(action)
switch action {
case "Q", "QUIT":
cui.Exit()
cui.Exit(0)
case "H", "HOME":
if c.Options["homeurl"] != "unset" {
go c.Visit(c.Options["homeurl"])

View File

@ -26,6 +26,8 @@ var Shapes = map[string]string{
"abr": "▟",
}
var ExitMessage string
func MoveCursorTo(row, col int) {
fmt.Printf("\033[%d;%dH", row, col)
}
@ -44,9 +46,12 @@ func moveCursorToward(dir string, amount int) {
}
// Exit performs cleanup operations before exiting the application
func Exit() {
func Exit(exitCode int) {
CleanupTerm()
os.Exit(0)
if ExitMessage != "" {
fmt.Print(ExitMessage, "\n")
}
os.Exit(exitCode)
}
// InitTerm sets the terminal modes appropriate for Bombadillo

View File

@ -165,7 +165,7 @@ func handleSignals(c <-chan os.Signal) {
cui.InitTerm()
bombadillo.Draw()
case syscall.SIGINT:
cui.Exit()
cui.Exit(130)
}
}
}
@ -197,11 +197,12 @@ func main() {
args := flag.Args()
cui.InitTerm()
defer cui.Exit()
defer cui.Exit(0)
err := initClient()
if err != nil {
// if we can't initialize we should bail out
panic(err)
cui.ExitMessage = err.Error()
cui.Exit(1)
}
// watch for signals, send them to be handled