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

View File

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

View File

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