get pager working for read action
This commit is contained in:
parent
360fedbebe
commit
972707e5fd
4 changed files with 97 additions and 388 deletions
|
|
@ -1,22 +1,43 @@
|
|||
package ui
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
gemlog "git.travisshears.com/travisshears/gemlog-cli/gemlog"
|
||||
"github.com/charmbracelet/bubbles/viewport"
|
||||
tea "github.com/charmbracelet/bubbletea"
|
||||
"github.com/charmbracelet/lipgloss"
|
||||
)
|
||||
|
||||
type EntryPageModel struct {
|
||||
entry gemlog.GemlogEntry
|
||||
entry gemlog.GemlogEntry
|
||||
ready bool
|
||||
viewport viewport.Model
|
||||
}
|
||||
|
||||
func initialEntryPageModel() EntryPageModel {
|
||||
return EntryPageModel{}
|
||||
}
|
||||
|
||||
func transformEntryToContent(entry gemlog.GemlogEntry) string {
|
||||
content := entry.Slug
|
||||
content += "\n\n"
|
||||
content += entry.Gemtxt
|
||||
content += "\n\n-------------------------\n"
|
||||
content += "\n\nPress h or left arrow to go back"
|
||||
return content
|
||||
}
|
||||
|
||||
func (m EntryPageModel) Update(msg tea.Msg, active bool, ctx *context) (EntryPageModel, tea.Cmd) {
|
||||
var (
|
||||
cmd tea.Cmd
|
||||
cmds []tea.Cmd
|
||||
)
|
||||
switch msg := msg.(type) {
|
||||
case GemLogLoaded:
|
||||
m.entry = msg.Log
|
||||
m.viewport.SetContent(transformEntryToContent(m.entry))
|
||||
case tea.KeyMsg:
|
||||
if !active {
|
||||
return m, nil
|
||||
|
|
@ -28,17 +49,72 @@ func (m EntryPageModel) Update(msg tea.Msg, active bool, ctx *context) (EntryPag
|
|||
}
|
||||
return m, cmd
|
||||
}
|
||||
case tea.WindowSizeMsg:
|
||||
headerHeight := lipgloss.Height(m.headerView())
|
||||
footerHeight := lipgloss.Height(m.footerView())
|
||||
verticalMarginHeight := headerHeight + footerHeight
|
||||
|
||||
if !m.ready {
|
||||
// Since this program is using the full size of the viewport we
|
||||
// need to wait until we've received the window dimensions before
|
||||
// we can initialize the viewport. The initial dimensions come in
|
||||
// quickly, though asynchronously, which is why we wait for them
|
||||
// here.
|
||||
|
||||
m.viewport = viewport.New(msg.Width, msg.Height-verticalMarginHeight)
|
||||
m.viewport.YPosition = headerHeight
|
||||
m.viewport.SetContent(transformEntryToContent(m.entry))
|
||||
m.ready = true
|
||||
} else {
|
||||
m.viewport.Width = msg.Width
|
||||
m.viewport.Height = msg.Height - verticalMarginHeight
|
||||
}
|
||||
}
|
||||
|
||||
return m, nil
|
||||
// Handle keyboard and mouse events in the viewport
|
||||
m.viewport, cmd = m.viewport.Update(msg)
|
||||
cmds = append(cmds, cmd)
|
||||
return m, tea.Batch(cmds...)
|
||||
|
||||
}
|
||||
|
||||
func (m EntryPageModel) View() string {
|
||||
s := m.entry.Slug
|
||||
s += "\n\n"
|
||||
s += m.entry.Gemtxt
|
||||
s += "\n\n-------------------------\n"
|
||||
s += "\n\nPress h or left arrow to go back"
|
||||
return s
|
||||
if !m.ready {
|
||||
return "\n Initializing..."
|
||||
}
|
||||
|
||||
return fmt.Sprintf("%s\n%s\n%s", m.headerView(), m.viewport.View(), m.footerView())
|
||||
}
|
||||
|
||||
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.BorderStyle(b)
|
||||
}()
|
||||
)
|
||||
|
||||
func (m EntryPageModel) headerView() string {
|
||||
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 EntryPageModel) footerView() string {
|
||||
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 {
|
||||
if a > b {
|
||||
return a
|
||||
}
|
||||
return b
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue