Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
pachecot opened this issue Sep 28, 2024 · 5 comments · Fixed by #386
Closed

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

pachecot opened this issue Sep 28, 2024 · 5 comments · Fixed by #386
Assignees

Comments

@pachecot
Copy link

pachecot commented Sep 28, 2024

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
Copy link
Author

pachecot commented Sep 28, 2024

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
Copy link
Member

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 bashbunni self-assigned this Oct 9, 2024
@bashbunni bashbunni linked a pull request Oct 9, 2024 that will close this issue
@bashbunni
Copy link
Member

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

@pachecot
Copy link
Author

thanks @bashbunni looks like it would fix it.

@pachecot
Copy link
Author

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

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants