Skip to content
This repository has been archived by the owner on Aug 29, 2020. It is now read-only.

Commit

Permalink
Port to termui 2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
cjbassi committed Jan 1, 2019
1 parent b73fe56 commit 2e48524
Show file tree
Hide file tree
Showing 14 changed files with 220 additions and 182 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,11 +113,11 @@ This will place the built packages into the `dist` folder.

## Built With

- [cjbassi/termui](https://github.com/cjbassi/termui)
- [drawille-go](https://github.com/exrook/drawille-go)
- [termbox](https://github.com/nsf/termbox-go)
- [gopsutil](https://github.com/shirou/gopsutil)
- [goreleaser](https://github.com/goreleaser/goreleaser)
- [gizak/termui](https://github.com/gizak/termui)
- [nsf/termbox](https://github.com/nsf/termbox-go)
- [exrook/drawille-go](https://github.com/exrook/drawille-go)
- [shirou/gopsutil](https://github.com/shirou/gopsutil)
- [goreleaser/goreleaser](https://github.com/goreleaser/goreleaser)

## Stargazers over time

Expand Down
7 changes: 2 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,11 @@ module github.com/cjbassi/gotop
require (
github.com/ProtonMail/go-appdir v0.0.0-20180220133335-7c788d1b45c6
github.com/StackExchange/wmi v0.0.0-20180116203802-5d049714c4a6 // indirect
github.com/cjbassi/drawille-go v0.0.0-20180329221028-ad535d0f92cd // indirect
github.com/cjbassi/termui v0.0.0-20181208033036-e8dd23f6146c
github.com/cjbassi/drawille-go v0.0.0-20180329221028-ad535d0f92cd
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/docopt/docopt-go v0.0.0-20180111231733-ee0de3bc6815
github.com/docopt/docopt.go v0.0.0-20180111231733-ee0de3bc6815
github.com/gizak/termui v0.0.0-20190101005313-46c77dca8480
github.com/go-ole/go-ole v1.2.1 // indirect
github.com/mattn/go-runewidth v0.0.2 // indirect
github.com/nsf/termbox-go v0.0.0-20180407224525-3e24a7b6661e // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/shirou/gopsutil v2.18.11+incompatible
github.com/shirou/w32 v0.0.0-20160930032740-bb4de0191aa4 // indirect
Expand Down
12 changes: 6 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,20 @@ github.com/StackExchange/wmi v0.0.0-20180116203802-5d049714c4a6 h1:fLjPD/aNc3UIO
github.com/StackExchange/wmi v0.0.0-20180116203802-5d049714c4a6/go.mod h1:3eOhrUMpNV+6aFIbp5/iudMxNCF27Vw2OZgy4xEx0Fg=
github.com/cjbassi/drawille-go v0.0.0-20180329221028-ad535d0f92cd h1:nSJpATLVvFa19BEHX4ys+VGNWfI4FUGMweEI6QXs8wg=
github.com/cjbassi/drawille-go v0.0.0-20180329221028-ad535d0f92cd/go.mod h1:vjcQJUZJYD3MeVGhtZXSMnCHfUNZxsyYzJt90eCYxK4=
github.com/cjbassi/termui v0.0.0-20181208033036-e8dd23f6146c h1:vcaCtK8ObawtpQRW8GdbKZ+eJGEUn41xJ8Snagd/c6I=
github.com/cjbassi/termui v0.0.0-20181208033036-e8dd23f6146c/go.mod h1:rqXckrwz+i0fH/zNwU6AdBNULHwmZsgehnSlhKP5i2Q=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/docopt/docopt-go v0.0.0-20180111231733-ee0de3bc6815 h1:bWDMxwH3px2JBh6AyO7hdCn/PkvCZXii8TGj7sbtEbQ=
github.com/docopt/docopt-go v0.0.0-20180111231733-ee0de3bc6815/go.mod h1:WwZ+bS3ebgob9U8Nd0kOddGdZWjyMGR8Wziv+TBNwSE=
github.com/docopt/docopt.go v0.0.0-20180111231733-ee0de3bc6815 h1:HMAfwOa33y82IaQEKQDfUCiwNlxtM1iw7HLM9ru0RNc=
github.com/docopt/docopt.go v0.0.0-20180111231733-ee0de3bc6815/go.mod h1:l7JNRynTRuqe45tpIyItHNqZWTxywYjp87MWTOnU5cg=
github.com/gizak/termui v0.0.0-20190101005313-46c77dca8480 h1:+NHS7QUnQqEwIuhrHaFT6R78LMGbja4fYTu6wny7Q4s=
github.com/gizak/termui v0.0.0-20190101005313-46c77dca8480/go.mod h1:S3xz8JHXNDPSNFsvXCdG7bHlEGrwvUG3a0joR/xYZ5M=
github.com/go-ole/go-ole v1.2.1 h1:2lOsA72HgjxAuMlKpFiCbHTvu44PIVkZ5hqm3RSdI/E=
github.com/go-ole/go-ole v1.2.1/go.mod h1:7FAglXiTm7HKlQRDeOQ6ZNUHidzCWXuZWq/1dTyBNF8=
github.com/mattn/go-runewidth v0.0.2 h1:UnlwIPBGaTZfPQ6T1IGzPI0EkYAQmT9fAEJ/poFC63o=
github.com/mattn/go-runewidth v0.0.2/go.mod h1:LwmH8dsx7+W8Uxz3IHJYH5QSwggIsqBzpuz5H//U1FU=
github.com/nsf/termbox-go v0.0.0-20180407224525-3e24a7b6661e h1:w2JDz0jtOlFFdvtUXISyYPFwmbZnwKL1mRDT0tKDvuk=
github.com/nsf/termbox-go v0.0.0-20180407224525-3e24a7b6661e/go.mod h1:IuKpRQcYE1Tfu+oAQqaLisqDeXgjyyltCfsaoYN18NQ=
github.com/mitchellh/go-wordwrap v0.0.0-20150314170334-ad45545899c7 h1:DpOJ2HYzCv8LZP15IdmG+YdwD2luVPHITV96TkirNBM=
github.com/mitchellh/go-wordwrap v0.0.0-20150314170334-ad45545899c7/go.mod h1:ZXFpozHsX6DPmq2I0TCekCxypsnAUbP2oI0UX1GXzOo=
github.com/nsf/termbox-go v0.0.0-20180613055208-5c94acc5e6eb h1:YahEjAGkJtCrkqgVHhX6n8ZX+CZ3hDRL9fjLYugLfSs=
github.com/nsf/termbox-go v0.0.0-20180613055208-5c94acc5e6eb/go.mod h1:IuKpRQcYE1Tfu+oAQqaLisqDeXgjyyltCfsaoYN18NQ=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/shirou/gopsutil v2.18.11+incompatible h1:PMFTKnFTr/YTRW5rbLK4vWALV3a+IGXse5nvhSjztmg=
Expand Down
102 changes: 59 additions & 43 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ import (
"github.com/cjbassi/gotop/colorschemes"
"github.com/cjbassi/gotop/src/logging"
w "github.com/cjbassi/gotop/src/widgets"
ui "github.com/cjbassi/termui"
docopt "github.com/docopt/docopt.go"
ui "github.com/gizak/termui"
)

var version = "1.7.1"
Expand All @@ -38,6 +38,8 @@ var (
configDir = appdir.New("gotop").UserConfig()
logPath = filepath.Join(configDir, "errors.log")
stderrLogger = log.New(os.Stderr, "", 0)
termWidth int
termHeight int

cpu *w.CPU
mem *w.Mem
Expand All @@ -46,6 +48,7 @@ var (
disk *w.Disk
temp *w.Temp
help *w.HelpMenu
grid *ui.Grid
)

func cliArguments() error {
Expand Down Expand Up @@ -137,41 +140,46 @@ func getCustomColorscheme(name string) (colorschemes.Colorscheme, error) {
}

func setupGrid() {
ui.Body.Cols = 12
ui.Body.Rows = 12
grid = ui.NewGrid()
grid.SetRect(0, 0, termWidth, termHeight)

if minimal {
ui.Body.Set(0, 0, 12, 6, cpu)
ui.Body.Set(0, 6, 6, 12, mem)
ui.Body.Set(6, 6, 12, 12, proc)
grid.Set(
ui.NewRow(1.0/2, cpu),
ui.NewRow(1.0/2,
ui.NewCol(1.0/2, mem),
ui.NewCol(1.0/2, proc),
),
)
} else {
ui.Body.Set(0, 0, 12, 4, cpu)

ui.Body.Set(0, 4, 4, 6, disk)
ui.Body.Set(0, 6, 4, 8, temp)
ui.Body.Set(4, 4, 12, 8, mem)

ui.Body.Set(0, 8, 6, 12, net)
ui.Body.Set(6, 8, 12, 12, proc)
grid.Set(
ui.NewRow(1.0/3, cpu),
ui.NewRow(1.0/3,
ui.NewCol(1.0/3,
ui.NewRow(1.0/2, disk),
ui.NewRow(1.0/2, temp),
),
ui.NewCol(2.0/3, mem),
),
ui.NewRow(1.0/3,
ui.NewCol(1.0/2, net),
ui.NewCol(1.0/2, proc),
),
)
}
}

func termuiColors() {
ui.Theme.Fg = ui.Color(colorscheme.Fg)
ui.Theme.Bg = ui.Color(colorscheme.Bg)
ui.Theme.LabelFg = ui.Color(colorscheme.BorderLabel)
ui.Theme.LabelBg = ui.Color(colorscheme.Bg)
ui.Theme.BorderFg = ui.Color(colorscheme.BorderLine)
ui.Theme.BorderBg = ui.Color(colorscheme.Bg)

ui.Theme.TableCursor = ui.Color(colorscheme.ProcCursor)
ui.Theme.Sparkline = ui.Color(colorscheme.Sparkline)
ui.Theme.GaugeColor = ui.Color(colorscheme.DiskBar)
ui.Theme.Default = ui.AttrPair{ui.Attribute(colorscheme.Fg), ui.Attribute(colorscheme.Bg)}
ui.Theme.Block.Title = ui.AttrPair{ui.Attribute(colorscheme.BorderLabel), ui.Attribute(colorscheme.Bg)}
ui.Theme.Block.Border = ui.AttrPair{ui.Attribute(colorscheme.BorderLine), ui.Attribute(colorscheme.Bg)}
}

func widgetColors() {
mem.LineColor["Main"] = ui.Color(colorscheme.MainMem)
mem.LineColor["Swap"] = ui.Color(colorscheme.SwapMem)
mem.LineColor["Main"] = ui.Attribute(colorscheme.MainMem)
mem.LineColor["Swap"] = ui.Attribute(colorscheme.SwapMem)

proc.CursorColor = ui.Attribute(colorscheme.ProcCursor)

var keys []string
for key := range cpu.Data {
Expand All @@ -185,13 +193,18 @@ func widgetColors() {
i = 0
}
c := colorscheme.CPULines[i]
cpu.LineColor[v] = ui.Color(c)
cpu.LineColor[v] = ui.Attribute(c)
i++
}

if !minimal {
temp.TempLow = ui.Color(colorscheme.TempLow)
temp.TempHigh = ui.Color(colorscheme.TempHigh)
temp.TempLow = ui.Attribute(colorscheme.TempLow)
temp.TempHigh = ui.Attribute(colorscheme.TempHigh)

net.Lines[0].LineColor = ui.Attribute(colorscheme.Sparkline)
net.Lines[0].TitleColor = ui.Attribute(colorscheme.BorderLabel)
net.Lines[1].LineColor = ui.Attribute(colorscheme.Sparkline)
net.Lines[1].TitleColor = ui.Attribute(colorscheme.BorderLabel)
}
}

Expand Down Expand Up @@ -246,7 +259,7 @@ func eventLoop() {
return
case <-drawTicker:
if !helpVisible {
ui.Render(ui.Body)
ui.Render(grid)
}
case e := <-uiEvents:
switch e.ID {
Expand All @@ -258,7 +271,7 @@ func eventLoop() {
ui.Clear()
ui.Render(help)
} else {
ui.Render(ui.Body)
ui.Render(grid)
}
case "h":
if !helpVisible {
Expand All @@ -279,27 +292,27 @@ func eventLoop() {
case "<Escape>":
if helpVisible {
helpVisible = false
ui.Render(ui.Body)
ui.Render(grid)
}
case "<Resize>":
payload := e.Payload.(ui.Resize)
ui.Body.Width, ui.Body.Height = payload.Width, payload.Height
ui.Body.Resize()
grid.SetRect(0, 0, payload.Width, payload.Height)
help.Resize(payload.Width, payload.Height)
ui.Clear()
if helpVisible {
ui.Render(help)
} else {
ui.Render(ui.Body)
ui.Render(grid)
}

case "<MouseLeft>":
payload := e.Payload.(ui.Mouse)
proc.Click(payload.X, payload.Y)
ui.Render(proc)
case "<MouseWheelUp>", "<Up>", "k":
case "k", "<Up>", "<MouseWheelUp>":
proc.Up()
ui.Render(proc)
case "<MouseWheelDown>", "<Down>", "j":
case "j", "<Down>", "<MouseWheelDown>":
proc.Down()
ui.Render(proc)
case "g", "<Home>":
Expand Down Expand Up @@ -373,20 +386,23 @@ func main() {
stderrLogger.Fatalf("failed to parse cli args: %v", err)
}

termuiColors() // need to do this before initializing widgets so that they can inherit the colors
initWidgets()
widgetColors()
help = w.NewHelpMenu()

if err := ui.Init(); err != nil {
stderrLogger.Fatalf("failed to initialize termui: %v", err)
}
defer ui.Close()

logging.StderrToLogfile(lf)

termWidth, termHeight = ui.TerminalSize()

termuiColors() // need to do this before initializing widgets so that they can inherit the colors
initWidgets()
widgetColors()
help = w.NewHelpMenu()
help.Resize(termWidth, termHeight)

setupGrid()
ui.Render(ui.Body)
ui.Render(grid)

eventLoop()
}
36 changes: 20 additions & 16 deletions src/termui/linegraph.go
Original file line number Diff line number Diff line change
@@ -1,45 +1,45 @@
package termui

import (
"image"
"sort"

drawille "github.com/cjbassi/drawille-go"
. "github.com/gizak/termui"
)

// LineGraph implements a line graph of data points.
type LineGraph struct {
*Block
Data map[string][]float64
LineColor map[string]Color
LineColor map[string]Attribute
Zoom int
Labels map[string]string

DefaultLineColor Color
DefaultLineColor Attribute
}

// NewLineGraph returns a new LineGraph with current theme.
func NewLineGraph() *LineGraph {
return &LineGraph{
Block: NewBlock(),
Data: make(map[string][]float64),
LineColor: make(map[string]Color),
LineColor: make(map[string]Attribute),
Labels: make(map[string]string),
Zoom: 5,

DefaultLineColor: Theme.LineGraph,
}
}

// Buffer implements Bufferer interface.
func (self *LineGraph) Buffer() *Buffer {
buf := self.Block.Buffer()
func (self *LineGraph) Draw(buf *Buffer) {
self.Block.Draw(buf)
// we render each data point on to the canvas then copy over the braille to the buffer at the end
// fyi braille characters have 2x4 dots for each character
c := drawille.NewCanvas()
// used to keep track of the braille colors until the end when we render the braille to the buffer
colors := make([][]Color, self.X+2)
colors := make([][]Attribute, self.Inner.Dx()+2)
for i := range colors {
colors[i] = make([]Color, self.Y+2)
colors[i] = make([]Attribute, self.Inner.Dy()+2)
}

// sort the series so that overlapping data will overlap the same way each time
Expand All @@ -64,8 +64,8 @@ func (self *LineGraph) Buffer() *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.X + 1) * 2) - 1 - (((len(seriesData) - 1) - i) * self.Zoom)
y := ((self.Y + 1) * 4) - 1 - int((float64((self.Y)*4)-1)*(seriesData[i]/100))
x := ((self.Inner.Dx() + 1) * 2) - 1 - (((len(seriesData) - 1) - i) * self.Zoom)
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 {
Expand Down Expand Up @@ -98,15 +98,18 @@ func (self *LineGraph) Buffer() *Buffer {
continue
}
if char != 10240 { // empty braille character
buf.SetCell(x, y, Cell{char, colors[x][y], self.Bg})
buf.SetCell(
Cell{char, AttrPair{colors[x][y], -1}},
image.Pt(self.Inner.Min.X+x-1, self.Inner.Min.Y+y-1),
)
}
}
}
}

// renders key/label ontop
for i, seriesName := range seriesList {
if i+2 > self.Y {
if i+2 > self.Inner.Dy() {
continue
}
seriesLineColor, ok := self.LineColor[seriesName]
Expand All @@ -118,11 +121,12 @@ func (self *LineGraph) Buffer() *Buffer {
str := seriesName + " " + self.Labels[seriesName]
for k, char := range str {
if char != ' ' {
buf.SetCell(3+k, i+2, Cell{char, seriesLineColor, self.Bg})
buf.SetCell(
Cell{char, AttrPair{seriesLineColor, -1}},
image.Pt(self.Inner.Min.X+2+k, self.Inner.Min.Y+i+1),
)
}
}

}

return buf
}
Loading

0 comments on commit 2e48524

Please sign in to comment.