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

The walk stopped after 200 commits: lines older than 3dfbcd9 point at it.

feat(render): skipping lines from previous frameKonstantin Antonovbefore adoption3dfbcd9

lines 1–6why, one step back
1package main
23import (4 "fmt"5 "os"6

Merge pull request #1118 from charmbracelet/v2-expAyman Bagabasbefore adoptiond1c8c97

lines 7–8why, one step back
7 "charm.land/bubbles/v2/viewport"
8 tea "charm.land/bubbletea/v2"

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

lines 9–10why, one step back
9 "charm.land/glamour/v2"
10 "charm.land/glamour/v2/styles"

Merge pull request #1118 from charmbracelet/v2-expAyman Bagabasbefore adoptiond1c8c97

line 11why, one step back
11 "charm.land/lipgloss/v2"

feat(render): skipping lines from previous frameKonstantin Antonovbefore adoption3dfbcd9

lines 12–41why, one step back
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! |41

Merge pull request #1118 from charmbracelet/v2-expAyman Bagabasbefore adoptiond1c8c97

line 42why, one step back
42All 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.

feat(render): skipping lines from previous frameKonstantin Antonovbefore adoption3dfbcd9

lines 43–58why, one step back
43
44Some 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}58

Merge pull request #1118 from charmbracelet/v2-expAyman Bagabasbefore adoptiond1c8c97

lines 59–63why, one step back
59func newExample(isDark bool) (*example, error) {
60 const (61 width = 7862 height = 2063 )

feat(render): skipping lines from previous frameKonstantin Antonovbefore adoption3dfbcd9

line 64why, one step back
64

Merge pull request #1118 from charmbracelet/v2-expAyman Bagabasbefore adoptiond1c8c97

lines 65–67why, one step back
65 vp := viewport.New()
66 vp.SetWidth(width)67 vp.SetHeight(height)

feat(render): skipping lines from previous frameKonstantin Antonovbefore adoption3dfbcd9

lines 68–72why, one step back
68 vp.Style = lipgloss.NewStyle().
69 BorderStyle(lipgloss.RoundedBorder()).70 BorderForeground(lipgloss.Color("62")).71 PaddingRight(2)72

docs(examples): fix glamour example (#1204)Christian Rochabefore adoption2060f93

lines 73–80why, one step back
73 // We need to adjust the width of the glamour render from our main width
74 // 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 //

Merge pull request #1118 from charmbracelet/v2-expAyman Bagabasbefore adoptiond1c8c97

line 81why, one step back
81 const glamourGutter = 3

docs(examples): fix glamour example (#1204)Christian Rochabefore adoption2060f93

lines 82–83why, one step back
82 glamourRenderWidth := width - vp.Style.GetHorizontalFrameSize() - glamourGutter
83

Merge pull request #1118 from charmbracelet/v2-expAyman Bagabasbefore adoptiond1c8c97

lines 84–87why, one step back
84 style := styles.DarkStyleConfig
85 if !isDark {86 style = styles.LightStyleConfig87 }

feat(render): skipping lines from previous frameKonstantin Antonovbefore adoption3dfbcd9

line 88why, one step back
88 renderer, err := glamour.NewTermRenderer(

Merge pull request #1118 from charmbracelet/v2-expAyman Bagabasbefore adoptiond1c8c97

line 89why, one step back
89 glamour.WithStyles(style),

docs(examples): fix glamour example (#1204)Christian Rochabefore adoption2060f93

line 90why, one step back
90 glamour.WithWordWrap(glamourRenderWidth),

feat(render): skipping lines from previous frameKonstantin Antonovbefore adoption3dfbcd9

lines 91–113why, one step back
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) {

Merge pull request #1118 from charmbracelet/v2-expAyman Bagabasbefore adoptiond1c8c97

line 114why, one step back
114 case tea.KeyPressMsg:

feat(render): skipping lines from previous frameKonstantin Antonovbefore adoption3dfbcd9

lines 115–127why, one step back
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}127

Merge pull request #1118 from charmbracelet/v2-expAyman Bagabasbefore adoptiond1c8c97

lines 128–129why, one step back
128func (e example) View() tea.View {
129 return tea.NewView(e.viewport.View() + e.helpView())

feat(render): skipping lines from previous frameKonstantin Antonovbefore adoption3dfbcd9

lines 130–136why, one step back
130}
131132func (e example) helpView() string {133 return helpStyle("\n ↑/↓: Navigate • q: Quit\n")134}135136func main() {

Merge pull request #1118 from charmbracelet/v2-expAyman Bagabasbefore adoptiond1c8c97

lines 137–138why, one step back
137 hasDarkBg := lipgloss.HasDarkBackground(os.Stdin, os.Stdout)
138 model, err := newExample(hasDarkBg)

feat(render): skipping lines from previous frameKonstantin Antonovbefore adoption3dfbcd9

lines 139–148why, one step back
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}