Skip to content

Commit

Permalink
add global Shutdown() method for exit cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
bcicen committed Mar 23, 2017
1 parent 5db90f3 commit 084c0c4
Showing 1 changed file with 11 additions and 9 deletions.
20 changes: 11 additions & 9 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ var (
cursor *GridCursor
cGrid *compact.CompactGrid
header *widgets.CTopHeader

versionStr = fmt.Sprintf("ctop version %v, build %v", version, build)
)

func main() {
Expand All @@ -36,7 +38,7 @@ func main() {
flag.Parse()

if *versionFlag {
printVersion()
fmt.Println(versionStr)
os.Exit(0)
}

Expand Down Expand Up @@ -77,8 +79,8 @@ func main() {
if err := ui.Init(); err != nil {
panic(err)
}
defer ui.Close()

defer Shutdown()
// init grid, cursor, header
cursor = NewGridCursor()
cGrid = compact.NewCompactGrid()
Expand All @@ -87,13 +89,17 @@ func main() {
for {
exit := Display()
if exit {
log.Notice("shutting down")
log.Exit()
return
}
}
}

func Shutdown() {
log.Notice("shutting down")
log.Exit()
ui.Close()
}

// ensure a given sort field is valid
func validSort(s string) {
if _, ok := Sorters[s]; !ok {
Expand All @@ -104,7 +110,7 @@ func validSort(s string) {

func panicExit() {
if r := recover(); r != nil {
ui.Clear()
Shutdown()
fmt.Printf("panic: %s\n", r)
os.Exit(1)
}
Expand All @@ -121,7 +127,3 @@ func printHelp() {
fmt.Println(helpMsg)
flag.PrintDefaults()
}

func printVersion() {
fmt.Printf("ctop version %v, build %v\n", version, build)
}

0 comments on commit 084c0c4

Please sign in to comment.