charmbracelet / bubbletea

A powerful little TUI framework 🏗
MIT License
27.24k stars 788 forks source link

Bubbletea table and Lip Gloss color do not work with ssh.Channel. #1040

Open anhcuongdev opened 3 months ago

anhcuongdev commented 3 months ago

Here is my sample code modified to use instead of default io.Reader, io.Writer to crypto/ssh Channel. Table seems to work fine but Table Style cannot accept Lip Gloss color code.

package proxy

import (
    "fmt"
    "github.com/charmbracelet/bubbles/table"
    tea "github.com/charmbracelet/bubbletea"
    "github.com/charmbracelet/lipgloss"
    "github.com/seknox/ssh"
)

var baseStyle = lipgloss.NewStyle().
    BorderStyle(lipgloss.RoundedBorder()).
    BorderForeground(lipgloss.Color("240"))

type model struct {
    table table.Model
}

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

func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
    var cmd tea.Cmd
    switch msg := msg.(type) {
    case tea.KeyMsg:
        switch msg.String() {
        case "esc":
            if m.table.Focused() {
                m.table.Blur()
            } else {
                m.table.Focus()
            }
        case "q", "ctrl+c":
            return m, tea.Quit
        case "enter":
            return m, tea.Batch(
                tea.Printf("Let's go to %s!", m.table.SelectedRow()[1]),
            )
        }
    }
    m.table, cmd = m.table.Update(msg)
    return m, cmd
}

func (m model) View() string {
    return baseStyle.Render(m.table.View()) + "\n"
}
func (s *Session) showTable(channel ssh.Channel) {
    columns := []table.Column{
        {Title: "Rank", Width: 4},
        {Title: "City", Width: 10},
        {Title: "Country", Width: 10},
        {Title: "Population", Width: 10},
    }

    rows := []table.Row{
        {"1", "Tokyo", "Japan", "37,274,000"},
        {"2", "Delhi", "India", "32,065,760"},
        {"3", "Shanghai", "China", "28,516,904"},
        {"4", "Dhaka", "Bangladesh", "22,478,116"},
        {"5", "São Paulo", "Brazil", "22,429,800"},
    }
    t := table.New(
        table.WithColumns(columns),
        table.WithRows(rows),
        table.WithFocused(true),
        table.WithHeight(7),
    )

    style := table.DefaultStyles()
    style.Header = style.Header.
        BorderStyle(lipgloss.NormalBorder()).
        BorderForeground(lipgloss.Color("240")).
        BorderBottom(true).
        Bold(false)
    style.Selected = style.Selected.
        Foreground(lipgloss.Color("229")).
        Background(lipgloss.Color("57")).
        Bold(false)
    t.SetStyles(style)

    m := model{t}
    if _, err := tea.NewProgram(m, tea.WithInput(channel), tea.WithOutput(channel)).Run(); err != nil {
        fmt.Println("Error running program:", err)
    }
}

image

kalensk commented 3 months ago

I think I am running into a similar issue. I am using a table model like you have in your example, but instead not calling tea.WithInput() or tea.WithOutput(). Rather I am simply ssh'ing into a server and running my bubble tea program. However, it is exhibiting the same issues as you are noting.

Any ideas?