From 8b7b7bb8d849ac6609b60c0588574c9d5dbebf64 Mon Sep 17 00:00:00 2001 From: Caleb Bassi Date: Tue, 10 Apr 2018 20:08:02 -0700 Subject: [PATCH] Add Error function; improve logging --- Gopkg.lock | 2 +- utils/utils.go | 14 +++++++++++++ vendor/github.com/cjbassi/termui/Gopkg.lock | 2 +- vendor/github.com/cjbassi/termui/README.md | 1 + vendor/github.com/cjbassi/termui/sparkline.go | 20 ++++++++++++++++--- vendor/github.com/cjbassi/termui/table.go | 10 ++++++++-- vendor/github.com/cjbassi/termui/utils.go | 12 +++++++++++ widgets/cpu.go | 12 +++++++++-- 8 files changed, 64 insertions(+), 9 deletions(-) diff --git a/Gopkg.lock b/Gopkg.lock index 98e9db61..438d9b84 100644 --- a/Gopkg.lock +++ b/Gopkg.lock @@ -17,7 +17,7 @@ branch = "master" name = "github.com/cjbassi/termui" packages = ["."] - revision = "66ddabe8ef7585a3ccfd0c14e7218940f5a95777" + revision = "b7a9b813b6a3bb09604fe10853cfd437291f20a1" [[projects]] branch = "master" diff --git a/utils/utils.go b/utils/utils.go index 046e791e..ef381627 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -1,7 +1,10 @@ package utils import ( + "fmt" "math" + + ui "github.com/cjbassi/termui" ) func BytesToKB(b uint64) float64 { @@ -15,3 +18,14 @@ func BytesToMB(b uint64) float64 { func BytesToGB(b uint64) float64 { return float64(b) / math.Pow10(9) } + +func Error(issue, diagnostics string) { + ui.Close() + fmt.Println("Error caught. Exiting program.") + fmt.Println() + fmt.Println("Issue with " + issue + ".") + fmt.Println() + fmt.Println("Diagnostics:\n" + diagnostics) + fmt.Println() + panic(1) +} diff --git a/vendor/github.com/cjbassi/termui/Gopkg.lock b/vendor/github.com/cjbassi/termui/Gopkg.lock index 893acce5..389639a3 100644 --- a/vendor/github.com/cjbassi/termui/Gopkg.lock +++ b/vendor/github.com/cjbassi/termui/Gopkg.lock @@ -17,7 +17,7 @@ branch = "master" name = "github.com/nsf/termbox-go" packages = ["."] - revision = "e2050e41c8847748ec5288741c0b19a8cb26d084" + revision = "3e24a7b6661e09b87a9f49d693034219f81602fa" [solve-meta] analyzer-name = "dep" diff --git a/vendor/github.com/cjbassi/termui/README.md b/vendor/github.com/cjbassi/termui/README.md index 5201197b..8b8e291c 100644 --- a/vendor/github.com/cjbassi/termui/README.md +++ b/vendor/github.com/cjbassi/termui/README.md @@ -12,6 +12,7 @@ Some usage improvements include: * linegraph uses [drawille-go](https://github.com/exrook/drawille-go) * no longer have to choose between dot mode and braille mode; uses a superior braille mode * table supports mouse and keyboard navigation +* table is scrollable * more powerful table column width sizing * visual improvements to linegraph and table diff --git a/vendor/github.com/cjbassi/termui/sparkline.go b/vendor/github.com/cjbassi/termui/sparkline.go index 41c35b19..e9073d3f 100644 --- a/vendor/github.com/cjbassi/termui/sparkline.go +++ b/vendor/github.com/cjbassi/termui/sparkline.go @@ -1,7 +1,7 @@ package termui import ( - "log" + "fmt" ) var SPARKS = [8]rune{'▁', '▂', '▃', '▄', '▅', '▆', '▇', '█'} @@ -71,9 +71,23 @@ func (self *Sparklines) Buffer() *Buffer { for x := self.X; x >= 1; x-- { char := SPARKS[0] if (self.X - x) < len(line.Data) { - index := int((float64(line.Data[(len(line.Data)-1)-(self.X-x)]) / float64(max)) * 7) + offset := self.X - x + cur_item := line.Data[(len(line.Data)-1)-offset] + percent := float64(cur_item) / float64(max) + index := int(percent * 7) if index < 0 || index >= len(SPARKS) { - log.Fatalf("\nindex: %d\nlen(SPARKS): %d", index, len(SPARKS)) + Error("sparkline", + fmt.Sprint( + "len(line.Data): ", len(line.Data), "\n", + "max: ", max, "\n", + "x: ", x, "\n", + "self.X: ", self.X, "\n", + "offset: ", offset, "\n", + "cur_item: ", cur_item, "\n", + "percent: ", percent, "\n", + "index: ", index, "\n", + "len(SPARKS): ", len(SPARKS), + )) } char = SPARKS[index] } diff --git a/vendor/github.com/cjbassi/termui/table.go b/vendor/github.com/cjbassi/termui/table.go index ab039077..d86798c3 100644 --- a/vendor/github.com/cjbassi/termui/table.go +++ b/vendor/github.com/cjbassi/termui/table.go @@ -1,7 +1,7 @@ package termui import ( - "log" + "fmt" "strings" ) @@ -76,7 +76,13 @@ func (self *Table) Buffer() *Buffer { // prints each row for rowNum := self.TopRow; rowNum < self.TopRow+self.Y-1 && rowNum < len(self.Rows); rowNum++ { if rowNum < 0 || rowNum >= len(self.Rows) { - log.Fatalf("\nrowNum: %d\nself.TopRow: %d\nlen(self.Rows): %d\nself.Y: %d", rowNum, self.TopRow, len(self.Rows), self.Y) + Error("table rows", + fmt.Sprint( + "rowNum: ", rowNum, "\n", + "self.TopRow: ", self.TopRow, "\n", + "len(self.Rows): ", len(self.Rows), "\n", + "self.Y: ", self.Y, + )) } row := self.Rows[rowNum] y := (rowNum + 2) - self.TopRow diff --git a/vendor/github.com/cjbassi/termui/utils.go b/vendor/github.com/cjbassi/termui/utils.go index c8f4da38..614828bc 100644 --- a/vendor/github.com/cjbassi/termui/utils.go +++ b/vendor/github.com/cjbassi/termui/utils.go @@ -1,6 +1,7 @@ package termui import ( + "fmt" "math" ) @@ -22,3 +23,14 @@ func MaxString(s string, l int) string { func Round(f float64) float64 { return math.Floor(f + .5) } + +func Error(issue, diagnostics string) { + Close() + fmt.Println("Error caught. Exiting program.") + fmt.Println() + fmt.Println("Issue with " + issue + ".") + fmt.Println() + fmt.Println("Diagnostics:\n" + diagnostics) + fmt.Println() + panic(1) +} diff --git a/widgets/cpu.go b/widgets/cpu.go index 84812f7a..4e226c02 100644 --- a/widgets/cpu.go +++ b/widgets/cpu.go @@ -1,10 +1,11 @@ package widgets import ( - "log" + "fmt" "strconv" "time" + "github.com/cjbassi/gotop/utils" ui "github.com/cjbassi/termui" psCPU "github.com/shirou/gopsutil/cpu" ) @@ -50,7 +51,14 @@ func (self *CPU) update() { percents, _ := psCPU.Percent(self.interval, true) if len(percents) != self.Count { count, _ := psCPU.Counts(false) - log.Fatalf("\nself.Count: %d\ngopsutil.Counts(): %d\nlen(percents): %d\npercents: %v\nself.interval: %v", self.Count, count, len(percents), percents, self.interval) + utils.Error("CPU percentages", + fmt.Sprint( + "self.Count: ", self.Count, "\n", + "gopsutil.Counts(): ", count, "\n", + "len(percents): ", len(percents), "\n", + "percents: ", percents, "\n", + "self.interval: ", self.interval, + )) } for i := 0; i < self.Count; i++ { key := "CPU" + strconv.Itoa(i)