gizak / termui

Golang terminal dashboard
MIT License
13.11k stars 786 forks source link

Sparklines bug? #133

Closed nifflin closed 5 years ago

nifflin commented 7 years ago
package main

import (
    ui "github.com/gizak/termui"
)

func main() {
    err := ui.Init()
    if err != nil {
        panic(err)
    }
    defer ui.Close()
    tsl := ui.NewSparkline()
    tsl.Height = 10
    tsl.LineColor = ui.ColorGreen
    tsl.Data = []int{4, 2, 1, 6, 3, 9, 1, 4, 2, 15, 14, 9, 8, 6, 10, 13, 15, 12, 10, 5, 3, 6, 1, 7, 10, 10, 14, 13, 6}
    tsls := ui.NewSparklines(tsl)
    tsls.BorderLabel = " Thruput "
    tsls.BorderFg = ui.ColorWhite
    tsls.X = 0
    tsls.Y = 4
    tsls.Height = 10
    tsls.Width = 128
    tsls.Border = false
    // build
    ui.Body.AddRows(
        ui.NewCol(10, 0, tsls))
    // calculate layout
    ui.Body.Align()
    ui.Render(ui.Body)
    ui.Handle("/sys/kbd/q", func(ui.Event) {
        ui.StopLoop()
    })
    ui.Loop()
}

When I delete tsls.Border = false, and rebuild it, the sparkline will not show normally. Thanks.

cjbassi commented 5 years ago

Hi, I (roughly) converted your code based on the latest api:

package main

import (
    ui "github.com/gizak/termui"
    "github.com/gizak/termui/widgets"
)

func main() {
    err := ui.Init()
    if err != nil {
        panic(err)
    }
    defer ui.Close()

    tsl := widgets.NewSparkline()
    tsl.LineColor = ui.ColorGreen
    tsl.Data = []float64{4, 2, 1, 6, 3, 9, 1, 4, 2, 15, 14, 9, 8, 6, 10, 13, 15, 12, 10, 5, 3, 6, 1, 7, 10, 10, 14, 13, 6}
    tsls := widgets.NewSparklineGroup(tsl)
    tsls.Title = " Thruput "
    tsls.BorderStyle.Fg = ui.ColorWhite
    // tsls.Border = false

    grid := ui.NewGrid()
    termWidth, termHeight := ui.TerminalDimensions()
    grid.SetRect(0, 0, termWidth, termHeight)

    grid.Set(
        ui.NewCol(1.0/2, tsls),
    )

    ui.Render(grid)

    uiEvents := ui.PollEvents()

    for {
        e := <-uiEvents
        switch e.ID {
        case "q", "<C-c>":
            return
        }
    }

}

And it seems to work for me with or without the tsls.Border = false line. I just rewrote the sparkline in 958a28575d7411492d25e42da3ad5a6e5947d9a4 on master, so that may have fixed it, but let me know if you still get issues. Do note that termui has been heavily reworked in that commit, so there's a lot of breaking changes.