Refactor
This commit is contained in:
parent
49f89cf7e9
commit
e74e48424a
76
main.go
76
main.go
|
@ -22,25 +22,29 @@ import (
|
|||
ui "github.com/gizak/termui"
|
||||
)
|
||||
|
||||
var version = "1.7.1"
|
||||
const (
|
||||
version = "1.7.1"
|
||||
|
||||
graphHorizontalScaleDelta = 3
|
||||
)
|
||||
|
||||
var (
|
||||
colorscheme = colorschemes.Default
|
||||
minimal = false
|
||||
interval = time.Second
|
||||
zoom = 7
|
||||
zoomInterval = 3
|
||||
helpVisible = false
|
||||
averageLoad = false
|
||||
battery = false
|
||||
percpuLoad = false
|
||||
fahrenheit = false
|
||||
configDir = appdir.New("gotop").UserConfig()
|
||||
logPath = filepath.Join(configDir, "errors.log")
|
||||
configDir = appdir.New("gotop").UserConfig()
|
||||
logPath = filepath.Join(configDir, "errors.log")
|
||||
|
||||
stderrLogger = log.New(os.Stderr, "", 0)
|
||||
statusbar = false
|
||||
termWidth int
|
||||
termHeight int
|
||||
|
||||
graphHorizontalScale = 7
|
||||
helpVisible = false
|
||||
|
||||
colorscheme = colorschemes.Default
|
||||
updateInterval = time.Second
|
||||
minimalMode = false
|
||||
averageLoad = false
|
||||
percpuLoad = false
|
||||
fahrenheit = false
|
||||
battery = false
|
||||
statusbar = false
|
||||
|
||||
cpu *w.CPU
|
||||
batt *w.Batt
|
||||
|
@ -90,7 +94,7 @@ Colorschemes:
|
|||
percpuLoad, _ = args["--percpu"].(bool)
|
||||
battery, _ = args["--battery"].(bool)
|
||||
|
||||
minimal, _ = args["--minimal"].(bool)
|
||||
minimalMode, _ = args["--minimal"].(bool)
|
||||
|
||||
statusbar, _ = args["--statusbar"].(bool)
|
||||
|
||||
|
@ -100,9 +104,9 @@ Colorschemes:
|
|||
return fmt.Errorf("invalid rate parameter")
|
||||
}
|
||||
if rate < 1 {
|
||||
interval = time.Second * time.Duration(1/rate)
|
||||
updateInterval = time.Second * time.Duration(1/rate)
|
||||
} else {
|
||||
interval = time.Second / time.Duration(rate)
|
||||
updateInterval = time.Second / time.Duration(rate)
|
||||
}
|
||||
fahrenheit, _ = args["--fahrenheit"].(bool)
|
||||
|
||||
|
@ -143,12 +147,12 @@ func getCustomColorscheme(name string) (colorschemes.Colorscheme, error) {
|
|||
return colorscheme, nil
|
||||
}
|
||||
|
||||
func setupGrid() {
|
||||
func setupGrid(termWidth, termHeight int) {
|
||||
grid = ui.NewGrid()
|
||||
grid.SetRect(0, 0, termWidth, termHeight)
|
||||
|
||||
var barRow interface{}
|
||||
if minimal {
|
||||
if minimalMode {
|
||||
rowHeight := 1.0 / 2
|
||||
if statusbar {
|
||||
rowHeight = 50.0 / 101
|
||||
|
@ -223,7 +227,7 @@ func widgetColors() {
|
|||
i++
|
||||
}
|
||||
|
||||
if !minimal {
|
||||
if !minimalMode {
|
||||
if battery {
|
||||
var battKeys []string
|
||||
for key := range batt.Data {
|
||||
|
@ -257,12 +261,12 @@ func initWidgets() {
|
|||
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
cpu = w.NewCPU(interval, zoom, averageLoad, percpuLoad)
|
||||
cpu = w.NewCPU(updateInterval, graphHorizontalScale, averageLoad, percpuLoad)
|
||||
wg.Done()
|
||||
}()
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
mem = w.NewMem(interval, zoom)
|
||||
mem = w.NewMem(updateInterval, graphHorizontalScale)
|
||||
wg.Done()
|
||||
}()
|
||||
wg.Add(1)
|
||||
|
@ -270,11 +274,11 @@ func initWidgets() {
|
|||
proc = w.NewProc()
|
||||
wg.Done()
|
||||
}()
|
||||
if !minimal {
|
||||
if !minimalMode {
|
||||
if battery {
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
batt = w.NewBatt(time.Minute, zoom)
|
||||
batt = w.NewBatt(time.Minute, graphHorizontalScale)
|
||||
wg.Done()
|
||||
}()
|
||||
}
|
||||
|
@ -299,7 +303,7 @@ func initWidgets() {
|
|||
}
|
||||
|
||||
func eventLoop() {
|
||||
drawTicker := time.NewTicker(interval).C
|
||||
drawTicker := time.NewTicker(updateInterval).C
|
||||
|
||||
// handles kill signal sent to gotop
|
||||
sigTerm := make(chan os.Signal, 2)
|
||||
|
@ -346,15 +350,15 @@ func eventLoop() {
|
|||
case "?":
|
||||
ui.Render(grid)
|
||||
case "h":
|
||||
zoom += zoomInterval
|
||||
cpu.Zoom = zoom
|
||||
mem.Zoom = zoom
|
||||
graphHorizontalScale += graphHorizontalScaleDelta
|
||||
cpu.HorizontalScale = graphHorizontalScale
|
||||
mem.HorizontalScale = graphHorizontalScale
|
||||
ui.Render(cpu, mem)
|
||||
case "l":
|
||||
if zoom > zoomInterval {
|
||||
zoom -= zoomInterval
|
||||
cpu.Zoom = zoom
|
||||
mem.Zoom = zoom
|
||||
if graphHorizontalScale > graphHorizontalScaleDelta {
|
||||
graphHorizontalScale -= graphHorizontalScaleDelta
|
||||
cpu.HorizontalScale = graphHorizontalScale
|
||||
mem.HorizontalScale = graphHorizontalScale
|
||||
ui.Render(cpu, mem)
|
||||
}
|
||||
case "<Resize>":
|
||||
|
@ -449,7 +453,7 @@ func main() {
|
|||
|
||||
logging.StderrToLogfile(lf)
|
||||
|
||||
termWidth, termHeight = ui.TerminalSize()
|
||||
termWidth, termHeight := ui.TerminalSize()
|
||||
|
||||
termuiColors() // need to do this before initializing widgets so that they can inherit the colors
|
||||
initWidgets()
|
||||
|
@ -457,7 +461,7 @@ func main() {
|
|||
help = w.NewHelpMenu()
|
||||
help.Resize(termWidth, termHeight)
|
||||
|
||||
setupGrid()
|
||||
setupGrid(termWidth, termHeight)
|
||||
ui.Render(grid)
|
||||
|
||||
eventLoop()
|
||||
|
|
|
@ -11,10 +11,10 @@ import (
|
|||
// LineGraph implements a line graph of data points.
|
||||
type LineGraph struct {
|
||||
*Block
|
||||
Data map[string][]float64
|
||||
LineColor map[string]Attribute
|
||||
Zoom int
|
||||
Labels map[string]string
|
||||
Data map[string][]float64
|
||||
LineColor map[string]Attribute
|
||||
HorizontalScale int
|
||||
Labels map[string]string
|
||||
|
||||
DefaultLineColor Attribute
|
||||
}
|
||||
|
@ -22,11 +22,11 @@ type LineGraph struct {
|
|||
// NewLineGraph returns a new LineGraph with current theme.
|
||||
func NewLineGraph() *LineGraph {
|
||||
return &LineGraph{
|
||||
Block: NewBlock(),
|
||||
Data: make(map[string][]float64),
|
||||
LineColor: make(map[string]Attribute),
|
||||
Labels: make(map[string]string),
|
||||
Zoom: 5,
|
||||
Block: NewBlock(),
|
||||
Data: make(map[string][]float64),
|
||||
LineColor: make(map[string]Attribute),
|
||||
Labels: make(map[string]string),
|
||||
HorizontalScale: 5,
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -63,11 +63,11 @@ func (self *LineGraph) Draw(buf *Buffer) {
|
|||
lastY, lastX := -1, -1
|
||||
// assign colors to `colors` and lines/points to the canvas
|
||||
for i := len(seriesData) - 1; i >= 0; i-- {
|
||||
x := ((self.Inner.Dx() + 1) * 2) - 1 - (((len(seriesData) - 1) - i) * self.Zoom)
|
||||
x := ((self.Inner.Dx() + 1) * 2) - 1 - (((len(seriesData) - 1) - i) * self.HorizontalScale)
|
||||
y := ((self.Inner.Dy() + 1) * 4) - 1 - int((float64((self.Inner.Dy())*4)-1)*(seriesData[i]/100))
|
||||
if x < 0 {
|
||||
// render the line to the last point up to the wall
|
||||
if x > 0-self.Zoom {
|
||||
if x > 0-self.HorizontalScale {
|
||||
for _, p := range drawille.Line(lastX, lastY, x, y) {
|
||||
if p.X > 0 {
|
||||
c.Set(p.X, p.Y)
|
||||
|
|
|
@ -17,7 +17,7 @@ type Batt struct {
|
|||
interval time.Duration
|
||||
}
|
||||
|
||||
func NewBatt(interval time.Duration, zoom int) *Batt {
|
||||
func NewBatt(interval time.Duration, horizontalScale int) *Batt {
|
||||
batts, err := battery.GetAll()
|
||||
self := &Batt{
|
||||
LineGraph: ui.NewLineGraph(),
|
||||
|
@ -25,7 +25,7 @@ func NewBatt(interval time.Duration, zoom int) *Batt {
|
|||
interval: interval,
|
||||
}
|
||||
self.Title = "Battery Status"
|
||||
self.Zoom = zoom
|
||||
self.HorizontalScale = horizontalScale
|
||||
if err != nil {
|
||||
log.Printf("failed to get battery info from system: %v", err)
|
||||
}
|
||||
|
|
|
@ -18,7 +18,7 @@ type CPU struct {
|
|||
formatString string
|
||||
}
|
||||
|
||||
func NewCPU(interval time.Duration, zoom int, average bool, percpu bool) *CPU {
|
||||
func NewCPU(interval time.Duration, horizontalScale int, average bool, percpu bool) *CPU {
|
||||
count, err := psCPU.Counts(false)
|
||||
if err != nil {
|
||||
log.Printf("failed to get CPU count from gopsutil: %v", err)
|
||||
|
@ -36,7 +36,7 @@ func NewCPU(interval time.Duration, zoom int, average bool, percpu bool) *CPU {
|
|||
formatString: formatString,
|
||||
}
|
||||
self.Title = " CPU Usage "
|
||||
self.Zoom = zoom
|
||||
self.HorizontalScale = horizontalScale
|
||||
|
||||
if !(self.Average || self.PerCPU) {
|
||||
if self.Count <= 8 {
|
||||
|
|
|
@ -15,13 +15,13 @@ type Mem struct {
|
|||
interval time.Duration
|
||||
}
|
||||
|
||||
func NewMem(interval time.Duration, zoom int) *Mem {
|
||||
func NewMem(interval time.Duration, horizontalScale int) *Mem {
|
||||
self := &Mem{
|
||||
LineGraph: ui.NewLineGraph(),
|
||||
interval: interval,
|
||||
}
|
||||
self.Title = " Memory Usage "
|
||||
self.Zoom = zoom
|
||||
self.HorizontalScale = horizontalScale
|
||||
self.Data["Main"] = []float64{0}
|
||||
self.Data["Swap"] = []float64{0}
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user