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

Setting value with a style breakes textarea #633

Open
ipsavitsky opened this issue Oct 7, 2024 · 1 comment
Open

Setting value with a style breakes textarea #633

ipsavitsky opened this issue Oct 7, 2024 · 1 comment
Labels
enhancement New feature or request

Comments

@ipsavitsky
Copy link

Describe the bug

I'm trying to set the textarea value to a string that has styles rendered. I expect the output to override the default styles, instead the textarea outputs the string escaped

Setup
Please complete the following information along with version numbers, if applicable.

  • OS Ubuntu on WSL
  • Shell bash
  • Terminal Emulator Windows Terminal
  • Terminal Multiplexer zellij
  • Locale en_US.UTF-8

To Reproduce
Steps to reproduce the behavior:

  • Compile the source in Source Code section
  • Start the compiled binary
  • Press arrow up

Source Code

package main

import (
	// "fmt"
	"log"

	"github.com/charmbracelet/bubbles/textarea"
	tea "github.com/charmbracelet/bubbletea"
	"github.com/charmbracelet/lipgloss"
)

var textStyle = lipgloss.NewStyle().Padding(1, 2).Faint(true);

func main() {
	p := tea.NewProgram(initialModel(), tea.WithAltScreen())

	if _, err := p.Run(); err != nil {
		log.Fatal(err)
	}
}

type model struct {
	textarea textarea.Model
}

func initialModel() model {
	ti := textarea.New()
	ti.CursorStart()
	ti.Focus()

	return model{
		textarea: ti,
	}
}

func (m model) Init() tea.Cmd {
	return textarea.Blink
}

func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
	var cmds []tea.Cmd
	var cmd tea.Cmd

	switch msg := msg.(type) {
	case tea.KeyMsg:
		switch msg.Type {
		case tea.KeyEsc:
			if m.textarea.Focused() {
				m.textarea.Blur()
			}
		case tea.KeyCtrlC:
			return m, tea.Quit
		case tea.KeyUp:
			m.textarea.SetValue(textStyle.Render("Hello world"))
		default:
			if !m.textarea.Focused() {
				cmd = m.textarea.Focus()
				cmds = append(cmds, cmd)
			}
		}
	}

	m.textarea, cmd = m.textarea.Update(msg)
	cmds = append(cmds, cmd)
	return m, tea.Batch(cmds...)
}

func (m model) View() string {
	return m.textarea.View()
}

Expected behavior
After pressing arrow up a string "Hello world" appears in faint text.

Screenshots
image

Additional context
I am planning to render text with multiple styles applied to different parts and since I can't set multiple styles to textarea, I have opted for rerendering text on every event. If I am misusing the textarea component or any other part of the library, let me know.

@prithvijj
Copy link

Just adding some random notes
image

After commenting out the code at

case unicode.IsControl(r):
, and then trying it, the control sequence characters end up rendering things, but it causes weird behaviour obivously like

(pressing the Left Arrow key multiple times, causes the part of the text to be in red and in regular font color white )
image


And there's the ability to set some styles within the BlurredStyle and FocusedStyle fields

image

@bashbunni bashbunni added the enhancement New feature or request label Dec 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants