gizak / termui

Golang terminal dashboard
MIT License
13.06k stars 783 forks source link

Why is my rendering messy? #269

Open fguby opened 4 years ago

fguby commented 4 years ago

this is my code.

func main() {
    var path string
    flag.StringVar(&path, "p", "D:\\test", "路径,默认为当前路径")
    flag.Parse()

    if err := ui.Init(); err != nil {
        log.Fatalf("failed to initialize termui: %v", err)
    }
    defer ui.Close()

    //
    p := widgets.NewParagraph()
    p.Title = "Atom Parsing"
    p.Text = " starting..."
    p.SetRect(0, 0, 50, 3)
    p.TextStyle.Fg = ui.ColorWhite
    p.BorderStyle.Fg = ui.ColorGreen
    p.TitleStyle.Fg = ui.ColorGreen

    //
    g1 := widgets.NewGauge()
    g1.Title = "进度"
    g1.Percent = 0
    g1.SetRect(50, 0, 100, 3)
    g1.BarColor = ui.ColorGreen
    g1.LabelStyle = ui.NewStyle(ui.ColorYellow)
    g1.TitleStyle.Fg = ui.ColorMagenta
    g1.BorderStyle.Fg = ui.ColorYellow

    //
    l := widgets.NewList()
    listData := make([]string, 0, 10)
    l.Title = "Error"
    l.TitleStyle.Fg = ui.ColorRed
    l.Rows = listData
    l.SetRect(0, 4, 100, 10)
    l.TextStyle.Fg = ui.ColorBlue
    l.BorderStyle.Fg = ui.ColorWhite

    draw := func() {
        ui.Render(p, g1, l)
    }
    draw()

    time.Sleep(1 * time.Second)
    //
    go func() {
        for {
            select {
            case d := <-terminal.GaugeChan:
                g1.Percent = d
            case text := <-terminal.TitleChan:
                p.Text = text
            case s, ok := <-terminal.ProblemChan:
                if ok {
                    listData = append(listData, fmt.Sprintf(" [%d] %s", len(listData), s))
                    l.Rows = listData
                }
            }
        }
    }()

    //
    go func() {
        err1 := parsing.XmlParsing(path)
        if err1 != nil {
            log.Fatal(err1)
        }
    }()

    uiEvents := ui.PollEvents()
    ticker := time.NewTicker(100 * time.Millisecond).C
    for {
        select {
        case e := <-uiEvents:
            switch e.ID {
            case "q", "<C-c>":
                return
            }
        case <-ticker:
            draw()
        }
    }
}

1F41BF99-5176-4BDB-8921-887036A26DA3