Add Error function; improve logging

This commit is contained in:
Caleb Bassi 2018-04-10 20:08:02 -07:00
parent 96596fe9f9
commit 8b7b7bb8d8
8 changed files with 64 additions and 9 deletions

2
Gopkg.lock generated
View File

@ -17,7 +17,7 @@
branch = "master"
name = "github.com/cjbassi/termui"
packages = ["."]
revision = "66ddabe8ef7585a3ccfd0c14e7218940f5a95777"
revision = "b7a9b813b6a3bb09604fe10853cfd437291f20a1"
[[projects]]
branch = "master"

View File

@ -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)
}

View File

@ -17,7 +17,7 @@
branch = "master"
name = "github.com/nsf/termbox-go"
packages = ["."]
revision = "e2050e41c8847748ec5288741c0b19a8cb26d084"
revision = "3e24a7b6661e09b87a9f49d693034219f81602fa"
[solve-meta]
analyzer-name = "dep"

View File

@ -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

View File

@ -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]
}

View File

@ -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

View File

@ -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)
}

View File

@ -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)