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 cursor *GridCursor
cGrid *compact.CompactGrid cGrid *compact.CompactGrid
header *widgets.CTopHeader header *widgets.CTopHeader
versionStr = fmt.Sprintf("ctop version %v, build %v", version, build)
) )
func main() { func main() {
@@ -36,7 +38,7 @@ func main() {
flag.Parse() flag.Parse()
if *versionFlag { if *versionFlag {
printVersion() fmt.Println(versionStr)
os.Exit(0) os.Exit(0)
} }
@@ -77,8 +79,8 @@ func main() {
if err := ui.Init(); err != nil { if err := ui.Init(); err != nil {
panic(err) panic(err)
} }
defer ui.Close()
defer Shutdown()
// init grid, cursor, header // init grid, cursor, header
cursor = NewGridCursor() cursor = NewGridCursor()
cGrid = compact.NewCompactGrid() cGrid = compact.NewCompactGrid()
@@ -87,13 +89,17 @@ func main() {
for { for {
exit := Display() exit := Display()
if exit { if exit {
log.Notice("shutting down")
log.Exit()
return return
} }
} }
} }
func Shutdown() {
log.Notice("shutting down")
log.Exit()
ui.Close()
}
// ensure a given sort field is valid // ensure a given sort field is valid
func validSort(s string) { func validSort(s string) {
if _, ok := Sorters[s]; !ok { if _, ok := Sorters[s]; !ok {
@@ -104,7 +110,7 @@ func validSort(s string) {
func panicExit() { func panicExit() {
if r := recover(); r != nil { if r := recover(); r != nil {
ui.Clear() Shutdown()
fmt.Printf("panic: %s\n", r) fmt.Printf("panic: %s\n", r)
os.Exit(1) os.Exit(1)
} }
@@ -121,7 +127,3 @@ func printHelp() {
fmt.Println(helpMsg) fmt.Println(helpMsg)
flag.PrintDefaults() flag.PrintDefaults()
} }
func printVersion() {
fmt.Printf("ctop version %v, build %v\n", version, build)
}