charmbracelet / lipgloss

Style definitions for nice terminal layouts 👄
MIT License
8.22k stars 231 forks source link

table resizing issue with \r\n in cells #383

Closed pachecot closed 1 month ago

pachecot commented 1 month ago

something in the resize logic seems to fail on a carriage return line feed "\r\n"

renders ok without setting the Width

package main

import (
    "fmt"
    "os"

    tea "github.com/charmbracelet/bubbletea"
    "github.com/charmbracelet/lipgloss"
    "github.com/charmbracelet/lipgloss/table"
)

var (
    data = [][]string{
        {"a0", "b0", "c0", "d0"},
        {"a1", "b1.0\r\nb1.1\r\nb1.2\r\nb1.3\r\nb1.4\r\nb1.5\r\nb1.6", "c1", "d1"},
        {"a2", "b2", "c2", "d2"},
        {"a3", "b3", "c3", "d3"},
    }
)

type Model struct {
    table *table.Table
}

func InitialModel() *Model {
    return &Model{
        table: table.New().
            Border(lipgloss.NormalBorder()).
            Data(table.NewStringData(data...)),
    }
}

func (m *Model) Init() tea.Cmd {
    return nil
}

func (m *Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {

    switch msg := msg.(type) {

    case tea.WindowSizeMsg:
        m.table.Width(msg.Width)

    case tea.KeyMsg:
        switch msg.String() {
        case "q", "ctrl+c":
            return m, tea.Quit
        }
    }
    return m, nil
}

func (m *Model) View() string {
    return m.table.String()
}

func main() {
    p := tea.NewProgram(
        InitialModel(),
        tea.WithAltScreen(),
    )
    if _, err := p.Run(); err != nil {
        fmt.Printf("Alas, there's been an error: %v", err)
        os.Exit(1)
    }
}

this is the result I got

┌──────────────────┬────────────────────┬──────────────────┬──────────────────┐
│a0                │b0                  │c0                │d0                │
                │c1                │d1                │
                │                  │                  │
                │                  │                  │
                │                  │                  │
                │                  │                  │
                │                  │                  │
│                  │b1.6                │                  │                  │
│a2                │b2                  │c2                │d2                │
│a3                │b3                  │c3                │d3                │

edit:

pachecot commented 1 month ago

Seems like issue is in x/ansi.Wrap isn't trimming the '\r' from "\r\n" and adding spaces so getting stuff like "\r \n".

bashbunni commented 1 month ago

Hey @pachecot I came across this problem as well recently. There's an open PR to strip \r in the Render function. That should hopefully fix this issue as well. I can add a test for this behaviour to that PR as well. Thanks a ton for including an example to reproduce this issue 🙏

I'm not sure if this fix should also be in other formatting functions as users will likely still encounter this if they're only using x/ansi for wrapping and not lipgloss for styling... 🤔 Maybe @aymanbagabas has thoughts on this?

bashbunni commented 1 month ago

Added a test for this case in the linked PR :)

pachecot commented 1 month ago

thanks @bashbunni looks like it would fix it.

pachecot commented 1 month ago

hey @bashbunni @aymanbagabas, on closer look x/ansi seems fine.

found issue source and submitted PR, which may be redundant with #386