Skip to content

Commit

Permalink
Cleanup header/footer rendering in pager example with Lip Gloss
Browse files Browse the repository at this point in the history
  • Loading branch information
meowgorithm committed Jan 13, 2022
1 parent e6e0ad9 commit 69a9305
Showing 1 changed file with 41 additions and 34 deletions.
75 changes: 41 additions & 34 deletions examples/pager/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ package main

import (
"fmt"
"io/ioutil"
"os"
"strings"

"github.com/charmbracelet/bubbles/viewport"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/mattn/go-runewidth"
)

// You generally won't need this unless you're processing stuff with
Expand All @@ -22,25 +22,19 @@ import (
// tea.EnterAltScreen().
const useHighPerformanceRenderer = false

func main() {
// Load some text for our viewport
content, err := os.ReadFile("artichoke.md")
if err != nil {
fmt.Println("could not load file:", err)
os.Exit(1)
}

p := tea.NewProgram(
model{content: string(content)},
tea.WithAltScreen(), // use the full size of the terminal in its "alternate screen buffer"
tea.WithMouseCellMotion(), // turn on mouse support so we can track the mouse wheel
)

if err := p.Start(); err != nil {
fmt.Println("could not run program:", err)
os.Exit(1)
}
}
var (
titleStyle = func() lipgloss.Style {
b := lipgloss.RoundedBorder()
b.Right = "โ”œ"
return lipgloss.NewStyle().BorderStyle(b).Padding(0, 1)
}()

infoStyle = func() lipgloss.Style {
b := lipgloss.RoundedBorder()
b.Left = "โ”ค"
return titleStyle.Copy().BorderStyle(b)
}()
)

type model struct {
content string
Expand Down Expand Up @@ -75,7 +69,7 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
// we can initialize the viewport. The initial dimensions come in
// quickly, though asynchronously, which is why we wait for them
// here.
m.viewport = viewport.NewModel(msg.Width, msg.Height-verticalMarginHeight)
m.viewport = viewport.New(msg.Width, msg.Height-verticalMarginHeight)
m.viewport.YPosition = headerHeight
m.viewport.HighPerformanceRendering = useHighPerformanceRenderer
m.viewport.SetContent(m.content)
Expand Down Expand Up @@ -115,22 +109,15 @@ func (m model) View() string {
}

func (m model) headerView() string {
headerTop := "โ•ญโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฎ"
headerMid := "โ”‚ Mr. Pager โ”œ"
headerBot := "โ•ฐโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฏ"
headerMid += strings.Repeat("โ”€", max(0, m.viewport.Width-runewidth.StringWidth(headerMid)))
return fmt.Sprintf("%s\n%s\n%s", headerTop, headerMid, headerBot)
title := titleStyle.Render("Mr. Pager")
line := strings.Repeat("โ”€", max(0, m.viewport.Width-lipgloss.Width(title)))
return lipgloss.JoinHorizontal(lipgloss.Center, title, line)
}

func (m model) footerView() string {
footerTop := "โ•ญโ”€โ”€โ”€โ”€โ”€โ”€โ•ฎ"
footerMid := fmt.Sprintf("โ”ค %3.f%% โ”‚", m.viewport.ScrollPercent()*100)
footerBot := "โ•ฐโ”€โ”€โ”€โ”€โ”€โ”€โ•ฏ"
gapSize := max(0, m.viewport.Width-runewidth.StringWidth(footerMid))
footerTop = strings.Repeat(" ", gapSize) + footerTop
footerMid = strings.Repeat("โ”€", gapSize) + footerMid
footerBot = strings.Repeat(" ", gapSize) + footerBot
return fmt.Sprintf("%s\n%s\n%s", footerTop, footerMid, footerBot)
info := infoStyle.Render(fmt.Sprintf("%3.f%%", m.viewport.ScrollPercent()*100))
line := strings.Repeat("โ”€", max(0, m.viewport.Width-lipgloss.Width(info)))
return lipgloss.JoinHorizontal(lipgloss.Center, line, info)
}

func max(a, b int) int {
Expand All @@ -139,3 +126,23 @@ func max(a, b int) int {
}
return b
}

func main() {
// Load some text for our viewport
content, err := ioutil.ReadFile("artichoke.md")
if err != nil {
fmt.Println("could not load file:", err)
os.Exit(1)
}

p := tea.NewProgram(
model{content: string(content)},
tea.WithAltScreen(), // use the full size of the terminal in its "alternate screen buffer"
tea.WithMouseCellMotion(), // turn on mouse support so we can track the mouse wheel
)

if err := p.Start(); err != nil {
fmt.Println("could not run program:", err)
os.Exit(1)
}
}

0 comments on commit 69a9305

Please sign in to comment.