Skip to content
Myr

docs(example): resize chat example based on window (#1262)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by bashbunni · committed by GitHub · parent 467ad68 · Browse files · 1 file+16 added−3 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+16 added−3 removed

  • other:+16 added−3 removed

1 file+16 added−3 removed

modified:examples/chat/main.go+16 added-3 removed
12 unmodified lines
13 tea "github.com/charmbracelet/bubbletea"14 "github.com/charmbracelet/lipgloss"15)1617const gap = "\n\n"1819func main() {20 p := tea.NewProgram(initialModel())2122 if _, err := p.Run(); err != nil {
57 unmodified lines
80 m.textarea, tiCmd = m.textarea.Update(msg)81 m.viewport, vpCmd = m.viewport.Update(msg)8283 switch msg := msg.(type) {84 case tea.WindowSizeMsg:85 m.viewport.Width = msg.Width86 m.textarea.SetWidth(msg.Width)87 m.viewport.Height = msg.Height - m.textarea.Height() - lipgloss.Height(gap)8889 if len(m.messages) > 0 {90 // Wrap content before setting it.91 m.viewport.SetContent(lipgloss.NewStyle().Width(m.viewport.Width).Render(strings.Join(m.messages, "\n")))92 }93 m.viewport.GotoBottom()94 case tea.KeyMsg:95 switch msg.Type {96 case tea.KeyCtrlC, tea.KeyEsc:97 fmt.Println(m.textarea.Value())98 return m, tea.Quit99 case tea.KeyEnter:100 m.messages = append(m.messages, m.senderStyle.Render("You: ")+m.textarea.Value())89 m.viewport.SetContent(strings.Join(m.messages, "\n"))101 m.viewport.SetContent(lipgloss.NewStyle().Width(m.viewport.Width).Render(strings.Join(m.messages, "\n")))102 m.textarea.Reset()103 m.viewport.GotoBottom()104 }105
7 unmodified lines
113}114115func (m model) View() string {116 return fmt.Sprintf(105 "%s\n\n%s",117 "%s%s%s",118 m.viewport.View(),119 gap,120 m.textarea.View(),108 ) + "\n\n"121 )122}