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

Fix Color Escaping Issue (#430) #452

Closed

Conversation

D7682
Copy link

@D7682 D7682 commented Dec 23, 2023

Description:
This pull request addresses issue #430.

Changes Made:

  • Fixed the list color rendering issue.

Additional Notes:

  • Performed local testing to validate the fix.

Related Issues:
Closes #430

Screenshots:

  • DefaultView
  • Filtered View
  • Filtered View (2)

Source Code:

package main

import (
	"fmt"
	"github.com/charmbracelet/bubbles/list"
	tea "github.com/charmbracelet/bubbletea"
	"github.com/charmbracelet/lipgloss"
	"os"
	"strconv"
)

var (
	docStyle = lipgloss.NewStyle().Margin(1, 2)
)

type Person struct {
	Name string
	Age  int
}

func (p Person) Title() string       { return p.Name }
func (p Person) Description() string { return strconv.Itoa(p.Age) }

func (p Person) FilterValue() string { return p.Name }

type model struct {
	list list.Model
}

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.KeyMsg:
		if msg.String() == "ctrl+c" {
			return m, tea.Quit
		}
	case tea.WindowSizeMsg:
		h, v := docStyle.GetFrameSize()
		m.list.SetSize(msg.Width-h, msg.Height-v)
	}

	var cmd tea.Cmd
	m.list, cmd = m.list.Update(msg)
	return m, cmd
}

func (m model) View() string {
	return docStyle.Render(m.list.View())
}

func main() {
	people := []list.Item{
		Person{Name: "John Smith", Age: 24},
		Person{Name: "Jane Doe", Age: 23},
		Person{Name: "Emily Johnson", Age: 25},
		Person{Name: "Alex Davis", Age: 14},
		Person{Name: "Ethan Brown", Age: 10},
	}

	m := model{list: list.New(people, list.NewDefaultDelegate(), 0, 0)}
	m.list.Title = "People"

	p := tea.NewProgram(m, tea.WithAltScreen())
	if _, err := p.Run(); err != nil {
		fmt.Println("Error running program: ", err)
		os.Exit(1)
	}

@jeffdupont
Copy link

I can confirm this corrects the color escaping in my issue.

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 this pull request may close these issues.

Filtered item shows escaped style color
2 participants