Skip to content
Myr

main.go

This file

Path

examples/glamour/main.go

148 lines · 3.4 KiB · Go

Last changed by

fix(examples): migrate imports to charm.land for the glamour example (#1642)Mahdi Nayefbefore adoption7df1e65

History of this file

Take it

1package main23import (4	"fmt"5	"os"67	"charm.land/bubbles/v2/viewport"8	tea "charm.land/bubbletea/v2"9	"charm.land/glamour/v2"10	"charm.land/glamour/v2/styles"11	"charm.land/lipgloss/v2"12)1314const content = `15# Today’s Menu1617## Appetizers1819| Name        | Price | Notes                           |20| ---         | ---   | ---                             |21| Tsukemono   | $2    | Just an appetizer               |22| Tomato Soup | $4    | Made with San Marzano tomatoes  |23| Okonomiyaki | $4    | Takes a few minutes to make     |24| Curry       | $3    | We can add squash if you’d like |2526## Seasonal Dishes2728| Name                 | Price | Notes              |29| ---                  | ---   | ---                |30| Steamed bitter melon | $2    | Not so bitter      |31| Takoyaki             | $3    | Fun to eat         |32| Winter squash        | $3    | Today it's pumpkin |3334## Desserts3536| Name         | Price | Notes                 |37| ---          | ---   | ---                   |38| Dorayaki     | $4    | Looks good on rabbits |39| Banana Split | $5    | A classic             |40| Cream Puff   | $3    | Pretty creamy!        |4142All our dishes are made in-house by Karen, our chef. Most of our ingredients are from our garden or the fish market down the street.4344Some famous people that have eaten here lately:4546* [x] René Redzepi47* [x] David Chang48* [ ] Jiro Ono (maybe some day)4950Bon appétit!51`5253var helpStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("241")).Render5455type example struct {56	viewport viewport.Model57}5859func newExample(isDark bool) (*example, error) {60	const (61		width  = 7862		height = 2063	)6465	vp := viewport.New()66	vp.SetWidth(width)67	vp.SetHeight(height)68	vp.Style = lipgloss.NewStyle().69		BorderStyle(lipgloss.RoundedBorder()).70		BorderForeground(lipgloss.Color("62")).71		PaddingRight(2)7273	// We need to adjust the width of the glamour render from our main width74	// to account for a few things:75	//76	//  * The viewport border width77	//  * The viewport padding78	//  * The viewport margins79	//  * The gutter glamour applies to the left side of the content80	//81	const glamourGutter = 382	glamourRenderWidth := width - vp.Style.GetHorizontalFrameSize() - glamourGutter8384	style := styles.DarkStyleConfig85	if !isDark {86		style = styles.LightStyleConfig87	}88	renderer, err := glamour.NewTermRenderer(89		glamour.WithStyles(style),90		glamour.WithWordWrap(glamourRenderWidth),91	)92	if err != nil {93		return nil, err94	}9596	str, err := renderer.Render(content)97	if err != nil {98		return nil, err99	}100101	vp.SetContent(str)102103	return &example{104		viewport: vp,105	}, nil106}107108func (e example) Init() tea.Cmd {109	return nil110}111112func (e example) Update(msg tea.Msg) (tea.Model, tea.Cmd) {113	switch msg := msg.(type) {114	case tea.KeyPressMsg:115		switch msg.String() {116		case "q", "ctrl+c", "esc":117			return e, tea.Quit118		default:119			var cmd tea.Cmd120			e.viewport, cmd = e.viewport.Update(msg)121			return e, cmd122		}123	default:124		return e, nil125	}126}127128func (e example) View() tea.View {129	return tea.NewView(e.viewport.View() + e.helpView())130}131132func (e example) helpView() string {133	return helpStyle("\n  ↑/↓: Navigate • q: Quit\n")134}135136func main() {137	hasDarkBg := lipgloss.HasDarkBackground(os.Stdin, os.Stdout)138	model, err := newExample(hasDarkBg)139	if err != nil {140		fmt.Println("Could not initialize Bubble Tea model:", err)141		os.Exit(1)142	}143144	if _, err := tea.NewProgram(model).Run(); err != nil {145		fmt.Println("Bummer, there's been an error:", err)146		os.Exit(1)147	}148}