Change methods to use 'self' keyword

This commit is contained in:
Caleb Bassi 2018-03-27 14:27:23 -07:00
parent 0aa09f4fc6
commit dc8cc5aa4d
14 changed files with 354 additions and 354 deletions

View File

@ -32,69 +32,69 @@ func NewBlock() *Block {
}
}
func (b *Block) drawBorder(buf *Buffer) {
x := b.X + 1
y := b.Y + 1
func (self *Block) drawBorder(buf *Buffer) {
x := self.X + 1
y := self.Y + 1
// draw lines
buf.Merge(NewFilledBuffer(0, 0, x, 1, Cell{HORIZONTAL_LINE, b.BorderFg, b.BorderBg}))
buf.Merge(NewFilledBuffer(0, y, x, y+1, Cell{HORIZONTAL_LINE, b.BorderFg, b.BorderBg}))
buf.Merge(NewFilledBuffer(0, 0, 1, y+1, Cell{VERTICAL_LINE, b.BorderFg, b.BorderBg}))
buf.Merge(NewFilledBuffer(x, 0, x+1, y+1, Cell{VERTICAL_LINE, b.BorderFg, b.BorderBg}))
buf.Merge(NewFilledBuffer(0, 0, x, 1, Cell{HORIZONTAL_LINE, self.BorderFg, self.BorderBg}))
buf.Merge(NewFilledBuffer(0, y, x, y+1, Cell{HORIZONTAL_LINE, self.BorderFg, self.BorderBg}))
buf.Merge(NewFilledBuffer(0, 0, 1, y+1, Cell{VERTICAL_LINE, self.BorderFg, self.BorderBg}))
buf.Merge(NewFilledBuffer(x, 0, x+1, y+1, Cell{VERTICAL_LINE, self.BorderFg, self.BorderBg}))
// draw corners
buf.SetCell(0, 0, Cell{TOP_LEFT, b.BorderFg, b.BorderBg})
buf.SetCell(x, 0, Cell{TOP_RIGHT, b.BorderFg, b.BorderBg})
buf.SetCell(0, y, Cell{BOTTOM_LEFT, b.BorderFg, b.BorderBg})
buf.SetCell(x, y, Cell{BOTTOM_RIGHT, b.BorderFg, b.BorderBg})
buf.SetCell(0, 0, Cell{TOP_LEFT, self.BorderFg, self.BorderBg})
buf.SetCell(x, 0, Cell{TOP_RIGHT, self.BorderFg, self.BorderBg})
buf.SetCell(0, y, Cell{BOTTOM_LEFT, self.BorderFg, self.BorderBg})
buf.SetCell(x, y, Cell{BOTTOM_RIGHT, self.BorderFg, self.BorderBg})
}
func (b *Block) drawLabel(buf *Buffer) {
r := MaxString(b.Label, (b.X-3)-1)
buf.SetString(3, 0, r, b.LabelFg, b.LabelBg)
if b.Label == "" {
func (self *Block) drawLabel(buf *Buffer) {
r := MaxString(self.Label, (self.X-3)-1)
buf.SetString(3, 0, r, self.LabelFg, self.LabelBg)
if self.Label == "" {
return
}
c := Cell{' ', b.Fg, b.Bg}
c := Cell{' ', self.Fg, self.Bg}
buf.SetCell(2, 0, c)
if len(b.Label)+3 < b.X {
buf.SetCell(len(b.Label)+3, 0, c)
if len(self.Label)+3 < self.X {
buf.SetCell(len(self.Label)+3, 0, c)
} else {
buf.SetCell(b.X-1, 0, c)
buf.SetCell(self.X-1, 0, c)
}
}
// Resize computes Height, Width, XOffset, and YOffset given terminal dimensions.
func (b *Block) Resize(termWidth, termHeight, termCols, termRows int) {
b.X = int((float64(b.Grid.Dx())/float64(termCols))*float64(termWidth)) - 2
b.Y = int((float64(b.Grid.Dy())/float64(termRows))*float64(termHeight)) - 2
b.XOffset = int((float64(b.Grid.Min.X) / float64(termCols)) * float64(termWidth))
b.YOffset = int((float64(b.Grid.Min.Y) / float64(termRows)) * float64(termHeight))
func (self *Block) Resize(termWidth, termHeight, termCols, termRows int) {
self.X = int((float64(self.Grid.Dx())/float64(termCols))*float64(termWidth)) - 2
self.Y = int((float64(self.Grid.Dy())/float64(termRows))*float64(termHeight)) - 2
self.XOffset = int((float64(self.Grid.Min.X) / float64(termCols)) * float64(termWidth))
self.YOffset = int((float64(self.Grid.Min.Y) / float64(termRows)) * float64(termHeight))
}
// SetGrid create a rectangle representing the block's dimensions in the grid.
func (b *Block) SetGrid(c0, r0, c1, r1 int) {
b.Grid = image.Rect(c0, r0, c1, r1)
func (self *Block) SetGrid(c0, r0, c1, r1 int) {
self.Grid = image.Rect(c0, r0, c1, r1)
}
// GetXOffset implements Bufferer interface.
func (b *Block) GetXOffset() int {
return b.XOffset
func (self *Block) GetXOffset() int {
return self.XOffset
}
// GetYOffset implements Bufferer interface.
func (b *Block) GetYOffset() int {
return b.YOffset
func (self *Block) GetYOffset() int {
return self.YOffset
}
// Buffer implements Bufferer interface and draws background, border, and borderlabel.
func (b *Block) Buffer() *Buffer {
func (self *Block) Buffer() *Buffer {
buf := NewBuffer()
buf.SetAreaXY(b.X+2, b.Y+2)
buf.Fill(Cell{' ', ColorDefault, b.Bg})
buf.SetAreaXY(self.X+2, self.Y+2)
buf.Fill(Cell{' ', ColorDefault, self.Bg})
b.drawBorder(buf)
b.drawLabel(buf)
self.drawBorder(buf)
self.drawLabel(buf)
return buf
}

View File

@ -40,60 +40,60 @@ func NewFilledBuffer(x0, y0, x1, y1 int, c Cell) *Buffer {
}
// SetCell assigns a Cell to (x,y).
func (b *Buffer) SetCell(x, y int, c Cell) {
b.CellMap[image.Pt(x, y)] = c
func (self *Buffer) SetCell(x, y int, c Cell) {
self.CellMap[image.Pt(x, y)] = c
}
// SetString assigns a string to a Buffer starting at (x,y).
func (b *Buffer) SetString(x, y int, s string, fg, bg Color) {
func (self *Buffer) SetString(x, y int, s string, fg, bg Color) {
for i, char := range s {
b.SetCell(x+i, y, Cell{char, fg, bg})
self.SetCell(x+i, y, Cell{char, fg, bg})
}
}
// At returns the cell at (x,y).
func (b *Buffer) At(x, y int) Cell {
return b.CellMap[image.Pt(x, y)]
func (self *Buffer) At(x, y int) Cell {
return self.CellMap[image.Pt(x, y)]
}
// SetArea assigns a new rect area to Buffer b.
func (b *Buffer) SetArea(r image.Rectangle) {
b.Area.Max = r.Max
b.Area.Min = r.Min
// SetArea assigns a new rect area to self.
func (self *Buffer) SetArea(r image.Rectangle) {
self.Area.Max = r.Max
self.Area.Min = r.Min
}
// SetAreaXY sets the Buffer bounds from (0,0) to (x,y).
func (b *Buffer) SetAreaXY(x, y int) {
b.Area.Min.Y = 0
b.Area.Min.X = 0
b.Area.Max.Y = y
b.Area.Max.X = x
func (self *Buffer) SetAreaXY(x, y int) {
self.Area.Min.Y = 0
self.Area.Min.X = 0
self.Area.Max.Y = y
self.Area.Max.X = x
}
// Merge merges the given buffers onto the current Buffer.
func (b *Buffer) Merge(bs ...*Buffer) {
func (self *Buffer) Merge(bs ...*Buffer) {
for _, buf := range bs {
for p, c := range buf.CellMap {
b.SetCell(p.X, p.Y, c)
self.SetCell(p.X, p.Y, c)
}
b.SetArea(b.Area.Union(buf.Area))
self.SetArea(self.Area.Union(buf.Area))
}
}
// MergeWithOffset merges a Buffer onto another with an offset.
func (b *Buffer) MergeWithOffset(buf *Buffer, xOffset, yOffset int) {
func (self *Buffer) MergeWithOffset(buf *Buffer, xOffset, yOffset int) {
for p, c := range buf.CellMap {
b.SetCell(p.X+xOffset, p.Y+yOffset, c)
self.SetCell(p.X+xOffset, p.Y+yOffset, c)
}
rect := image.Rect(xOffset, yOffset, buf.Area.Max.X+xOffset, buf.Area.Max.Y+yOffset)
b.SetArea(b.Area.Union(rect))
self.SetArea(self.Area.Union(rect))
}
// Fill fills the Buffer with a Cell.
func (b *Buffer) Fill(c Cell) {
for x := b.Area.Min.X; x < b.Area.Max.X; x++ {
for y := b.Area.Min.Y; y < b.Area.Max.Y; y++ {
b.SetCell(x, y, c)
func (self *Buffer) Fill(c Cell) {
for x := self.Area.Min.X; x < self.Area.Max.X; x++ {
for y := self.Area.Min.Y; y < self.Area.Max.Y; y++ {
self.SetCell(x, y, c)
}
}
}

View File

@ -21,27 +21,27 @@ func NewGauge() *Gauge {
}
// Buffer implements Bufferer interface.
func (g *Gauge) Buffer() *Buffer {
buf := g.Block.Buffer()
func (self *Gauge) Buffer() *Buffer {
buf := self.Block.Buffer()
// plot bar
width := g.Percent * g.X / 100
for y := 1; y <= g.Y; y++ {
width := self.Percent * self.X / 100
for y := 1; y <= self.Y; y++ {
for x := 1; x <= width; x++ {
buf.SetCell(x, y, Cell{' ', g.GaugeColor, g.GaugeColor})
buf.SetCell(x, y, Cell{' ', self.GaugeColor, self.GaugeColor})
}
}
// plot percentage
s := strconv.Itoa(g.Percent) + "%" + g.Description
s = MaxString(s, g.X)
y := (g.Y + 1) / 2
x := ((g.X - len(s)) + 1) / 2
s := strconv.Itoa(self.Percent) + "%" + self.Description
s = MaxString(s, self.X)
y := (self.Y + 1) / 2
x := ((self.X - len(s)) + 1) / 2
for i, char := range s {
bg := g.Bg
fg := g.Fg
bg := self.Bg
fg := self.Fg
if x+i < width {
fg = g.GaugeColor
fg = self.GaugeColor
bg = AttrReverse
}
buf.SetCell(1+x+i, y, Cell{char, fg, bg})

View File

@ -25,7 +25,7 @@ func NewGrid() *Grid {
}
// Set assigns a widget and its grid dimensions to Grid.
func (g *Grid) Set(x0, y0, x1, y1 int, widget GridBufferer) {
func (self *Grid) Set(x0, y0, x1, y1 int, widget GridBufferer) {
if widget == nil {
return
}
@ -34,33 +34,33 @@ func (g *Grid) Set(x0, y0, x1, y1 int, widget GridBufferer) {
}
widget.SetGrid(x0, y0, x1, y1)
widget.Resize(g.Width, g.Height, g.Cols, g.Rows)
widget.Resize(self.Width, self.Height, self.Cols, self.Rows)
g.Widgets = append(g.Widgets, widget)
self.Widgets = append(self.Widgets, widget)
}
// Resize resizes each widget in the grid.
func (g *Grid) Resize() {
for _, w := range g.Widgets {
w.Resize(g.Width, g.Height, g.Cols, g.Rows)
func (self *Grid) Resize() {
for _, w := range self.Widgets {
w.Resize(self.Width, self.Height, self.Cols, self.Rows)
}
}
// Buffer implements the Bufferer interface by merging each widget in Grid into one buffer.
func (g *Grid) Buffer() *Buffer {
buf := NewFilledBuffer(0, 0, g.Width, g.Height, Cell{' ', ColorDefault, Theme.Bg})
for _, w := range g.Widgets {
func (self *Grid) Buffer() *Buffer {
buf := NewFilledBuffer(0, 0, self.Width, self.Height, Cell{' ', ColorDefault, Theme.Bg})
for _, w := range self.Widgets {
buf.MergeWithOffset(w.Buffer(), w.GetXOffset(), w.GetYOffset())
}
return buf
}
// GetXOffset implements Bufferer interface.
func (g *Grid) GetXOffset() int {
func (self *Grid) GetXOffset() int {
return 0
}
// GetYOffset implements Bufferer interface.
func (g *Grid) GetYOffset() int {
func (self *Grid) GetYOffset() int {
return 0
}

View File

@ -30,21 +30,21 @@ func NewLineGraph() *LineGraph {
}
// Buffer implements Bufferer interface.
func (lc *LineGraph) Buffer() *Buffer {
buf := lc.Block.Buffer()
func (self *LineGraph) Buffer() *Buffer {
buf := self.Block.Buffer()
// 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, lc.X+2)
colors := make([][]Color, self.X+2)
for i := range colors {
colors[i] = make([]Color, lc.Y+2)
colors[i] = make([]Color, self.Y+2)
}
// sort the series so that overlapping data will overlap the same way each time
seriesList := make([]string, len(lc.Data))
seriesList := make([]string, len(self.Data))
i := 0
for seriesName := range lc.Data {
for seriesName := range self.Data {
seriesList[i] = seriesName
i++
}
@ -53,21 +53,21 @@ func (lc *LineGraph) Buffer() *Buffer {
// draw lines in reverse order so that the first color defined in the colorscheme is on top
for i := len(seriesList) - 1; i >= 0; i-- {
seriesName := seriesList[i]
seriesData := lc.Data[seriesName]
seriesLineColor, ok := lc.LineColor[seriesName]
seriesData := self.Data[seriesName]
seriesLineColor, ok := self.LineColor[seriesName]
if !ok {
seriesLineColor = lc.DefaultLineColor
seriesLineColor = self.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-- {
x := ((lc.X + 1) * 2) - 1 - (((len(seriesData) - 1) - i) * lc.Zoom)
y := ((lc.Y + 1) * 4) - 1 - int((float64((lc.Y)*4)-1)*(seriesData[i]/100))
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))
if x < 0 {
// render the line to the last point up to the wall
if x > 0-lc.Zoom {
if x > 0-self.Zoom {
for _, p := range drawille.Line(lastX, lastY, x, y) {
if p.X > 0 {
c.Set(p.X, p.Y)
@ -97,7 +97,7 @@ func (lc *LineGraph) Buffer() *Buffer {
continue
}
if char != 10240 { // empty braille character
buf.SetCell(x, y, Cell{char, colors[x][y], lc.Bg})
buf.SetCell(x, y, Cell{char, colors[x][y], self.Bg})
}
}
}
@ -106,17 +106,17 @@ func (lc *LineGraph) Buffer() *Buffer {
// renders key ontop
for j, seriesName := range seriesList {
// sorts lines again
seriesData := lc.Data[seriesName]
seriesLineColor, ok := lc.LineColor[seriesName]
seriesData := self.Data[seriesName]
seriesLineColor, ok := self.LineColor[seriesName]
if !ok {
seriesLineColor = lc.DefaultLineColor
seriesLineColor = self.DefaultLineColor
}
// render key ontop, but let braille be drawn over space characters
str := fmt.Sprintf("%s %3.0f%%", seriesName, seriesData[len(seriesData)-1])
for k, char := range str {
if char != ' ' {
buf.SetCell(3+k, j+2, Cell{char, seriesLineColor, lc.Bg})
buf.SetCell(3+k, j+2, Cell{char, seriesLineColor, self.Bg})
}
}

View File

@ -18,8 +18,8 @@ type Sparklines struct {
}
// Add appends a given Sparkline to the *Sparklines.
func (s *Sparklines) Add(sl Sparkline) {
s.Lines = append(s.Lines, &sl)
func (self *Sparklines) Add(sl Sparkline) {
self.Lines = append(self.Lines, &sl)
}
// NewSparkline returns an unrenderable single sparkline that intended to be added into a Sparklines.
@ -39,37 +39,37 @@ func NewSparklines(ss ...*Sparkline) *Sparklines {
}
// Buffer implements Bufferer interface.
func (sl *Sparklines) Buffer() *Buffer {
buf := sl.Block.Buffer()
func (self *Sparklines) Buffer() *Buffer {
buf := self.Block.Buffer()
lc := len(sl.Lines) // lineCount
lc := len(self.Lines) // lineCount
// renders each sparkline and its titles
for i, line := range sl.Lines {
for i, line := range self.Lines {
// prints titles
title1Y := 2 + (sl.Y/lc)*i
title2Y := (2 + (sl.Y/lc)*i) + 1
title1 := MaxString(line.Title1, sl.X)
title2 := MaxString(line.Title2, sl.X)
buf.SetString(1, title1Y, title1, line.TitleColor|AttrBold, sl.Bg)
buf.SetString(1, title2Y, title2, line.TitleColor|AttrBold, sl.Bg)
title1Y := 2 + (self.Y/lc)*i
title2Y := (2 + (self.Y/lc)*i) + 1
title1 := MaxString(line.Title1, self.X)
title2 := MaxString(line.Title2, self.X)
buf.SetString(1, title1Y, title1, line.TitleColor|AttrBold, self.Bg)
buf.SetString(1, title2Y, title2, line.TitleColor|AttrBold, self.Bg)
sparkY := (sl.Y / lc) * (i + 1)
sparkY := (self.Y / lc) * (i + 1)
// 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-- {
for i := len(line.Data) - 1; i >= 0 && self.X-((len(line.Data)-1)-i) >= 1; i-- {
if line.Data[i] > max {
max = line.Data[i]
}
}
// prints sparkline
for x := sl.X; x >= 1; x-- {
for x := self.X; x >= 1; x-- {
char := SPARKS[0]
if (sl.X - x) < len(line.Data) {
char = SPARKS[int((float64(line.Data[(len(line.Data)-1)-(sl.X-x)])/float64(max))*7)]
if (self.X - x) < len(line.Data) {
char = SPARKS[int((float64(line.Data[(len(line.Data)-1)-(self.X-x)])/float64(max))*7)]
}
buf.SetCell(x, sparkY, Cell{char, line.LineColor, sl.Bg})
buf.SetCell(x, sparkY, Cell{char, line.LineColor, self.Bg})
}
}

View File

@ -22,82 +22,82 @@ type Table struct {
// NewTable returns a new Table instance
func NewTable() *Table {
t := &Table{
self := &Table{
Block: NewBlock(),
Cursor: Theme.TableCursor,
SelectedRow: 0,
TopRow: 0,
UniqueCol: 0,
}
t.ColResizer = t.ColResize
return t
self.ColResizer = self.ColResize
return self
}
// ColResize is the default column resizer, but can be overriden.
// ColResize calculates the width of each column.
func (t *Table) ColResize() {
func (self *Table) ColResize() {
// calculate gap size based on total width
t.Gap = 3
if t.X < 50 {
t.Gap = 1
} else if t.X < 75 {
t.Gap = 2
self.Gap = 3
if self.X < 50 {
self.Gap = 1
} else if self.X < 75 {
self.Gap = 2
}
cur := 0
for _, w := range t.ColWidths {
cur += t.Gap
t.CellXPos = append(t.CellXPos, cur)
for _, w := range self.ColWidths {
cur += self.Gap
self.CellXPos = append(self.CellXPos, cur)
cur += w
}
}
// Buffer implements the Bufferer interface.
func (t *Table) Buffer() *Buffer {
buf := t.Block.Buffer()
func (self *Table) Buffer() *Buffer {
buf := self.Block.Buffer()
// removes gap at the bottom of the current view if there is one
if t.TopRow > len(t.Rows)-(t.Y-1) {
t.TopRow = len(t.Rows) - (t.Y - 1)
if self.TopRow > len(self.Rows)-(self.Y-1) {
self.TopRow = len(self.Rows) - (self.Y - 1)
}
t.ColResizer()
self.ColResizer()
// prints header
for i, width := range t.ColWidths {
for i, width := range self.ColWidths {
if width == 0 {
break
}
r := MaxString(t.Header[i], t.X-6)
buf.SetString(t.CellXPos[i], 1, r, t.Fg|AttrBold, t.Bg)
r := MaxString(self.Header[i], self.X-6)
buf.SetString(self.CellXPos[i], 1, r, self.Fg|AttrBold, self.Bg)
}
// prints each row
for rowNum := t.TopRow; rowNum < t.TopRow+t.Y-1 && rowNum < len(t.Rows); rowNum++ {
row := t.Rows[rowNum]
y := (rowNum + 2) - t.TopRow
for rowNum := self.TopRow; rowNum < self.TopRow+self.Y-1 && rowNum < len(self.Rows); rowNum++ {
row := self.Rows[rowNum]
y := (rowNum + 2) - self.TopRow
// prints cursor
bg := t.Bg
if (t.SelectedItem == "" && rowNum == t.SelectedRow) || (t.SelectedItem != "" && t.SelectedItem == row[t.UniqueCol]) {
bg = t.Cursor
for _, width := range t.ColWidths {
bg := self.Bg
if (self.SelectedItem == "" && rowNum == self.SelectedRow) || (self.SelectedItem != "" && self.SelectedItem == row[self.UniqueCol]) {
bg = self.Cursor
for _, width := range self.ColWidths {
if width == 0 {
break
}
buf.SetString(1, y, strings.Repeat(" ", t.X), t.Fg, bg)
buf.SetString(1, y, strings.Repeat(" ", self.X), self.Fg, bg)
}
t.SelectedItem = row[t.UniqueCol]
t.SelectedRow = rowNum
self.SelectedItem = row[self.UniqueCol]
self.SelectedRow = rowNum
}
// prints each col of the row
for i, width := range t.ColWidths {
for i, width := range self.ColWidths {
if width == 0 {
break
}
r := MaxString(row[i], t.X-6)
buf.SetString(t.CellXPos[i], y, r, t.Fg, bg)
r := MaxString(row[i], self.X-6)
buf.SetString(self.CellXPos[i], y, r, self.Fg, bg)
}
}
@ -109,71 +109,71 @@ func (t *Table) Buffer() *Buffer {
/////////////////////////////////////////////////////////////////////////////////
// calcPos is used to calculate the cursor position and the current view.
func (t *Table) calcPos() {
t.SelectedItem = ""
func (self *Table) calcPos() {
self.SelectedItem = ""
if t.SelectedRow < 0 {
t.SelectedRow = 0
if self.SelectedRow < 0 {
self.SelectedRow = 0
}
if t.SelectedRow < t.TopRow {
t.TopRow = t.SelectedRow
if self.SelectedRow < self.TopRow {
self.TopRow = self.SelectedRow
}
if t.SelectedRow > len(t.Rows)-1 {
t.SelectedRow = len(t.Rows) - 1
if self.SelectedRow > len(self.Rows)-1 {
self.SelectedRow = len(self.Rows) - 1
}
if t.SelectedRow > t.TopRow+(t.Y-2) {
t.TopRow = t.SelectedRow - (t.Y - 2)
if self.SelectedRow > self.TopRow+(self.Y-2) {
self.TopRow = self.SelectedRow - (self.Y - 2)
}
}
func (t *Table) Up() {
t.SelectedRow -= 1
t.calcPos()
func (self *Table) Up() {
self.SelectedRow -= 1
self.calcPos()
}
func (t *Table) Down() {
t.SelectedRow += 1
t.calcPos()
func (self *Table) Down() {
self.SelectedRow += 1
self.calcPos()
}
func (t *Table) Top() {
t.SelectedRow = 0
t.calcPos()
func (self *Table) Top() {
self.SelectedRow = 0
self.calcPos()
}
func (t *Table) Bottom() {
t.SelectedRow = len(t.Rows) - 1
t.calcPos()
func (self *Table) Bottom() {
self.SelectedRow = len(self.Rows) - 1
self.calcPos()
}
// The number of lines in a page is equal to the height of the widget.
// The number of lines in a page is equal to the height of the widgeself.
func (t *Table) HalfPageUp() {
t.SelectedRow = t.SelectedRow - (t.Y-2)/2
t.calcPos()
func (self *Table) HalfPageUp() {
self.SelectedRow = self.SelectedRow - (self.Y-2)/2
self.calcPos()
}
func (t *Table) HalfPageDown() {
t.SelectedRow = t.SelectedRow + (t.Y-2)/2
t.calcPos()
func (self *Table) HalfPageDown() {
self.SelectedRow = self.SelectedRow + (self.Y-2)/2
self.calcPos()
}
func (t *Table) PageUp() {
t.SelectedRow -= (t.Y - 2)
t.calcPos()
func (self *Table) PageUp() {
self.SelectedRow -= (self.Y - 2)
self.calcPos()
}
func (t *Table) PageDown() {
t.SelectedRow += (t.Y - 2)
t.calcPos()
func (self *Table) PageDown() {
self.SelectedRow += (self.Y - 2)
self.calcPos()
}
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.SelectedRow = (t.TopRow + y) - 2
t.calcPos()
func (self *Table) Click(x, y int) {
x = x - self.XOffset
y = y - self.YOffset
if (x > 0 && x <= self.X) && (y > 0 && y <= self.Y) {
self.SelectedRow = (self.TopRow + y) - 2
self.calcPos()
}
}

View File

@ -16,43 +16,43 @@ type CPU struct {
func NewCPU(interval time.Duration, zoom int) *CPU {
count, _ := psCPU.Counts(false)
c := &CPU{
self := &CPU{
LineGraph: ui.NewLineGraph(),
Count: count,
interval: interval,
}
c.Label = "CPU Usage"
c.Zoom = zoom
if c.Count <= 8 {
for i := 0; i < c.Count; i++ {
self.Label = "CPU Usage"
self.Zoom = zoom
if self.Count <= 8 {
for i := 0; i < self.Count; i++ {
key := "CPU" + strconv.Itoa(i+1)
c.Data[key] = []float64{0}
self.Data[key] = []float64{0}
}
} else {
c.Data["Average"] = []float64{0}
self.Data["Average"] = []float64{0}
}
go c.update()
ticker := time.NewTicker(c.interval)
go self.update()
ticker := time.NewTicker(self.interval)
go func() {
for range ticker.C {
c.update()
self.update()
}
}()
return c
return self
}
func (c *CPU) update() {
func (self *CPU) update() {
// psutil calculates the CPU usage over a 1 second interval, therefore it blocks for 1 second
if c.Count <= 8 {
percent, _ := psCPU.Percent(c.interval, true)
for i := 0; i < c.Count; i++ {
if self.Count <= 8 {
percent, _ := psCPU.Percent(self.interval, true)
for i := 0; i < self.Count; i++ {
key := "CPU" + strconv.Itoa(i+1)
c.Data[key] = append(c.Data[key], percent[i])
self.Data[key] = append(self.Data[key], percent[i])
}
} else {
percent, _ := psCPU.Percent(c.interval, false)
c.Data["Average"] = append(c.Data["Average"], percent[0])
percent, _ := psCPU.Percent(self.interval, false)
self.Data["Average"] = append(self.Data["Average"], percent[0])
}
}

View File

@ -16,26 +16,26 @@ type Disk struct {
}
func NewDisk() *Disk {
d := &Disk{
self := &Disk{
Gauge: ui.NewGauge(),
fs: "/",
interval: time.Second * 5,
}
d.Label = "Disk Usage"
self.Label = "Disk Usage"
go d.update()
ticker := time.NewTicker(d.interval)
go self.update()
ticker := time.NewTicker(self.interval)
go func() {
for range ticker.C {
d.update()
self.update()
}
}()
return d
return self
}
func (d *Disk) update() {
usage, _ := psDisk.Usage(d.fs)
d.Percent = int(usage.UsedPercent)
d.Description = fmt.Sprintf(" (%dGB free)", int(utils.BytesToGB(usage.Free)))
func (self *Disk) update() {
usage, _ := psDisk.Usage(self.fs)
self.Percent = int(usage.UsedPercent)
self.Description = fmt.Sprintf(" (%dGB free)", int(utils.BytesToGB(usage.Free)))
}

View File

@ -39,12 +39,12 @@ func NewHelpMenu() *HelpMenu {
return &HelpMenu{block}
}
func (hm *HelpMenu) Buffer() *ui.Buffer {
buf := hm.Block.Buffer()
func (self *HelpMenu) Buffer() *ui.Buffer {
buf := self.Block.Buffer()
for y, line := range strings.Split(KEYBINDS, "\n") {
for x, char := range line {
buf.SetCell(x+1, y, ui.NewCell(char, ui.Color(7), hm.Bg))
buf.SetCell(x+1, y, ui.NewCell(char, ui.Color(7), self.Bg))
}
}

View File

@ -13,29 +13,29 @@ type Mem struct {
}
func NewMem(interval time.Duration, zoom int) *Mem {
m := &Mem{
self := &Mem{
LineGraph: ui.NewLineGraph(),
interval: interval,
}
m.Label = "Memory Usage"
m.Zoom = zoom
m.Data["Main"] = []float64{0}
m.Data["Swap"] = []float64{0}
self.Label = "Memory Usage"
self.Zoom = zoom
self.Data["Main"] = []float64{0}
self.Data["Swap"] = []float64{0}
go m.update()
ticker := time.NewTicker(m.interval)
go self.update()
ticker := time.NewTicker(self.interval)
go func() {
for range ticker.C {
m.update()
self.update()
}
}()
return m
return self
}
func (m *Mem) update() {
func (self *Mem) update() {
main, _ := psMem.VirtualMemory()
swap, _ := psMem.SwapMemory()
m.Data["Main"] = append(m.Data["Main"], main.UsedPercent)
m.Data["Swap"] = append(m.Data["Swap"], swap.UsedPercent)
self.Data["Main"] = append(self.Data["Main"], main.UsedPercent)
self.Data["Swap"] = append(self.Data["Swap"], swap.UsedPercent)
}

View File

@ -25,46 +25,46 @@ func NewNet() *Net {
sent.Data = []int{0}
spark := ui.NewSparklines(recv, sent)
n := &Net{
self := &Net{
Sparklines: spark,
interval: time.Second,
}
n.Label = "Network Usage"
self.Label = "Network Usage"
go n.update()
ticker := time.NewTicker(n.interval)
go self.update()
ticker := time.NewTicker(self.interval)
go func() {
for range ticker.C {
n.update()
self.update()
}
}()
return n
return self
}
func (n *Net) update() {
func (self *Net) update() {
// `false` causes psutil to group all network activity
interfaces, _ := psNet.IOCounters(false)
recvTotal := interfaces[0].BytesRecv
sentTotal := interfaces[0].BytesSent
if n.recvTotal != 0 { // if this isn't the first update
recvRecent := recvTotal - n.recvTotal
sentRecent := sentTotal - n.sentTotal
if self.recvTotal != 0 { // if this isn't the first update
recvRecent := recvTotal - self.recvTotal
sentRecent := sentTotal - self.sentTotal
n.Lines[0].Data = append(n.Lines[0].Data, int(recvRecent))
n.Lines[1].Data = append(n.Lines[1].Data, int(sentRecent))
self.Lines[0].Data = append(self.Lines[0].Data, int(recvRecent))
self.Lines[1].Data = append(self.Lines[1].Data, int(sentRecent))
}
// used in later calls to update
n.recvTotal = recvTotal
n.sentTotal = sentTotal
self.recvTotal = recvTotal
self.sentTotal = sentTotal
// renders net widget titles
for i := 0; i < 2; i++ {
var method string // either 'Rx' or 'Tx'
var total float64
recent := n.Lines[i].Data[len(n.Lines[i].Data)-1]
recent := self.Lines[i].Data[len(self.Lines[i].Data)-1]
unitTotal := "B"
unitRecent := "B"
@ -92,7 +92,7 @@ func (n *Net) update() {
unitTotal = "MB"
}
n.Lines[i].Title1 = fmt.Sprintf(" Total %s: %5.1f %s", method, total, unitTotal)
n.Lines[i].Title2 = fmt.Sprintf(" %s/s: %9d %2s/s", method, recent, unitRecent)
self.Lines[i].Title1 = fmt.Sprintf(" Total %s: %5.1f %s", method, total, unitTotal)
self.Lines[i].Title2 = fmt.Sprintf(" %s/s: %9d %2s/s", method, recent, unitRecent)
}
}

View File

@ -37,7 +37,7 @@ type Proc struct {
func NewProc(loaded, keyPressed chan bool) *Proc {
cpuCount, _ := psCPU.Counts(false)
p := &Proc{
self := &Proc{
Table: ui.NewTable(),
interval: time.Second,
cpuCount: cpuCount,
@ -45,33 +45,33 @@ func NewProc(loaded, keyPressed chan bool) *Proc {
group: true,
KeyPressed: keyPressed,
}
p.ColResizer = p.ColResize
p.Label = "Process List"
p.ColWidths = []int{5, 10, 4, 4}
self.ColResizer = self.ColResize
self.Label = "Process List"
self.ColWidths = []int{5, 10, 4, 4}
p.UniqueCol = 0
if p.group {
p.UniqueCol = 1
self.UniqueCol = 0
if self.group {
self.UniqueCol = 1
}
p.keyBinds()
self.keyBinds()
go func() {
p.update()
self.update()
loaded <- true
}()
ticker := time.NewTicker(p.interval)
ticker := time.NewTicker(self.interval)
go func() {
for range ticker.C {
p.update()
self.update()
}
}()
return p
return self
}
func (p *Proc) update() {
func (self *Proc) update() {
psProcesses, _ := psProc.Processes()
processes := make([]Process, len(psProcesses))
for i, psProcess := range psProcesses {
@ -83,150 +83,150 @@ func (p *Proc) update() {
processes[i] = Process{
pid,
command,
cpu / float64(p.cpuCount),
cpu / float64(self.cpuCount),
mem,
}
}
p.ungroupedProcs = processes
p.groupedProcs = Group(processes)
self.ungroupedProcs = processes
self.groupedProcs = Group(processes)
p.Sort()
self.Sort()
}
// Sort sorts either the grouped or ungrouped []Process based on the sortMethod.
// Called with every update, when the sort method is changed, and when processes are grouped and ungrouped.
func (p *Proc) Sort() {
p.Header = []string{"Count", "Command", "CPU%", "Mem%"}
func (self *Proc) Sort() {
self.Header = []string{"Count", "Command", "CPU%", "Mem%"}
if !p.group {
p.Header[0] = "PID"
if !self.group {
self.Header[0] = "PID"
}
processes := &p.ungroupedProcs
if p.group {
processes = &p.groupedProcs
processes := &self.ungroupedProcs
if self.group {
processes = &self.groupedProcs
}
switch p.sortMethod {
switch self.sortMethod {
case "c":
sort.Sort(sort.Reverse(ProcessByCPU(*processes)))
p.Header[2] += DOWN
self.Header[2] += DOWN
case "p":
if p.group {
if self.group {
sort.Sort(sort.Reverse(ProcessByPID(*processes)))
} else {
sort.Sort(ProcessByPID(*processes))
}
p.Header[0] += DOWN
self.Header[0] += DOWN
case "m":
sort.Sort(sort.Reverse(ProcessByMem(*processes)))
p.Header[3] += DOWN
self.Header[3] += DOWN
}
p.Rows = FieldsToStrings(*processes)
self.Rows = FieldsToStrings(*processes)
}
// ColResize overrides the default ColResize in the termui table.
func (p *Proc) ColResize() {
func (self *Proc) ColResize() {
// calculate gap size based on total width
p.Gap = 3
if p.X < 50 {
p.Gap = 1
} else if p.X < 75 {
p.Gap = 2
self.Gap = 3
if self.X < 50 {
self.Gap = 1
} else if self.X < 75 {
self.Gap = 2
}
p.CellXPos = []int{
p.Gap,
p.Gap + p.ColWidths[0] + p.Gap,
p.X - p.Gap - p.ColWidths[3] - p.Gap - p.ColWidths[2],
p.X - p.Gap - p.ColWidths[3],
self.CellXPos = []int{
self.Gap,
self.Gap + self.ColWidths[0] + self.Gap,
self.X - self.Gap - self.ColWidths[3] - self.Gap - self.ColWidths[2],
self.X - self.Gap - self.ColWidths[3],
}
rowWidth := p.Gap + p.ColWidths[0] + p.Gap + p.ColWidths[1] + p.Gap + p.ColWidths[2] + p.Gap + p.ColWidths[3] + p.Gap
rowWidth := self.Gap + self.ColWidths[0] + self.Gap + self.ColWidths[1] + self.Gap + self.ColWidths[2] + self.Gap + self.ColWidths[3] + self.Gap
// only renders a column if it fits
if p.X < (rowWidth - p.Gap - p.ColWidths[3]) {
p.ColWidths[2] = 0
p.ColWidths[3] = 0
} else if p.X < rowWidth {
p.CellXPos[2] = p.CellXPos[3]
p.ColWidths[3] = 0
if self.X < (rowWidth - self.Gap - self.ColWidths[3]) {
self.ColWidths[2] = 0
self.ColWidths[3] = 0
} else if self.X < rowWidth {
self.CellXPos[2] = self.CellXPos[3]
self.ColWidths[3] = 0
}
}
func (p *Proc) keyBinds() {
func (self *Proc) keyBinds() {
ui.On("<MouseLeft>", func(e ui.Event) {
p.Click(e.MouseX, e.MouseY)
p.KeyPressed <- true
self.Click(e.MouseX, e.MouseY)
self.KeyPressed <- true
})
ui.On("<MouseWheelUp>", "<MouseWheelDown>", func(e ui.Event) {
switch e.Key {
case "<MouseWheelDown>":
p.Down()
self.Down()
case "<MouseWheelUp>":
p.Up()
self.Up()
}
p.KeyPressed <- true
self.KeyPressed <- true
})
ui.On("<up>", "<down>", func(e ui.Event) {
switch e.Key {
case "<up>":
p.Up()
self.Up()
case "<down>":
p.Down()
self.Down()
}
p.KeyPressed <- true
self.KeyPressed <- true
})
viKeys := []string{"j", "k", "gg", "G", "<C-d>", "<C-u>", "<C-f>", "<C-b>"}
ui.On(viKeys, func(e ui.Event) {
switch e.Key {
case "j":
p.Down()
self.Down()
case "k":
p.Up()
self.Up()
case "gg":
p.Top()
self.Top()
case "G":
p.Bottom()
self.Bottom()
case "<C-d>":
p.HalfPageDown()
self.HalfPageDown()
case "<C-u>":
p.HalfPageUp()
self.HalfPageUp()
case "<C-f>":
p.PageDown()
self.PageDown()
case "<C-b>":
p.PageUp()
self.PageUp()
}
p.KeyPressed <- true
self.KeyPressed <- true
})
ui.On("dd", func(e ui.Event) {
p.Kill()
self.Kill()
})
ui.On("<tab>", func(e ui.Event) {
p.group = !p.group
if p.group {
p.UniqueCol = 1
self.group = !self.group
if self.group {
self.UniqueCol = 1
} else {
p.UniqueCol = 0
self.UniqueCol = 0
}
p.sortMethod = "c"
p.Sort()
p.Top()
p.KeyPressed <- true
self.sortMethod = "c"
self.Sort()
self.Top()
self.KeyPressed <- true
})
ui.On("m", "c", "p", func(e ui.Event) {
if p.sortMethod != e.Key {
p.sortMethod = e.Key
p.Top()
p.Sort()
p.KeyPressed <- true
if self.sortMethod != e.Key {
self.sortMethod = e.Key
self.Top()
self.Sort()
self.KeyPressed <- true
}
})
}
@ -279,13 +279,13 @@ func FieldsToStrings(P []Process) [][]string {
}
// Kill kills process or group of processes.
func (p *Proc) Kill() {
p.SelectedItem = ""
func (self *Proc) Kill() {
self.SelectedItem = ""
command := "kill"
if p.UniqueCol == 1 {
if self.UniqueCol == 1 {
command = "pkill"
}
cmd := exec.Command(command, p.Rows[p.SelectedRow][p.UniqueCol])
cmd := exec.Command(command, self.Rows[self.SelectedRow][self.UniqueCol])
cmd.Start()
}

View File

@ -23,60 +23,60 @@ type Temp struct {
}
func NewTemp() *Temp {
t := &Temp{
self := &Temp{
Block: ui.NewBlock(),
interval: time.Second * 5,
Data: make(map[string]int),
Threshold: 80, // temp at which color should change
}
t.Label = "Temperatures"
self.Label = "Temperatures"
go t.update()
ticker := time.NewTicker(t.interval)
go self.update()
ticker := time.NewTicker(self.interval)
go func() {
for range ticker.C {
t.update()
self.update()
}
}()
return t
return self
}
func (t *Temp) update() {
func (self *Temp) update() {
sensors, _ := psHost.SensorsTemperatures()
for _, sensor := range sensors {
// only sensors with input in their name are giving us live temp info
if strings.Contains(sensor.SensorKey, "input") {
// removes '_input' from the end of the sensor name
label := sensor.SensorKey[:strings.Index(sensor.SensorKey, "_input")]
t.Data[label] = int(sensor.Temperature)
self.Data[label] = int(sensor.Temperature)
}
}
}
// Buffer implements ui.Bufferer interface.
func (t *Temp) Buffer() *ui.Buffer {
buf := t.Block.Buffer()
func (self *Temp) Buffer() *ui.Buffer {
buf := self.Block.Buffer()
var keys []string
for k := range t.Data {
for k := range self.Data {
keys = append(keys, k)
}
sort.Strings(keys)
for y, key := range keys {
if y+1 > t.Y {
if y+1 > self.Y {
break
}
fg := t.TempLow
if t.Data[key] >= t.Threshold {
fg = t.TempHigh
fg := self.TempLow
if self.Data[key] >= self.Threshold {
fg = self.TempHigh
}
s := ui.MaxString(key, (t.X - 4))
buf.SetString(1, y+1, s, t.Fg, t.Bg)
buf.SetString(t.X-2, y+1, fmt.Sprintf("%dC", t.Data[key]), fg, t.Bg)
s := ui.MaxString(key, (self.X - 4))
buf.SetString(1, y+1, s, self.Fg, self.Bg)
buf.SetString(self.X-2, y+1, fmt.Sprintf("%dC", self.Data[key]), fg, self.Bg)
}