Strip down pager; the teminal size must now be fetched by the parent

This commit is contained in:
Christian Rocha 2020-05-14 10:45:53 -04:00
parent ffdaba08eb
commit 0e39761525
No known key found for this signature in database
GPG Key ID: D6CC7A16E5878018
1 changed files with 15 additions and 83 deletions

View File

@ -2,71 +2,33 @@ package pager
import ( import (
"errors" "errors"
"os"
"strings" "strings"
"github.com/charmbracelet/boba" "github.com/charmbracelet/boba"
"golang.org/x/crypto/ssh/terminal"
) )
// MSG
type terminalSizeMsg struct {
width int
height int
}
type errMsg error
// MODEL // MODEL
type State int
const (
StateInit State = iota
StateReady
)
type Model struct { type Model struct {
Err error Err error
Standalone bool Width int
State State Height int
Width int Y int
Height int
Y int
lines []string lines []string
} }
func (m *Model) PageUp() { // Content set the pager's text content
m.Y = max(0, m.Y-m.Height)
}
func (m *Model) PageDown() {
m.Y = min(len(m.lines)-m.Height, m.Y+m.Height)
}
// Content adds text content to the model
func (m *Model) Content(s string) { func (m *Model) Content(s string) {
s = strings.TrimSpace(s) s = strings.TrimSpace(s)
s = strings.Replace(s, "\r\n", "\n", -1) // normalize line endings s = strings.Replace(s, "\r\n", "\n", -1) // normalize line endings
m.lines = strings.Split(s, "\n") m.lines = strings.Split(s, "\n")
} }
func NewModel() Model { func NewModel(width, height int) Model {
return Model{ return Model{
State: StateInit, Width: width,
} Height: height,
}
// INIT
func Init(initialContent string) func() (boba.Model, boba.Cmd) {
m := NewModel()
m.Standalone = true
m.Content(initialContent)
return func() (boba.Model, boba.Cmd) {
return m, GetTerminalSize
} }
} }
@ -84,12 +46,6 @@ func Update(msg boba.Msg, model boba.Model) (boba.Model, boba.Cmd) {
case boba.KeyMsg: case boba.KeyMsg:
switch msg.String() { switch msg.String() {
case "q":
fallthrough
case "ctrl+c":
if m.Standalone {
return m, boba.Quit
}
// Up one page // Up one page
case "pgup": case "pgup":
@ -133,19 +89,9 @@ func Update(msg boba.Msg, model boba.Model) (boba.Model, boba.Cmd) {
// Re-render // Re-render
case "ctrl+l": case "ctrl+l":
return m, GetTerminalSize return m, nil
} }
case errMsg:
m.Err = msg
return m, nil
case terminalSizeMsg:
m.Width = msg.width
m.Height = msg.height
m.State = StateReady
return m, nil
} }
return model, nil return model, nil
@ -164,28 +110,14 @@ func View(model boba.Model) string {
} }
if len(m.lines) == 0 { if len(m.lines) == 0 {
return "(Buffer empty)" return ""
} }
if m.State == StateReady { // Render viewport
// Render viewport top := max(0, m.Y)
top := max(0, m.Y) bottom := min(len(m.lines), m.Y+m.Height)
bottom := min(len(m.lines), m.Y+m.Height) lines := m.lines[top:bottom]
lines := m.lines[top:bottom] return "\n" + strings.Join(lines, "\n")
return "\n" + strings.Join(lines, "\n")
}
return ""
}
// CMD
func GetTerminalSize() boba.Msg {
w, h, err := terminal.GetSize(int(os.Stdout.Fd()))
if err != nil {
return errMsg(err)
}
return terminalSizeMsg{w, h}
} }
// ETC // ETC