summaryrefslogtreecommitdiffstats
path: root/termui
diff options
context:
space:
mode:
authorCaleb Bassi <calebjbassi@gmail.com>2018-02-24 21:35:57 -0800
committerCaleb Bassi <calebjbassi@gmail.com>2018-02-24 21:35:57 -0800
commit21aed4b1eafd6cd28b9deef8084c009c38dd8f5f (patch)
tree34e5b2d81d3b77df7f812021044d203e7a2f4e46 /termui
parenta2fd8dc4fa3470bd4f286a7923f82a870a7dcecc (diff)
Removed gotop specific stuff from termui
Diffstat (limited to 'termui')
-rw-r--r--termui/colors.go8
-rw-r--r--termui/gauge.go16
-rw-r--r--termui/list.go44
-rw-r--r--termui/sparkline.go1
-rw-r--r--termui/table.go162
5 files changed, 86 insertions, 145 deletions
diff --git a/termui/colors.go b/termui/colors.go
index d8dc8bf..2fdacd3 100644
--- a/termui/colors.go
+++ b/termui/colors.go
@@ -28,9 +28,7 @@ var DefaultTheme = Colorscheme{
Sparkline: 4,
LineGraph: -1,
TableCursor: 4,
- BarColor: 7,
- TempLow: 2,
- TempHigh: 1,
+ GaugeColor: 7,
}
// A Colorscheme represents the current look-and-feel of the dashboard.
@@ -46,7 +44,5 @@ type Colorscheme struct {
Sparkline Color
LineGraph Color
TableCursor Color
- BarColor Color
- TempLow Color
- TempHigh Color
+ GaugeColor Color
}
diff --git a/termui/gauge.go b/termui/gauge.go
index 11235f4..5667686 100644
--- a/termui/gauge.go
+++ b/termui/gauge.go
@@ -7,18 +7,16 @@ import (
// Gauge is a progress bar like widget.
type Gauge struct {
*Block
- Percent int
- BarColor Color
- PercentColor Color
- Description string
+ Percent int
+ GaugeColor Color
+ Description string
}
// NewGauge return a new gauge with current theme.
func NewGauge() *Gauge {
return &Gauge{
- Block: NewBlock(),
- PercentColor: Theme.Fg,
- BarColor: Theme.BarColor,
+ Block: NewBlock(),
+ GaugeColor: Theme.GaugeColor,
}
}
@@ -30,7 +28,7 @@ func (g *Gauge) Buffer() *Buffer {
width := g.Percent * g.X / 100
for y := 1; y <= g.Y; y++ {
for x := 1; x <= width; x++ {
- buf.SetCell(x, y, Cell{' ', g.BarColor, g.BarColor})
+ buf.SetCell(x, y, Cell{' ', g.GaugeColor, g.GaugeColor})
}
}
@@ -45,7 +43,7 @@ func (g *Gauge) Buffer() *Buffer {
bg := g.Bg
fg := g.Fg
if x+i < width {
- fg = g.BarColor
+ fg = g.GaugeColor
bg = AttrReverse
}
buf.SetCell(1+x+i, y, Cell{char, fg, bg})
diff --git a/termui/list.go b/termui/list.go
deleted file mode 100644
index f1908ed..0000000
--- a/termui/list.go
+++ /dev/null
@@ -1,44 +0,0 @@
-package termui
-
-import (
- "fmt"
-)
-
-// BarChart creates multiple bars in a widget:
-type List struct {
- *Block
- TextColor Color
- Data []int
- DataLabels []string
- Threshold int
-}
-
-// NewBarChart returns a new *BarChart with current theme.
-func NewList() *List {
- return &List{
- Block: NewBlock(),
- TextColor: Theme.Fg,
- }
-}
-
-// Buffer implements Bufferer interface.
-func (bc *List) Buffer() *Buffer {
- buf := bc.Block.Buffer()
-
- for y, text := range bc.DataLabels {
- if y+1 > bc.Y {
- break
- }
-
- fg := Theme.TempLow
- if bc.Data[y] >= bc.Threshold {
- fg = Theme.TempHigh
- }
-
- 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)
- }
-
- return buf
-}
diff --git a/termui/sparkline.go b/termui/sparkline.go
index c9bcb0d..5de952f 100644
--- a/termui/sparkline.go
+++ b/termui/sparkline.go
@@ -8,7 +8,6 @@ type Sparkline struct {
Title1 string
Title2 string
TitleColor Color
- Total int
LineColor Color
}
diff --git a/termui/table.go b/termui/table.go
index 883cda7..d18bdc6 100644
--- a/termui/table.go
+++ b/termui/table.go
@@ -1,101 +1,103 @@
package termui
import (
- "os/exec"
"strings"
)
// Table tracks all the attributes of a Table instance
type Table struct {
*Block
- 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 // used to keep the cursor on the correct process after each update
- selected int // selected row
- topRow int // top process in current view
+ Header []string
+ Rows [][]string
+ ColWidths []int
+ Cp []int // column position
+ Gap int // gap between columns
+ Cursor Color
+ UniqueCol int // the column used to identify the selected item
+ SelectedItem string // used to keep the cursor on the correct item if the data changes
+ SelectedRow int
+ TopRow int // used to indicate where in the table we are scrolled at
+ ColResizer func()
}
// NewTable returns a new Table instance
func NewTable() *Table {
- return &Table{
- Block: NewBlock(),
- Fg: Theme.Fg,
- Bg: Theme.Bg,
- Cursor: Theme.TableCursor,
- selected: 0,
- topRow: 0,
- UniqueCol: 0,
+ t := &Table{
+ Block: NewBlock(),
+ Cursor: Theme.TableCursor,
+ SelectedRow: 0,
+ TopRow: 0,
+ UniqueCol: 0,
}
+ t.ColResizer = t.ColResize
+ return t
}
-// Buffer implements the Bufferer interface.
-func (t *Table) Buffer() *Buffer {
- buf := t.Block.Buffer()
-
- if t.topRow > len(t.Rows)-(t.Y-1) {
- t.topRow = len(t.Rows) - (t.Y - 1)
- }
-
+// ColResize is the default column resizer, but can be overriden.
+func (t *Table) ColResize() {
// calculate gap size based on total width
- gap := 3
+ t.Gap = 3
if t.X < 50 {
- gap = 1
+ t.Gap = 1
} else if t.X < 75 {
- gap = 2
+ t.Gap = 2
}
- cw := []int{5, 10, 4, 4} // cellWidth
- cp := []int{ // cellPosition
- gap,
- gap + cw[0] + gap,
- t.X - gap - cw[3] - gap - cw[2],
- t.X - gap - cw[3],
+ cur := 0
+ for _, w := range t.ColWidths {
+ cur += t.Gap
+ t.Cp = append(t.Cp, cur)
+ cur += w
}
+}
- // 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 render based on the terminal width
+// Buffer implements the Bufferer interface.
+func (t *Table) Buffer() *Buffer {
+ buf := t.Block.Buffer()
- // 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 {
- cp[2] = cp[3]
- render = 3
+ // makes sure there isn't a gap at the bottom of the table view
+ if t.TopRow > len(t.Rows)-(t.Y-1) {
+ t.TopRow = len(t.Rows) - (t.Y - 1)
}
+ t.ColResizer()
+
// print header
- for i := 0; i < render; i++ {
+ // for i := 0; i < render; i++ {
+ for i, width := range t.ColWidths {
+ if width == 0 {
+ break
+ }
r := MaxString(t.Header[i], t.X-6)
- buf.SetString(cp[i], 1, r, t.Fg|AttrBold, t.Bg)
+ buf.SetString(t.Cp[i], 1, r, t.Fg|AttrBold, t.Bg)
}
// prints each row
- for rowNum := t.topRow; rowNum < t.topRow+t.Y-1 && rowNum < len(t.Rows); rowNum++ {
+ for rowNum := t.TopRow; rowNum < t.TopRow+t.Y-1 && rowNum < len(t.Rows); rowNum++ {
row := t.Rows[rowNum]
- y := (rowNum + 2) - t.topRow
+ y := (rowNum + 2) - t.TopRow
// cursor
bg := t.Bg
- if (t.pid == "" && rowNum == t.selected) || (t.pid != "" && t.pid == row[t.UniqueCol]) {
+ if (t.SelectedItem == "" && rowNum == t.SelectedRow) || (t.SelectedItem != "" && t.SelectedItem == row[t.UniqueCol]) {
bg = t.Cursor
- for i := 0; i < render; i++ {
+ for _, width := range t.ColWidths {
+ if width == 0 {
+ break
+ }
buf.SetString(1, y, strings.Repeat(" ", t.X), t.Fg, bg)
}
- t.pid = row[t.UniqueCol]
- t.selected = rowNum
+ t.SelectedItem = row[t.UniqueCol]
+ t.SelectedRow = rowNum
}
// prints each col of the row
- for i := 0; i < render; i++ {
+ for i, width := range t.ColWidths {
+ if width == 0 {
+ break
+ }
r := MaxString(row[i], t.X-6)
- buf.SetString(cp[i], y, r, t.Fg, bg)
+ buf.SetString(t.Cp[i], y, r, t.Fg, bg)
}
}
@@ -103,63 +105,64 @@ func (t *Table) Buffer() *Buffer {
}
////////////////////////////////////////////////////////////////////////////////
+// Cursor movement
// calcPos is used to calculate the cursor position and where in the process list we are located.
func (t *Table) calcPos() {
- t.pid = ""
+ t.SelectedItem = ""
- if t.selected < 0 {
- t.selected = 0
+ if t.SelectedRow < 0 {
+ t.SelectedRow = 0
}
- if t.selected < t.topRow {
- t.topRow = t.selected
+ if t.SelectedRow < t.TopRow {
+ t.TopRow = t.SelectedRow
}
- if t.selected > len(t.Rows)-1 {
- t.selected = len(t.Rows) - 1
+ if t.SelectedRow > len(t.Rows)-1 {
+ t.SelectedRow = len(t.Rows) - 1
}
- if t.selected > t.topRow+(t.Y-2) {
- t.topRow = t.selected - (t.Y - 2)
+ if t.SelectedRow > t.TopRow+(t.Y-2) {
+ t.TopRow = t.SelectedRow - (t.Y - 2)
}
}
func (t *Table) Up() {
- t.selected -= 1
+ t.SelectedRow -= 1
t.calcPos()
}
func (t *Table) Down() {
- t.selected += 1
+ t.SelectedRow += 1
t.calcPos()
}
func (t *Table) Top() {
- t.selected = 0
+ t.SelectedRow = 0
t.calcPos()
}
func (t *Table) Bottom() {
- t.selected = len(t.Rows) - 1
+ t.SelectedRow = len(t.Rows) - 1
t.calcPos()
}
func (t *Table) HalfPageUp() {
- t.selected = t.selected - (t.Y-2)/2
+ t.SelectedRow = t.SelectedRow - (t.Y-2)/2
t.calcPos()
}
func (t *Table) HalfPageDown() {
- t.selected = t.selected + (t.Y-2)/2
+ t.SelectedRow = t.SelectedRow + (t.Y-2)/2
t.calcPos()
}
func (t *Table) PageUp() {
- t.selected -= (t.Y - 2)
+ t.SelectedRow -= (t.Y - 2)
t.calcPos()
}
func (t *Table) PageDown() {
- t.selected += (t.Y - 2)
+ t.SelectedRow += (t.Y - 2)
t.calcPos()
}
@@ -167,18 +170,7 @@ func (t *Table) Click(x, y int) {
x = x - t.XOffset
y = y - t.YOffset
if (x > 0 && x <= t.X) && (y > 0 && y <= t.Y) {
- t.selected = (t.topRow + y) - 2
+ t.SelectedRow = (t.TopRow + y) - 2
t.calcPos()
}
}
-
-// Kill kills process or group of processes.
-func (t *Table) Kill() {
- t.pid = ""
- command := "kill"
- if t.UniqueCol == 1 {
- command = "pkill"
- }
- cmd := exec.Command(command, t.Rows[t.selected][t.UniqueCol])
- cmd.Start()
-}