mirror of
https://github.com/navidys/tvxwidgets.git
synced 2025-05-09 19:29:29 +08:00
activity mode gauge
This commit is contained in:
parent
542bf2bbb3
commit
6ea88b3b33
@ -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
97
am_gauge.go
Normal 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)
|
||||
}
|
@ -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
1
demos/am_gauge/README.md
Normal file
@ -0,0 +1 @@
|
||||

|
35
demos/am_gauge/main.go
Normal file
35
demos/am_gauge/main.go
Normal 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)
|
||||
}
|
||||
}
|
BIN
demos/am_gauge/screenshot.png
Normal file
BIN
demos/am_gauge/screenshot.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 3.0 KiB |
@ -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")
|
||||
|
@ -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
8
go.mod
@ -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
|
||||
|
Loading…
x
Reference in New Issue
Block a user