glow/ui/pager.go

540 lines
13 KiB
Go
Raw Normal View History

package ui
import (
"errors"
"fmt"
2020-08-18 19:41:57 +00:00
"log"
"math"
2020-08-21 00:21:52 +00:00
"path"
"strings"
2020-08-21 00:21:52 +00:00
"time"
"github.com/charmbracelet/bubbles/textinput"
"github.com/charmbracelet/bubbles/viewport"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/charm"
"github.com/charmbracelet/charm/ui/common"
"github.com/charmbracelet/glamour"
2020-06-03 18:17:41 +00:00
runewidth "github.com/mattn/go-runewidth"
"github.com/muesli/reflow/ansi"
te "github.com/muesli/termenv"
)
const (
maxDocumentWidth = 120
statusBarHeight = 1
gray = "#333333"
yellowGreen = "#ECFD65"
fuschia = "#EE6FF8"
2020-08-21 00:21:52 +00:00
mintGreen = "#89F0CB"
darkGreen = "#089B66"
noteHeadingText = " Set Memo "
notePromptText = " > "
)
var (
pagerHelpHeight = strings.Count(pagerHelpView(pagerModel{}, 0), "\n")
2020-06-03 18:17:41 +00:00
noteHeading = te.String(noteHeadingText).
Foreground(common.Cream.Color()).
Background(common.Green.Color()).
String()
statusBarBg = common.NewColorPair("#242424", "#E6E6E6")
statusBarNoteFg = common.NewColorPair("#7D7D7D", "#656565")
statusBarScrollPosFg = common.NewColorPair("#5A5A5A", "#949494")
2020-08-21 00:21:52 +00:00
// Styling functions
statusBarScrollPosStyle = newStyle(statusBarScrollPosFg.String(), statusBarBg.String())
statusBarNoteStyle = newStyle(statusBarNoteFg.String(), statusBarBg.String())
statusBarHelpStyle = newStyle(statusBarNoteFg.String(), common.NewColorPair("#323232", "#DCDCDC").String())
statusBarMessageHeaderStyle = newStyle(common.Cream.String(), common.Green.String())
statusBarMessageBodyStyle = newStyle(mintGreen, darkGreen)
helpViewStyle = newStyle(statusBarNoteFg.String(), common.NewColorPair("#1B1B1B", "#f2f2f2").String())
)
2020-08-21 00:21:52 +00:00
// Create a new termenv styling function
func newStyle(fg, bg string) func(string) string {
return te.Style{}.
Foreground(te.ColorProfile().Color(fg)).
Background(te.ColorProfile().Color(bg)).
Styled
}
// MSG
type contentRenderedMsg string
type noteSavedMsg *charm.Markdown
2020-08-21 00:21:52 +00:00
type statusMessageTimeoutMsg struct{}
type stashSuccessMsg markdown
type stashErrMsg struct {
err error
}
func (s stashErrMsg) Error() string {
return s.err.Error()
}
// MODEL
type pagerState int
const (
pagerStateBrowse pagerState = iota
pagerStateSetNote
2020-08-21 00:21:52 +00:00
pagerStateStashing
pagerStateStatusMessage
)
type pagerModel struct {
cc *charm.Client
viewport viewport.Model
state pagerState
glamourStyle string
width int
height int
textInput textinput.Model
2020-06-03 18:17:41 +00:00
showHelp bool
2020-08-21 00:21:52 +00:00
statusMessageHeader string
statusMessageBody string
statusMessageTimer *time.Timer
// Current document being rendered, sans-glamour rendering. We cache
2020-08-13 19:37:53 +00:00
// it here so we can re-render it on resize.
2020-08-21 00:21:52 +00:00
currentDocument markdown
}
func newPagerModel(glamourStyle string) pagerModel {
2020-06-20 23:54:01 +00:00
// Init viewport
vp := viewport.Model{}
vp.YPosition = 0
vp.HighPerformanceRendering = config.HighPerformancePager
// Init text input UI for notes/memos
ti := textinput.NewModel()
ti.Prompt = te.String(notePromptText).
Foreground(te.ColorProfile().Color(gray)).
Background(te.ColorProfile().Color(yellowGreen)).
String()
ti.TextColor = gray
ti.BackgroundColor = yellowGreen
ti.CursorColor = fuschia
2020-05-22 02:29:46 +00:00
ti.CharLimit = noteCharacterLimit
ti.Focus()
return pagerModel{
state: pagerStateBrowse,
glamourStyle: glamourStyle,
textInput: ti,
viewport: vp,
}
}
func (m *pagerModel) setSize(w, h int) {
m.width = w
m.height = h
m.viewport.Width = w
m.viewport.Height = h - statusBarHeight
m.textInput.Width = w - len(noteHeadingText) - len(notePromptText) - 1
2020-06-03 18:17:41 +00:00
if m.showHelp {
2020-06-20 19:21:30 +00:00
m.viewport.Height -= (statusBarHeight + pagerHelpHeight)
2020-06-03 18:17:41 +00:00
}
}
func (m *pagerModel) setContent(s string) {
m.viewport.SetContent(s)
}
func (m *pagerModel) toggleHelp() {
m.showHelp = !m.showHelp
m.setSize(m.width, m.height)
if m.viewport.PastBottom() {
m.viewport.GotoBottom()
}
}
func (m *pagerModel) unload() {
if m.showHelp {
m.toggleHelp()
}
2020-08-21 00:21:52 +00:00
if m.statusMessageTimer != nil {
m.statusMessageTimer.Stop()
}
m.state = pagerStateBrowse
m.viewport.SetContent("")
2020-06-20 18:49:56 +00:00
m.viewport.YOffset = 0
m.textInput.Reset()
}
// UPDATE
func pagerUpdate(msg tea.Msg, m pagerModel) (pagerModel, tea.Cmd) {
var (
cmd tea.Cmd
cmds []tea.Cmd
)
switch msg := msg.(type) {
case tea.KeyMsg:
switch m.state {
case pagerStateSetNote:
switch msg.String() {
case "esc":
m.state = pagerStateBrowse
return m, nil
case "enter":
var cmd tea.Cmd
if m.textInput.Value() != m.currentDocument.Note { // don't update if the note didn't change
m.currentDocument.Note = m.textInput.Value() // update optimistically
cmd = saveDocumentNote(m.cc, m.currentDocument.ID, m.currentDocument.Note)
}
m.state = pagerStateBrowse
m.textInput.Reset()
return m, cmd
}
default:
switch msg.String() {
case "q", "esc":
if m.state != pagerStateBrowse {
m.state = pagerStateBrowse
return m, nil
}
case "m":
// Users can only set the note on user-stashed markdown
if m.currentDocument.markdownType != stashedMarkdown {
2020-05-21 19:14:33 +00:00
break
}
m.state = pagerStateSetNote
2020-08-21 00:21:52 +00:00
// Stop the timer for hiding a status message since changing
// the state above will have cleared it.
if m.statusMessageTimer != nil {
m.statusMessageTimer.Stop()
}
// Pre-populate note with existing value
if m.textInput.Value() == "" {
m.textInput.SetValue(m.currentDocument.Note)
m.textInput.CursorEnd()
}
2020-08-21 00:21:52 +00:00
return m, textinput.Blink(m.textInput)
2020-08-21 00:21:52 +00:00
case "s":
// Stash a local document
if m.state != pagerStateStashing && m.currentDocument.markdownType == localMarkdown {
m.state = pagerStateStashing
// Create new markdown based on loaded markdown
md := m.currentDocument
md.markdownType = stashedMarkdown
t := time.Now()
md.CreatedAt = &t
// Set the note as the filename without the extension
p := md.localPath
md.Note = strings.Replace(path.Base(p), path.Ext(p), "", 1)
md.localPath = ""
return m, stashDocument(m.cc, md)
}
2020-06-03 18:17:41 +00:00
case "?":
m.toggleHelp()
2020-06-19 23:05:45 +00:00
if m.viewport.HighPerformanceRendering {
cmds = append(cmds, viewport.Sync(m.viewport))
}
}
}
// Glow has rendered the content
case contentRenderedMsg:
m.setContent(string(msg))
2020-06-19 23:05:45 +00:00
if m.viewport.HighPerformanceRendering {
cmds = append(cmds, viewport.Sync(m.viewport))
}
// We've reveived terminal dimensions, either for the first time or
// after a resize
case tea.WindowSizeMsg:
2020-08-21 00:21:52 +00:00
return m, renderWithGlamour(m, m.currentDocument.Body)
case stashSuccessMsg:
// Stashing was successful. Convert the loaded document to a stashed
// one. Note that we're also handling this message in the main update
// function where we're adding this stashed item to the stash listing.
m.state = pagerStateBrowse
// Replace the current document in the state so its metadata becomes
// that of a stashed document, but don't re-render since the body is
// identical to what's already rendered.
m.currentDocument = markdown(msg)
// Show a success message to the user.
m.state = pagerStateStatusMessage
m.statusMessageHeader = "Stashed!"
m.statusMessageBody = ""
m.statusMessageTimer = time.NewTimer(time.Second * 3)
cmds = append(cmds, waitForStatusMessageTimeout(m.statusMessageTimer))
case stashErrMsg:
// TODO
case statusMessageTimeoutMsg:
// Hide the status message bar
m.state = pagerStateBrowse
}
switch m.state {
case pagerStateSetNote:
m.textInput, cmd = textinput.Update(msg, m.textInput)
cmds = append(cmds, cmd)
default:
m.viewport, cmd = viewport.Update(msg, m.viewport)
cmds = append(cmds, cmd)
}
return m, tea.Batch(cmds...)
}
// VIEW
func pagerView(m pagerModel) string {
var b strings.Builder
fmt.Fprint(&b, viewport.View(m.viewport)+"\n")
// Footer
2020-08-21 00:21:52 +00:00
switch m.state {
case pagerStateSetNote:
pagerSetNoteView(&b, m)
2020-08-21 00:21:52 +00:00
case pagerStateStatusMessage:
pagerStatusMessageView(&b, m)
default:
pagerStatusBarView(&b, m)
}
2020-06-03 18:17:41 +00:00
if m.showHelp {
fmt.Fprintf(&b, pagerHelpView(m, m.width))
2020-06-03 18:17:41 +00:00
}
return b.String()
}
func pagerStatusBarView(b *strings.Builder, m pagerModel) {
// Logo
logo := glowLogoView(" Glow ")
// Scroll percent
scrollPercent := math.Max(0.0, math.Min(1.0, m.viewport.ScrollPercent()))
percentText := fmt.Sprintf(" %3.f%% ", scrollPercent*100)
2020-06-03 18:17:41 +00:00
// "Help" note
helpNote := statusBarHelpStyle(" ? Help ")
2020-06-03 18:17:41 +00:00
// Note
noteText := m.currentDocument.Note
if len(noteText) == 0 {
noteText = "(No title)"
}
noteText = truncate(" "+noteText+" ", max(0,
m.width-
ansi.PrintableRuneWidth(logo)-
ansi.PrintableRuneWidth(percentText)-
ansi.PrintableRuneWidth(helpNote),
2020-06-03 18:17:41 +00:00
))
// Empty space
emptyCell := te.String(" ").Background(statusBarBg.Color()).String()
padding := max(0,
m.width-
ansi.PrintableRuneWidth(logo)-
ansi.PrintableRuneWidth(noteText)-
ansi.PrintableRuneWidth(percentText)-
ansi.PrintableRuneWidth(helpNote),
)
emptySpace := strings.Repeat(emptyCell, padding)
2020-06-03 18:17:41 +00:00
fmt.Fprintf(b, "%s%s%s%s%s",
logo,
statusBarNoteStyle(noteText),
emptySpace,
statusBarScrollPosStyle(percentText),
2020-06-03 18:17:41 +00:00
helpNote,
)
}
func pagerSetNoteView(b *strings.Builder, m pagerModel) {
fmt.Fprint(b, noteHeading)
fmt.Fprint(b, textinput.View(m.textInput))
}
2020-08-21 00:21:52 +00:00
func pagerStatusMessageView(b *strings.Builder, m pagerModel) {
const bodyGapWidth = 2 // extra spaces we're adding before/after the body
header := m.statusMessageHeader
if len(header) > 0 {
header = " " + header + " "
}
body := m.statusMessageBody
bodyWidth := runewidth.StringWidth(body)
availBodySpace := m.width - runewidth.StringWidth(header) - bodyGapWidth
if availBodySpace < runewidth.StringWidth(body) {
body = runewidth.Truncate(body, availBodySpace, "…")
} else if availBodySpace > bodyWidth {
body = " " + body + " " + strings.Repeat(" ", availBodySpace-bodyWidth)
}
if len(header) > 0 {
fmt.Fprintf(b, statusBarMessageHeaderStyle(header))
}
fmt.Fprintf(b, statusBarMessageBodyStyle(body))
}
func pagerHelpView(m pagerModel, width int) (s string) {
col1 := [...]string{
"m set memo",
"esc back to files",
"q quit",
}
2020-08-21 00:21:52 +00:00
if m.currentDocument.markdownType != stashedMarkdown {
col1[0] = "s stash this document"
}
2020-06-03 18:17:41 +00:00
s += "\n"
s += "k/↑ up " + col1[0] + "\n"
s += "j/↓ down " + col1[1] + "\n"
s += "j/↓ down " + col1[2] + "\n"
2020-06-03 18:17:41 +00:00
s += "b/pgup page up\n"
2020-06-07 13:59:05 +00:00
s += "f/pgdn page down\n"
2020-06-03 18:17:41 +00:00
s += "u ½ page up\n"
s += "d ½ page down"
s = indent(s, 2)
// Fill up empty cells with spaces for background coloring
if width > 0 {
lines := strings.Split(s, "\n")
for i := 0; i < len(lines); i++ {
l := runewidth.StringWidth(lines[i])
n := max(width-l, 0)
2020-06-03 18:17:41 +00:00
lines[i] += strings.Repeat(" ", n)
}
s = strings.Join(lines, "\n")
}
return helpViewStyle(s)
}
// CMD
func renderWithGlamour(m pagerModel, md string) tea.Cmd {
return func() tea.Msg {
s, err := glamourRender(m, md)
if err != nil {
2020-08-18 19:41:57 +00:00
if debug {
log.Println("error rendering with Glamour:", err)
}
return errMsg(err)
}
return contentRenderedMsg(s)
}
}
func saveDocumentNote(cc *charm.Client, id int, note string) tea.Cmd {
if cc == nil {
return func() tea.Msg {
return errMsg(errors.New("can't set note; no charm client"))
}
}
return func() tea.Msg {
if err := cc.SetMarkdownNote(id, note); err != nil {
2020-08-18 19:41:57 +00:00
if debug {
log.Println("error saving note:", err)
}
return errMsg(err)
}
return noteSavedMsg(&charm.Markdown{ID: id, Note: note})
}
}
2020-08-21 00:21:52 +00:00
func stashDocument(cc *charm.Client, md markdown) tea.Cmd {
if cc == nil {
return func() tea.Msg {
err := errors.New("can't stash; no charm client")
if debug {
log.Println("error stash document:", err)
}
return stashErrMsg{err}
}
}
return func() tea.Msg {
err := cc.StashMarkdown(md.Note, md.Body)
if err != nil {
if debug {
log.Println("error stashing document:", err)
}
return errMsg(err)
}
return stashSuccessMsg(md)
}
}
func waitForStatusMessageTimeout(t *time.Timer) tea.Cmd {
return func() tea.Msg {
<-t.C
return statusMessageTimeoutMsg{}
}
}
// This is where the magic happens
func glamourRender(m pagerModel, markdown string) (string, error) {
2020-06-20 23:54:01 +00:00
if !config.GlamourEnabled {
return markdown, nil
}
// initialize glamour
var gs glamour.TermRendererOption
if m.glamourStyle == "auto" {
gs = glamour.WithAutoStyle()
} else {
gs = glamour.WithStylePath(m.glamourStyle)
}
width := max(0, min(maxDocumentWidth, m.viewport.Width))
r, err := glamour.NewTermRenderer(
gs,
glamour.WithWordWrap(width),
)
if err != nil {
return "", err
}
out, err := r.Render(markdown)
if err != nil {
return "", err
}
// trim lines
lines := strings.Split(string(out), "\n")
var content string
for i, s := range lines {
content += strings.TrimSpace(s)
// don't add an artificial newline after the last split
if i+1 < len(lines) {
content += "\n"
}
}
return content, nil
}