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