gizak / termui

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

grid or block not working as expected #199

Closed lnxbil closed 5 years ago

lnxbil commented 5 years ago

Hi everyone,

I adapted the grid example to show you something strange:

package main

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

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

    l1 := ui.NewBlock()
    l1.Border = true
    l1.Height = ui.TermHeight() / 2

    r1 := ui.NewBlock()
    r1.Border = true
    r1.Height = ui.TermHeight() / 2

    l2 := ui.NewBlock()
    l2.Border = true
    l2.Height = ui.TermHeight() / 2

    r2 := ui.NewBlock()
    r2.Border = true
    r2.Height = ui.TermHeight() / 2

    // disable border on second row
    r2.BorderLeft = false
    l2.BorderRight = false

    ui.Body.AddRows(
        ui.NewRow(
            ui.NewCol(6, 0, l1),
            ui.NewCol(6, 0, r1)),
        ui.NewRow(
            ui.NewCol(6, 0, l2),
            ui.NewCol(6, 0, r2)),
    )
    ui.Body.Align()

    ui.Render(ui.Body)

    ui.Handle("<Resize>", func(e ui.Event) {
        payload := e.Payload.(ui.Resize)
        ui.Body.Width = payload.Width
        ui.Body.Align()
        ui.Clear()
        ui.Render(ui.Body)
    })

    ui.Handle("q", func(ui.Event) {
        ui.StopLoop()
    })

    ui.Loop()
}

yields this result in which the left box is one character short, so that there is a one character gap between the boxes:

image

Is this a bug or some strange side effect?

Best, Andreas

cjbassi commented 5 years ago

I'm not quite sure what you're referring to when you say 'the left box is one character short'. Are you referring to the gap between the top 2 boxes and the bottom 2 boxes or do you mean something else?

cjbassi commented 5 years ago

Hi, I get what you were saying now. It seems that the bottom left block should be joined with the bottom right block since we disabled the inner borders of each. termui just went through a sizable rewrite, and I rewrote your example to the latest api and it seems to be working now. Let me know if you still get this issue after updating.

package main

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

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

    l1 := ui.NewBlock()
    r1 := ui.NewBlock()
    l2 := ui.NewBlock()
    r2 := ui.NewBlock()

    // disable border on second row
    r2.BorderLeft = false
    l2.BorderRight = false

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

    grid.Set(
        ui.NewRow(1.0/2,
            ui.NewCol(1.0/2, l1),
            ui.NewCol(1.0/2, r1),
        ),
        ui.NewRow(1.0/2,
            ui.NewCol(1.0/2, l2),
            ui.NewCol(1.0/2, r2),
        ),
    )

    ui.Render(grid)

    uiEvents := ui.PollEvents()
    for {
        select {
        case e := <-uiEvents:
            switch e.ID {
            case "q", "<C-c>":
                return
            case "<Resize>":
                payload := e.Payload.(ui.Resize)
                grid.SetRect(0, 0, payload.Width, payload.Height)
                ui.Clear()
                ui.Render(grid)
            }
        }
    }
}
lnxbil commented 5 years ago

Thank you.