diff options
author | Caleb Bassi <calebjbassi@gmail.com> | 2018-02-23 21:15:38 -0800 |
---|---|---|
committer | Caleb Bassi <calebjbassi@gmail.com> | 2018-02-23 21:15:38 -0800 |
commit | a2fd8dc4fa3470bd4f286a7923f82a870a7dcecc (patch) | |
tree | fcee891c7e84d886d8d9fdd186f01729cd3f76f0 /termui | |
parent | b06502d7f2a936a51ab7c7e714b513e482f11cd4 (diff) |
Moar code cleanup
Diffstat (limited to 'termui')
-rw-r--r-- | termui/gauge.go | 3 | ||||
-rw-r--r-- | termui/linegraph.go | 12 | ||||
-rw-r--r-- | termui/list.go | 6 | ||||
-rw-r--r-- | termui/sparkline.go | 4 | ||||
-rw-r--r-- | termui/table.go | 30 |
5 files changed, 31 insertions, 24 deletions
diff --git a/termui/gauge.go b/termui/gauge.go index 7e6ab7e..11235f4 100644 --- a/termui/gauge.go +++ b/termui/gauge.go @@ -36,8 +36,9 @@ func (g *Gauge) Buffer() *Buffer { // plot percentage s := strconv.Itoa(g.Percent) + "%" + g.Description - y := (g.Y + 1) / 2 s = MaxString(s, g.X) + + y := (g.Y + 1) / 2 x := ((g.X - len(s)) + 1) / 2 for i, char := range s { diff --git a/termui/linegraph.go b/termui/linegraph.go index acf35fc..7a88566 100644 --- a/termui/linegraph.go +++ b/termui/linegraph.go @@ -31,6 +31,7 @@ func NewLineGraph() *LineGraph { func (lc *LineGraph) Buffer() *Buffer { buf := lc.Block.Buffer() c := drawille.NewCanvas() + // used to keep track of colors but not write them to the buffer until the end colors := make([][]Color, lc.X+2) for i := range colors { colors[i] = make([]Color, lc.Y+2) @@ -52,6 +53,7 @@ func (lc *LineGraph) Buffer() *Buffer { seriesLineColor = lc.DefaultLineColor } + // coordinates of last point lastY, lastX := -1, -1 // assign colors to `colors` and lines/points to the canvas for i := len(seriesData) - 1; i >= 0; i-- { @@ -63,7 +65,7 @@ func (lc *LineGraph) Buffer() *Buffer { } if lastY == -1 { // if this is the first point c.Set(x, y) - colors[x/2][y/4] = seriesLineColor + colors[x/2][y/4] = seriesLineColor // divide by 2 and 4 due to 2x4 dots in braille characters } else { c.DrawLine(lastX, lastY, x, y) for _, p := range drawille.Line(lastX, lastY, x, y) { @@ -73,20 +75,20 @@ func (lc *LineGraph) Buffer() *Buffer { lastX, lastY = x, y } - // copy drawille and colors to buffer + // copy braille and colors to buffer for y, line := range c.Rows(c.MinX(), c.MinY(), c.MaxX(), c.MaxY()) { for x, char := range line { - x /= 3 + x /= 3 // idk why but it works if x == 0 { continue } - if char != 10240 { + if char != 10240 { // empty braille character buf.SetCell(x, y, Cell{char, colors[x][y], lc.Bg}) } } } - // Render key + // Render key ontop, but let braille be drawn between words str := fmt.Sprintf("%s %3.0f%%", seriesName, seriesData[len(seriesData)-1]) for k, char := range str { if char != ' ' { diff --git a/termui/list.go b/termui/list.go index f011239..f1908ed 100644 --- a/termui/list.go +++ b/termui/list.go @@ -29,12 +29,14 @@ func (bc *List) Buffer() *Buffer { if y+1 > bc.Y { break } + fg := Theme.TempLow if bc.Data[y] >= bc.Threshold { fg = Theme.TempHigh } - r := MaxString(text, (bc.X - 4)) - buf.SetString(1, y+1, r, Theme.Fg, bc.Bg) + + s := MaxString(text, (bc.X - 4)) + buf.SetString(1, y+1, s, Theme.Fg, bc.Bg) buf.SetString(bc.X-2, y+1, fmt.Sprintf("%dC", bc.Data[y]), fg, bc.Bg) } diff --git a/termui/sparkline.go b/termui/sparkline.go index 9f3b9ee..c9bcb0d 100644 --- a/termui/sparkline.go +++ b/termui/sparkline.go @@ -56,9 +56,9 @@ func (sl *Sparklines) Buffer() *Buffer { buf.SetString(1, title1Y, title1, line.TitleColor|AttrBold, sl.Bg) buf.SetString(1, title2Y, title2, line.TitleColor|AttrBold, sl.Bg) - // sparkline sparkY := (sl.Y / lc) * (i + 1) - // finds max used for relative heights + + // finds max data in current view used for relative heights max := 1 for i := len(line.Data) - 1; i >= 0 && sl.X-((len(line.Data)-1)-i) >= 1; i-- { if line.Data[i] > max { diff --git a/termui/table.go b/termui/table.go index 172ad90..883cda7 100644 --- a/termui/table.go +++ b/termui/table.go @@ -8,15 +8,17 @@ import ( // Table tracks all the attributes of a Table instance type Table struct { *Block - Header []string - Rows [][]string - Fg Color - Bg Color - Cursor Color + Header []string + Rows [][]string + Fg Color + Bg Color + Cursor Color + // the unique column used to keep track of which process we're one + // either the PID column or Command column depending on if processes are grouped UniqueCol int - pid string - selected int - topRow int + pid string // used to keep the cursor on the correct process after each update + selected int // selected row + topRow int // top process in current view } // NewTable returns a new Table instance @@ -32,7 +34,7 @@ func NewTable() *Table { } } -// Buffer ... +// Buffer implements the Bufferer interface. func (t *Table) Buffer() *Buffer { buf := t.Block.Buffer() @@ -58,9 +60,9 @@ func (t *Table) Buffer() *Buffer { // total width requires by all 4 columns contentWidth := gap + cw[0] + gap + cw[1] + gap + cw[2] + gap + cw[3] + gap - render := 4 // number of columns to iterate through + render := 4 // number of columns to render based on the terminal width - // removes CPU and MEM if there isn't enough room + // removes CPU and MEM columns if there isn't enough room if t.X < (contentWidth - gap - cw[3]) { render = 2 } else if t.X < contentWidth { @@ -75,8 +77,6 @@ func (t *Table) Buffer() *Buffer { } // prints each row - // for y, row := range t.Rows { - // for y := t.topRow; y <= t.topRow+t.Y; y++ { for rowNum := t.topRow; rowNum < t.topRow+t.Y-1 && rowNum < len(t.Rows); rowNum++ { row := t.Rows[rowNum] y := (rowNum + 2) - t.topRow @@ -92,7 +92,7 @@ func (t *Table) Buffer() *Buffer { t.selected = rowNum } - // prints each string + // prints each col of the row for i := 0; i < render; i++ { r := MaxString(row[i], t.X-6) buf.SetString(cp[i], y, r, t.Fg, bg) @@ -104,6 +104,7 @@ func (t *Table) Buffer() *Buffer { //////////////////////////////////////////////////////////////////////////////// +// calcPos is used to calculate the cursor position and where in the process list we are located. func (t *Table) calcPos() { t.pid = "" @@ -171,6 +172,7 @@ func (t *Table) Click(x, y int) { } } +// Kill kills process or group of processes. func (t *Table) Kill() { t.pid = "" command := "kill" |