forked from Mirrors/bubbletea
Cleanup header/footer rendering in pager example with Lip Gloss
This commit is contained in:
parent
7517c1b1bd
commit
ee92bed288
|
@ -5,13 +5,13 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/charmbracelet/bubbles/viewport"
|
"github.com/charmbracelet/bubbles/viewport"
|
||||||
tea "github.com/charmbracelet/bubbletea"
|
tea "github.com/charmbracelet/bubbletea"
|
||||||
"github.com/charmbracelet/lipgloss"
|
"github.com/charmbracelet/lipgloss"
|
||||||
"github.com/mattn/go-runewidth"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// You generally won't need this unless you're processing stuff with
|
// You generally won't need this unless you're processing stuff with
|
||||||
|
@ -22,25 +22,19 @@ import (
|
||||||
// tea.EnterAltScreen().
|
// tea.EnterAltScreen().
|
||||||
const useHighPerformanceRenderer = false
|
const useHighPerformanceRenderer = false
|
||||||
|
|
||||||
func main() {
|
var (
|
||||||
// Load some text for our viewport
|
titleStyle = func() lipgloss.Style {
|
||||||
content, err := os.ReadFile("artichoke.md")
|
b := lipgloss.RoundedBorder()
|
||||||
if err != nil {
|
b.Right = "├"
|
||||||
fmt.Println("could not load file:", err)
|
return lipgloss.NewStyle().BorderStyle(b).Padding(0, 1)
|
||||||
os.Exit(1)
|
}()
|
||||||
}
|
|
||||||
|
|
||||||
p := tea.NewProgram(
|
infoStyle = func() lipgloss.Style {
|
||||||
model{content: string(content)},
|
b := lipgloss.RoundedBorder()
|
||||||
tea.WithAltScreen(), // use the full size of the terminal in its "alternate screen buffer"
|
b.Left = "┤"
|
||||||
tea.WithMouseCellMotion(), // turn on mouse support so we can track the mouse wheel
|
return titleStyle.Copy().BorderStyle(b)
|
||||||
)
|
}()
|
||||||
|
)
|
||||||
if err := p.Start(); err != nil {
|
|
||||||
fmt.Println("could not run program:", err)
|
|
||||||
os.Exit(1)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
type model struct {
|
type model struct {
|
||||||
content string
|
content string
|
||||||
|
@ -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
|
// we can initialize the viewport. The initial dimensions come in
|
||||||
// quickly, though asynchronously, which is why we wait for them
|
// quickly, though asynchronously, which is why we wait for them
|
||||||
// here.
|
// 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.YPosition = headerHeight
|
||||||
m.viewport.HighPerformanceRendering = useHighPerformanceRenderer
|
m.viewport.HighPerformanceRendering = useHighPerformanceRenderer
|
||||||
m.viewport.SetContent(m.content)
|
m.viewport.SetContent(m.content)
|
||||||
|
@ -115,22 +109,15 @@ func (m model) View() string {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m model) headerView() string {
|
func (m model) headerView() string {
|
||||||
headerTop := "╭───────────╮"
|
title := titleStyle.Render("Mr. Pager")
|
||||||
headerMid := "│ Mr. Pager ├"
|
line := strings.Repeat("─", max(0, m.viewport.Width-lipgloss.Width(title)))
|
||||||
headerBot := "╰───────────╯"
|
return lipgloss.JoinHorizontal(lipgloss.Center, title, line)
|
||||||
headerMid += strings.Repeat("─", max(0, m.viewport.Width-runewidth.StringWidth(headerMid)))
|
|
||||||
return fmt.Sprintf("%s\n%s\n%s", headerTop, headerMid, headerBot)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m model) footerView() string {
|
func (m model) footerView() string {
|
||||||
footerTop := "╭──────╮"
|
info := infoStyle.Render(fmt.Sprintf("%3.f%%", m.viewport.ScrollPercent()*100))
|
||||||
footerMid := fmt.Sprintf("┤ %3.f%% │", m.viewport.ScrollPercent()*100)
|
line := strings.Repeat("─", max(0, m.viewport.Width-lipgloss.Width(info)))
|
||||||
footerBot := "╰──────╯"
|
return lipgloss.JoinHorizontal(lipgloss.Center, line, info)
|
||||||
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)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func max(a, b int) int {
|
func max(a, b int) int {
|
||||||
|
@ -139,3 +126,23 @@ func max(a, b int) int {
|
||||||
}
|
}
|
||||||
return b
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue