2020-05-11 20:13:16 +00:00
|
|
|
package ui
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
2020-05-14 19:06:13 +00:00
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"strings"
|
2020-05-11 20:13:16 +00:00
|
|
|
|
2020-05-13 16:53:58 +00:00
|
|
|
"github.com/charmbracelet/boba"
|
2020-05-14 02:08:17 +00:00
|
|
|
"github.com/charmbracelet/boba/pager"
|
2020-05-13 16:53:58 +00:00
|
|
|
"github.com/charmbracelet/boba/spinner"
|
2020-05-11 20:13:16 +00:00
|
|
|
"github.com/charmbracelet/charm"
|
|
|
|
"github.com/charmbracelet/charm/ui/common"
|
|
|
|
"github.com/charmbracelet/charm/ui/keygen"
|
2020-05-15 19:08:45 +00:00
|
|
|
"github.com/charmbracelet/glamour"
|
2020-05-13 23:02:39 +00:00
|
|
|
"github.com/muesli/reflow/indent"
|
2020-05-14 19:06:13 +00:00
|
|
|
te "github.com/muesli/termenv"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
statusBarHeight = 1
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2020-05-14 22:03:24 +00:00
|
|
|
glowLogoTextColor = common.Color("#ECFD65")
|
|
|
|
statusBarBg = common.NewColorPair("#242424", "#E6E6E6")
|
|
|
|
statusBarFg = common.NewColorPair("#5A5A5A", "#949494")
|
2020-05-11 20:13:16 +00:00
|
|
|
)
|
|
|
|
|
2020-05-13 16:53:58 +00:00
|
|
|
// NewProgram returns a new Boba program
|
2020-05-15 17:09:46 +00:00
|
|
|
func NewProgram(style string) *boba.Program {
|
|
|
|
return boba.NewProgram(initialize(style), update, view)
|
2020-05-11 20:13:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// MESSAGES
|
|
|
|
|
|
|
|
type fatalErrMsg error
|
|
|
|
type errMsg error
|
|
|
|
type newCharmClientMsg *charm.Client
|
|
|
|
type sshAuthErrMsg struct{}
|
2020-05-15 19:08:45 +00:00
|
|
|
type contentRenderedMsg string
|
2020-05-18 21:53:46 +00:00
|
|
|
type terminalResizedMsg struct{}
|
2020-05-11 20:13:16 +00:00
|
|
|
|
2020-05-14 19:06:13 +00:00
|
|
|
type terminalSizeMsg struct {
|
|
|
|
width int
|
|
|
|
height int
|
|
|
|
err error
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t terminalSizeMsg) Size() (int, int) { return t.width, t.height }
|
|
|
|
func (t terminalSizeMsg) Error() error { return t.err }
|
|
|
|
|
2020-05-11 20:13:16 +00:00
|
|
|
// MODEL
|
|
|
|
|
|
|
|
type state int
|
|
|
|
|
|
|
|
const (
|
|
|
|
stateInitCharmClient state = iota
|
|
|
|
stateKeygenRunning
|
|
|
|
stateKeygenFinished
|
2020-05-13 16:53:58 +00:00
|
|
|
stateShowStash
|
2020-05-14 02:08:17 +00:00
|
|
|
stateShowDocument
|
2020-05-11 20:13:16 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type model struct {
|
2020-05-15 17:09:46 +00:00
|
|
|
style string // style to use
|
2020-05-14 19:06:13 +00:00
|
|
|
cc *charm.Client
|
|
|
|
user *charm.User
|
|
|
|
spinner spinner.Model
|
|
|
|
keygen keygen.Model
|
|
|
|
state state
|
|
|
|
err error
|
|
|
|
stash stashModel
|
|
|
|
pager pager.Model
|
|
|
|
terminalWidth int
|
|
|
|
terminalHeight int
|
2020-05-18 22:58:19 +00:00
|
|
|
|
|
|
|
// Current document being rendered, sans-glamour rendering. We cache
|
|
|
|
// this here so we can re-render it on resize.
|
|
|
|
currentDocument *charm.Markdown
|
2020-05-14 02:08:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *model) unloadDocument() {
|
|
|
|
m.pager = pager.Model{}
|
|
|
|
m.state = stateShowStash
|
2020-05-15 19:08:45 +00:00
|
|
|
m.stash.state = stashStateStashLoaded
|
2020-05-18 22:58:19 +00:00
|
|
|
m.currentDocument = nil
|
2020-05-11 20:13:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// INIT
|
|
|
|
|
2020-05-15 17:09:46 +00:00
|
|
|
func initialize(style string) func() (boba.Model, boba.Cmd) {
|
|
|
|
return func() (boba.Model, boba.Cmd) {
|
|
|
|
s := spinner.NewModel()
|
|
|
|
s.Type = spinner.Dot
|
|
|
|
s.ForegroundColor = common.SpinnerColor
|
|
|
|
|
2020-05-16 18:45:32 +00:00
|
|
|
if style == "auto" {
|
|
|
|
dbg := te.HasDarkBackground()
|
|
|
|
if dbg == true {
|
|
|
|
style = "dark"
|
|
|
|
} else {
|
|
|
|
style = "light"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-15 17:09:46 +00:00
|
|
|
return model{
|
2020-05-18 19:00:44 +00:00
|
|
|
style: style,
|
|
|
|
spinner: s,
|
|
|
|
state: stateInitCharmClient,
|
2020-05-15 17:09:46 +00:00
|
|
|
}, boba.Batch(
|
|
|
|
newCharmClient,
|
|
|
|
spinner.Tick(s),
|
|
|
|
getTerminalSize(),
|
2020-05-18 21:53:46 +00:00
|
|
|
listenForTerminalResize(),
|
2020-05-15 17:09:46 +00:00
|
|
|
)
|
|
|
|
}
|
2020-05-11 20:13:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// UPDATE
|
|
|
|
|
2020-05-13 16:53:58 +00:00
|
|
|
func update(msg boba.Msg, mdl boba.Model) (boba.Model, boba.Cmd) {
|
2020-05-11 20:13:16 +00:00
|
|
|
m, ok := mdl.(model)
|
|
|
|
if !ok {
|
2020-05-13 16:53:58 +00:00
|
|
|
return model{err: errors.New("could not perform assertion on model in update")}, boba.Quit
|
2020-05-11 20:13:16 +00:00
|
|
|
}
|
|
|
|
|
2020-05-13 20:00:27 +00:00
|
|
|
var cmd boba.Cmd
|
|
|
|
|
2020-05-11 20:13:16 +00:00
|
|
|
switch msg := msg.(type) {
|
|
|
|
|
2020-05-13 16:53:58 +00:00
|
|
|
case boba.KeyMsg:
|
2020-05-11 20:13:16 +00:00
|
|
|
switch msg.String() {
|
2020-05-14 02:08:17 +00:00
|
|
|
|
2020-05-13 23:15:39 +00:00
|
|
|
case "q":
|
|
|
|
fallthrough
|
|
|
|
case "esc":
|
2020-05-14 02:08:17 +00:00
|
|
|
if m.state == stateShowDocument {
|
|
|
|
m.unloadDocument()
|
|
|
|
return m, nil
|
|
|
|
}
|
|
|
|
return m, boba.Quit
|
|
|
|
|
2020-05-11 20:13:16 +00:00
|
|
|
case "ctrl+c":
|
2020-05-13 16:53:58 +00:00
|
|
|
return m, boba.Quit
|
2020-05-15 00:23:11 +00:00
|
|
|
|
|
|
|
// Re-render
|
|
|
|
case "ctrl+l":
|
|
|
|
return m, getTerminalSize()
|
2020-05-11 20:13:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
case fatalErrMsg:
|
|
|
|
m.err = msg
|
2020-05-13 16:53:58 +00:00
|
|
|
return m, boba.Quit
|
2020-05-11 20:13:16 +00:00
|
|
|
|
|
|
|
case errMsg:
|
|
|
|
m.err = msg
|
|
|
|
return m, nil
|
|
|
|
|
2020-05-18 21:53:46 +00:00
|
|
|
case terminalResizedMsg:
|
|
|
|
return m, boba.Batch(
|
|
|
|
getTerminalSize(),
|
|
|
|
listenForTerminalResize(),
|
|
|
|
)
|
|
|
|
|
2020-05-14 19:06:13 +00:00
|
|
|
case terminalSizeMsg:
|
|
|
|
if msg.Error() != nil {
|
|
|
|
m.err = msg.Error()
|
|
|
|
return m, nil
|
|
|
|
}
|
|
|
|
w, h := msg.Size()
|
|
|
|
m.terminalWidth = w
|
|
|
|
m.terminalHeight = h
|
2020-05-15 00:23:11 +00:00
|
|
|
m.stash.SetSize(w, h)
|
2020-05-18 22:58:19 +00:00
|
|
|
|
|
|
|
if m.state == stateShowDocument {
|
|
|
|
m.pager.Width = w
|
|
|
|
m.pager.Height = h
|
|
|
|
}
|
|
|
|
|
|
|
|
var cmd boba.Cmd
|
|
|
|
if m.state == stateShowDocument {
|
|
|
|
cmd = renderWithGlamour(m, m.currentDocument.Body)
|
|
|
|
}
|
|
|
|
|
2020-05-15 02:48:38 +00:00
|
|
|
// TODO: load more stash pages if we've resized, are on the last page,
|
|
|
|
// and haven't loaded more pages yet.
|
2020-05-18 22:58:19 +00:00
|
|
|
return m, cmd
|
2020-05-14 19:06:13 +00:00
|
|
|
|
2020-05-11 20:13:16 +00:00
|
|
|
case sshAuthErrMsg:
|
|
|
|
// If we haven't run the keygen yet, do that
|
|
|
|
if m.state != stateKeygenFinished {
|
|
|
|
m.state = stateKeygenRunning
|
|
|
|
m.keygen = keygen.NewModel()
|
|
|
|
return m, keygen.GenerateKeys
|
|
|
|
}
|
|
|
|
|
2020-05-13 20:00:27 +00:00
|
|
|
// The keygen didn't work and we can't auth
|
2020-05-11 20:13:16 +00:00
|
|
|
m.err = errors.New("SSH authentication failed")
|
2020-05-13 16:53:58 +00:00
|
|
|
return m, boba.Quit
|
2020-05-11 20:13:16 +00:00
|
|
|
|
|
|
|
case spinner.TickMsg:
|
2020-05-13 20:00:27 +00:00
|
|
|
switch m.state {
|
|
|
|
case stateInitCharmClient:
|
2020-05-13 16:53:58 +00:00
|
|
|
m.spinner, cmd = spinner.Update(msg, m.spinner)
|
|
|
|
}
|
2020-05-13 20:00:27 +00:00
|
|
|
return m, cmd
|
|
|
|
|
|
|
|
case stashSpinnerTickMsg:
|
2020-05-14 19:06:13 +00:00
|
|
|
if m.state == stateShowStash {
|
|
|
|
m.stash, cmd = stashUpdate(msg, m.stash)
|
|
|
|
}
|
2020-05-13 20:00:27 +00:00
|
|
|
return m, cmd
|
2020-05-11 20:13:16 +00:00
|
|
|
|
|
|
|
case keygen.DoneMsg:
|
|
|
|
m.state = stateKeygenFinished
|
|
|
|
return m, newCharmClient
|
|
|
|
|
|
|
|
case newCharmClientMsg:
|
|
|
|
m.cc = msg
|
2020-05-13 16:53:58 +00:00
|
|
|
m.state = stateShowStash
|
2020-05-13 20:00:27 +00:00
|
|
|
m.stash, cmd = stashInit(m.cc)
|
2020-05-15 00:23:11 +00:00
|
|
|
m.stash.SetSize(m.terminalWidth, m.terminalHeight)
|
2020-05-13 20:00:27 +00:00
|
|
|
return m, cmd
|
2020-05-14 02:08:17 +00:00
|
|
|
|
|
|
|
case gotStashedItemMsg:
|
2020-05-15 19:08:45 +00:00
|
|
|
// We've received stashed item data. Render with Glamour and send to
|
|
|
|
// the pager.
|
2020-05-14 19:06:13 +00:00
|
|
|
m.pager = pager.NewModel(
|
|
|
|
m.terminalWidth,
|
|
|
|
m.terminalHeight-statusBarHeight,
|
|
|
|
)
|
2020-05-15 20:22:30 +00:00
|
|
|
|
2020-05-18 22:58:19 +00:00
|
|
|
m.currentDocument = msg
|
2020-05-15 19:08:45 +00:00
|
|
|
return m, renderWithGlamour(m, msg.Body)
|
|
|
|
|
|
|
|
case contentRenderedMsg:
|
|
|
|
m.state = stateShowDocument
|
|
|
|
m.pager.SetContent(string(msg))
|
2020-05-14 19:06:13 +00:00
|
|
|
return m, nil
|
2020-05-15 19:08:45 +00:00
|
|
|
|
2020-05-13 20:00:27 +00:00
|
|
|
}
|
2020-05-11 20:13:16 +00:00
|
|
|
|
2020-05-13 20:00:27 +00:00
|
|
|
switch m.state {
|
|
|
|
|
|
|
|
case stateKeygenRunning:
|
|
|
|
mdl, cmd := keygen.Update(msg, boba.Model(m.keygen))
|
|
|
|
keygenModel, ok := mdl.(keygen.Model)
|
|
|
|
if !ok {
|
|
|
|
m.err = errors.New("could not perform assertion on keygen model in main update")
|
|
|
|
return m, boba.Quit
|
2020-05-11 20:13:16 +00:00
|
|
|
}
|
2020-05-13 20:00:27 +00:00
|
|
|
m.keygen = keygenModel
|
|
|
|
return m, cmd
|
|
|
|
|
|
|
|
case stateShowStash:
|
|
|
|
m.stash, cmd = stashUpdate(msg, m.stash)
|
|
|
|
return m, cmd
|
2020-05-14 02:08:17 +00:00
|
|
|
|
|
|
|
case stateShowDocument:
|
2020-05-15 19:08:45 +00:00
|
|
|
// Process keys (and eventually mouse) with pager.Update
|
2020-05-15 20:22:30 +00:00
|
|
|
var cmd boba.Cmd
|
|
|
|
m.pager, cmd = pager.Update(msg, m.pager)
|
2020-05-14 02:08:17 +00:00
|
|
|
return m, cmd
|
2020-05-11 20:13:16 +00:00
|
|
|
}
|
2020-05-13 16:53:58 +00:00
|
|
|
|
|
|
|
return m, nil
|
2020-05-11 20:13:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// VIEW
|
|
|
|
|
2020-05-13 16:53:58 +00:00
|
|
|
func view(mdl boba.Model) string {
|
2020-05-11 20:13:16 +00:00
|
|
|
m, ok := mdl.(model)
|
|
|
|
if !ok {
|
|
|
|
return "could not perform assertion on model in view"
|
|
|
|
}
|
|
|
|
|
|
|
|
if m.err != nil {
|
2020-05-13 20:00:27 +00:00
|
|
|
return m.err.Error() + "\n"
|
2020-05-11 20:13:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var s string
|
|
|
|
switch m.state {
|
|
|
|
case stateInitCharmClient:
|
|
|
|
s += spinner.View(m.spinner) + " Initializing..."
|
|
|
|
case stateKeygenRunning:
|
|
|
|
s += keygen.View(m.keygen)
|
|
|
|
case stateKeygenFinished:
|
|
|
|
s += spinner.View(m.spinner) + " Re-initializing..."
|
2020-05-13 16:53:58 +00:00
|
|
|
case stateShowStash:
|
2020-05-14 19:41:17 +00:00
|
|
|
return stashView(m.stash)
|
2020-05-14 02:08:17 +00:00
|
|
|
case stateShowDocument:
|
2020-05-14 19:41:17 +00:00
|
|
|
return fmt.Sprintf("\n%s\n%s", pager.View(m.pager), statusBarView(m))
|
2020-05-11 20:13:16 +00:00
|
|
|
}
|
2020-05-14 19:41:17 +00:00
|
|
|
|
|
|
|
return "\n" + indent.String(s, 2)
|
2020-05-11 20:13:16 +00:00
|
|
|
}
|
|
|
|
|
2020-05-15 01:12:13 +00:00
|
|
|
func glowLogoView(text string) string {
|
|
|
|
return te.String(text).
|
2020-05-14 19:06:13 +00:00
|
|
|
Bold().
|
2020-05-14 22:03:24 +00:00
|
|
|
Foreground(glowLogoTextColor).
|
2020-05-14 19:06:13 +00:00
|
|
|
Background(common.Fuschia.Color()).
|
|
|
|
String()
|
2020-05-15 01:12:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func statusBarView(m model) string {
|
|
|
|
// Logo
|
|
|
|
logoText := " Glow "
|
|
|
|
logo := glowLogoView(logoText)
|
2020-05-14 19:06:13 +00:00
|
|
|
|
2020-05-18 21:31:10 +00:00
|
|
|
// Scroll percent
|
|
|
|
percentText := fmt.Sprintf(" %3.f%% ", m.pager.ScrollPercent()*100)
|
|
|
|
percent := te.String(percentText).
|
|
|
|
Foreground(statusBarFg.Color()).
|
|
|
|
Background(statusBarBg.Color()).
|
|
|
|
String()
|
|
|
|
|
2020-05-14 19:06:13 +00:00
|
|
|
// Note
|
2020-05-18 22:58:19 +00:00
|
|
|
noteText := m.currentDocument.Note
|
2020-05-14 19:06:13 +00:00
|
|
|
if len(noteText) == 0 {
|
|
|
|
noteText = "(No title)"
|
|
|
|
}
|
2020-05-18 21:31:10 +00:00
|
|
|
noteText = truncate(" "+noteText+" ", max(0, m.terminalWidth-len(logoText)-len(percentText)))
|
2020-05-14 19:06:13 +00:00
|
|
|
note := te.String(noteText).
|
|
|
|
Foreground(statusBarFg.Color()).
|
|
|
|
Background(statusBarBg.Color()).String()
|
|
|
|
|
2020-05-14 19:41:17 +00:00
|
|
|
// Empty space
|
|
|
|
emptyCell := te.String(" ").Background(statusBarBg.Color()).String()
|
2020-05-18 21:31:10 +00:00
|
|
|
padding := max(0, m.terminalWidth-len(logoText)-len(noteText)-len(percentText))
|
|
|
|
emptySpace := strings.Repeat(emptyCell, padding)
|
2020-05-14 19:06:13 +00:00
|
|
|
|
2020-05-14 19:41:17 +00:00
|
|
|
return logo + note + emptySpace + percent
|
2020-05-14 19:06:13 +00:00
|
|
|
}
|
|
|
|
|
2020-05-11 20:13:16 +00:00
|
|
|
// COMMANDS
|
2020-05-18 21:53:46 +00:00
|
|
|
|
|
|
|
func listenForTerminalResize() boba.Cmd {
|
|
|
|
return boba.OnResize(func() boba.Msg {
|
|
|
|
return terminalResizedMsg{}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-05-15 00:23:11 +00:00
|
|
|
func getTerminalSize() boba.Cmd {
|
|
|
|
return boba.GetTerminalSize(func(w, h int, err error) boba.TerminalSizeMsg {
|
|
|
|
return terminalSizeMsg{width: w, height: h, err: err}
|
|
|
|
})
|
|
|
|
}
|
2020-05-11 20:13:16 +00:00
|
|
|
|
2020-05-13 16:53:58 +00:00
|
|
|
func newCharmClient() boba.Msg {
|
2020-05-11 20:13:16 +00:00
|
|
|
cfg, err := charm.ConfigFromEnv()
|
|
|
|
if err != nil {
|
|
|
|
return fatalErrMsg(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
cc, err := charm.NewClient(cfg)
|
|
|
|
if err == charm.ErrMissingSSHAuth {
|
|
|
|
return sshAuthErrMsg{}
|
|
|
|
} else if err != nil {
|
|
|
|
return fatalErrMsg(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return newCharmClientMsg(cc)
|
|
|
|
}
|
2020-05-14 00:21:37 +00:00
|
|
|
|
2020-05-15 19:08:45 +00:00
|
|
|
func renderWithGlamour(m model, md string) boba.Cmd {
|
|
|
|
return func() boba.Msg {
|
|
|
|
s, err := glamourRender(m, md)
|
|
|
|
if err != nil {
|
|
|
|
return errMsg(err)
|
|
|
|
}
|
|
|
|
return contentRenderedMsg(s)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-14 00:21:37 +00:00
|
|
|
// ETC
|
|
|
|
|
2020-05-15 19:08:45 +00:00
|
|
|
// This is where the magic happens
|
|
|
|
func glamourRender(m model, markdown string) (string, error) {
|
|
|
|
|
2020-05-15 23:47:34 +00:00
|
|
|
if os.Getenv("GLOW_DISABLE_GLAMOUR") != "" {
|
|
|
|
return markdown, nil
|
|
|
|
}
|
|
|
|
|
2020-05-15 19:08:45 +00:00
|
|
|
// initialize glamour
|
|
|
|
var gs glamour.TermRendererOption
|
|
|
|
if m.style == "auto" {
|
|
|
|
gs = glamour.WithAutoStyle()
|
|
|
|
} else {
|
|
|
|
gs = glamour.WithStylePath(m.style)
|
|
|
|
}
|
|
|
|
|
|
|
|
r, err := glamour.NewTermRenderer(
|
|
|
|
gs,
|
2020-05-18 22:58:19 +00:00
|
|
|
glamour.WithWordWrap(min(120, m.terminalWidth)),
|
2020-05-15 19:08:45 +00:00
|
|
|
)
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-05-14 00:21:37 +00:00
|
|
|
func min(a, b int) int {
|
|
|
|
if a < b {
|
|
|
|
return a
|
|
|
|
}
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
|
|
|
func max(a, b int) int {
|
|
|
|
if a > b {
|
|
|
|
return a
|
|
|
|
}
|
|
|
|
return b
|
|
|
|
}
|