activity mode gauge

This commit is contained in:
Navid Yaghoobi 2021-12-21 18:13:01 +11:00
parent 542bf2bbb3
commit 6ea88b3b33
10 changed files with 159 additions and 10 deletions

View File

@ -3,5 +3,5 @@
tvxwidgets provides extra useful primitives (widgets) for [tview](https://github.com/rivo/tview)
* bar chart
* simple gauge
* resource utilisatrion gauge
* activity mode (am) gauge
* percentage mode (pm) gauge

97
am_gauge.go Normal file
View File

@ -0,0 +1,97 @@
package tvxwidgets
import (
"fmt"
"github.com/gdamore/tcell/v2"
"github.com/rivo/tview"
)
const (
// gauge cell
prgCell = "▉"
)
//ActivityModeGauge represents activity mode gauge permitive.
type ActivityModeGauge struct {
*tview.Box
// counter value
counter int
// pgBgColor: progress block background color
pgBgColor tcell.Color
}
// NewActivityModeGauge returns new activity mode gauge permitive.
func NewActivityModeGauge() *ActivityModeGauge {
gauge := &ActivityModeGauge{
Box: tview.NewBox(),
counter: 0,
pgBgColor: tcell.ColorBlue,
}
return gauge
}
// Draw draws this primitive onto the screen.
func (g *ActivityModeGauge) Draw(screen tcell.Screen) {
g.Box.DrawForSubclass(screen, g)
x, y, width, height := g.Box.GetInnerRect()
tickStr := g.tickStr(width)
for i := 0; i < height; i++ {
tview.Print(screen, tickStr, x, y+i, width, tview.AlignLeft, tcell.ColorYellow)
}
}
// SetTitle sets title for this primitive.
func (g *ActivityModeGauge) SetTitle(title string) {
g.Box.SetTitle(title)
}
// Focus is called when this primitive receives focus
func (g *ActivityModeGauge) Focus(delegate func(p tview.Primitive)) {
}
// HasFocus returns whether or not this primitive has focus
func (g *ActivityModeGauge) HasFocus() bool {
return g.Box.HasFocus()
}
// GetRect return primitive current rect
func (g *ActivityModeGauge) GetRect() (int, int, int, int) {
return g.Box.GetRect()
}
// SetRect sets rect for this primitive.
func (g *ActivityModeGauge) SetRect(x, y, width, height int) {
g.Box.SetRect(x, y, width, height)
}
// SetPgBgColor sets progress block background color
func (g *ActivityModeGauge) SetPgBgColor(color tcell.Color) {
g.pgBgColor = color
}
// Pulse pulse update the gauge progress bar
func (g *ActivityModeGauge) Pulse() {
g.counter = g.counter + 1
}
func (g *ActivityModeGauge) tickStr(max int) string {
if g.counter >= max-4 {
g.counter = 0
}
prgHeadStr := ""
hWidth := 0
prgEndStr := ""
prgStr := ""
for i := 0; i < g.counter; i++ {
prgHeadStr = prgHeadStr + fmt.Sprintf("[%s::]%s", getColorName(tview.Styles.PrimitiveBackgroundColor), prgCell)
hWidth++
}
prgStr = prgCell + prgCell + prgCell + prgCell
for i := 0; i < max+hWidth+4; i++ {
prgEndStr = prgEndStr + fmt.Sprintf("[%s::]%s", getColorName(tview.Styles.PrimitiveBackgroundColor), prgCell)
}
return fmt.Sprintf("%s[%s::]%s%s", prgHeadStr, getColorName(g.pgBgColor), prgStr, prgEndStr)
}

View File

@ -1,4 +1,4 @@
package tgraph
package tvxwidgets
import (
"fmt"
@ -136,6 +136,7 @@ func (c *BarChart) SetBorder(status bool) {
c.Box.SetBorder(status)
}
// GetRect return primitive current rect
func (c *BarChart) GetRect() (int, int, int, int) {
return c.Box.GetRect()
}

1
demos/am_gauge/README.md Normal file
View File

@ -0,0 +1 @@
![Screenshot](screenshot.png)

35
demos/am_gauge/main.go Normal file
View File

@ -0,0 +1,35 @@
// Demo code for the bar chart primitive.
package main
import (
"time"
"github.com/gdamore/tcell/v2"
"github.com/navidys/tvxwidgets"
"github.com/rivo/tview"
)
func main() {
app := tview.NewApplication()
gauge := tvxwidgets.NewActivityModeGauge()
gauge.SetTitle("activity mode gauge")
gauge.SetPgBgColor(tcell.ColorOrange)
gauge.SetRect(10, 4, 50, 3)
gauge.SetBorder(true)
update := func() {
tick := time.NewTicker(500 * time.Millisecond)
for {
select {
case <-tick.C:
gauge.Pulse()
app.Draw()
}
}
}
go update()
if err := app.SetRoot(gauge, false).EnableMouse(true).Run(); err != nil {
panic(err)
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.0 KiB

View File

@ -3,13 +3,13 @@ package main
import (
"github.com/gdamore/tcell/v2"
"github.com/navidys/tgraph"
"github.com/navidys/tvxwidgets"
"github.com/rivo/tview"
)
func main() {
app := tview.NewApplication()
barGraph := tgraph.NewBarChart()
barGraph := tvxwidgets.NewBarChart()
barGraph.SetRect(4, 2, 50, 20)
barGraph.SetBorder(true)
barGraph.SetTitle("System Resource Usage")

View File

@ -6,14 +6,14 @@ import (
"time"
"github.com/gdamore/tcell/v2"
"github.com/navidys/tgraph"
"github.com/navidys/tvxwidgets"
"github.com/rivo/tview"
)
func main() {
app := tview.NewApplication()
barGraph := tgraph.NewBarChart()
barGraph := tvxwidgets.NewBarChart()
barGraph.SetBorder(true)
barGraph.SetTitle("System Resource Usage")
// display system metric usage

8
go.mod
View File

@ -1,12 +1,14 @@
module github.com/navidys/tgraph
module github.com/navidys/tvxwidgets
go 1.17
require github.com/rivo/tview v0.0.0-20211202162923-2a6de950f73b
require (
github.com/gdamore/tcell/v2 v2.4.1-0.20210905002822-f057f0a857a1
github.com/rivo/tview v0.0.0-20211202162923-2a6de950f73b
)
require (
github.com/gdamore/encoding v1.0.0 // indirect
github.com/gdamore/tcell/v2 v2.4.1-0.20210905002822-f057f0a857a1 // indirect
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
github.com/mattn/go-runewidth v0.0.13 // indirect
github.com/rivo/uniseg v0.2.0 // indirect

13
utils.go Normal file
View File

@ -0,0 +1,13 @@
package tvxwidgets
import "github.com/gdamore/tcell/v2"
// getColorName returns convert tcell color to its name
func getColorName(color tcell.Color) string {
for name, c := range tcell.ColorNames {
if c == color {
return name
}
}
return ""
}