add global Shutdown() method for exit cleanup

This commit is contained in:
Bradley Cicenas 2017-03-23 16:48:25 +10:00
parent 5db90f31dc
commit 084c0c4ec8

20
main.go
View File

@ -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() {
@ -36,7 +38,7 @@ func main() {
flag.Parse()
if *versionFlag {
printVersion()
fmt.Println(versionStr)
os.Exit(0)
}
@ -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()
@ -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 {
@ -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)
}
@ -121,7 +127,3 @@ func printHelp() {
fmt.Println(helpMsg)
flag.PrintDefaults()
}
func printVersion() {
fmt.Printf("ctop version %v, build %v\n", version, build)
}