mirror of
https://github.com/charmbracelet/glow
synced 2024-11-10 06:04:18 +00:00
Load stash + fix spinner issues + render nothing useful
Note that this is we're currently building aginst unreleased versions of Charm and Boba.
This commit is contained in:
parent
da4e98f749
commit
bb6904afe2
3 changed files with 52 additions and 29 deletions
8
main.go
8
main.go
|
@ -15,6 +15,7 @@ import (
|
|||
"github.com/spf13/cobra"
|
||||
"golang.org/x/crypto/ssh/terminal"
|
||||
|
||||
"github.com/charmbracelet/boba"
|
||||
"github.com/charmbracelet/glamour"
|
||||
"github.com/charmbracelet/glow/ui"
|
||||
)
|
||||
|
@ -151,9 +152,12 @@ func execute(cmd *cobra.Command, args []string) error {
|
|||
|
||||
if len(args) == 0 {
|
||||
//return executeArg(cmd, "", os.Stdout)
|
||||
debug := os.Getenv("GLOW_DEBUG")
|
||||
if debug != "" {
|
||||
boba.UseSysLog("glow")
|
||||
}
|
||||
if err := ui.NewProgram().Start(); err != nil {
|
||||
fmt.Println(err)
|
||||
os.Exit(1)
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
|
|
18
ui/stash.go
18
ui/stash.go
|
@ -13,12 +13,14 @@ type stashErrMsg error
|
|||
|
||||
type gotStashMsg []*charm.Markdown
|
||||
|
||||
type stashSpinnerTickMsg struct{}
|
||||
|
||||
// MODEL
|
||||
|
||||
type stashState int
|
||||
|
||||
const (
|
||||
stashStateInit stashState = 1 << iota
|
||||
stashStateInit stashState = iota
|
||||
stashStateStashLoaded
|
||||
)
|
||||
|
||||
|
@ -37,10 +39,12 @@ func stashInit(cc *charm.Client) (stashModel, boba.Cmd) {
|
|||
s := spinner.NewModel()
|
||||
s.Type = spinner.Dot
|
||||
s.ForegroundColor = common.SpinnerColor
|
||||
s.CustomMsgFunc = newSpinnerTickMsg
|
||||
|
||||
m := stashModel{
|
||||
cc: cc,
|
||||
spinner: s,
|
||||
page: 1,
|
||||
}
|
||||
|
||||
return m, boba.Batch(
|
||||
|
@ -48,6 +52,9 @@ func stashInit(cc *charm.Client) (stashModel, boba.Cmd) {
|
|||
spinner.Tick(s),
|
||||
)
|
||||
}
|
||||
func newSpinnerTickMsg() boba.Msg {
|
||||
return stashSpinnerTickMsg{}
|
||||
}
|
||||
|
||||
// UPDATE
|
||||
|
||||
|
@ -59,15 +66,14 @@ func stashUpdate(msg boba.Msg, m stashModel) (stashModel, boba.Cmd) {
|
|||
|
||||
case gotStashMsg:
|
||||
m.documents = msg
|
||||
m.state |= stashStateStashLoaded
|
||||
m.state = stashStateStashLoaded
|
||||
|
||||
case spinner.TickMsg:
|
||||
if (m.state & stashStateStashLoaded) == 0 {
|
||||
case stashSpinnerTickMsg:
|
||||
if m.state == stashStateInit {
|
||||
var cmd boba.Cmd
|
||||
m.spinner, cmd = spinner.Update(msg, m.spinner)
|
||||
return m, cmd
|
||||
}
|
||||
return m, nil
|
||||
}
|
||||
|
||||
return m, nil
|
||||
|
@ -78,7 +84,7 @@ func stashUpdate(msg boba.Msg, m stashModel) (stashModel, boba.Cmd) {
|
|||
func stashView(m stashModel) string {
|
||||
var s string
|
||||
if (m.state & stashStateStashLoaded) != 0 {
|
||||
|
||||
return "Loaded stash."
|
||||
}
|
||||
s += spinner.View(m.spinner) + " Loading stash..."
|
||||
return s + "\n"
|
||||
|
|
55
ui/ui.go
55
ui/ui.go
|
@ -31,6 +31,7 @@ const (
|
|||
stateKeygenRunning
|
||||
stateKeygenFinished
|
||||
stateShowStash
|
||||
stateQuitting
|
||||
)
|
||||
|
||||
type model struct {
|
||||
|
@ -40,6 +41,7 @@ type model struct {
|
|||
keygen keygen.Model
|
||||
state state
|
||||
err error
|
||||
stash stashModel
|
||||
}
|
||||
|
||||
// INIT
|
||||
|
@ -66,14 +68,15 @@ func update(msg boba.Msg, mdl boba.Model) (boba.Model, boba.Cmd) {
|
|||
return model{err: errors.New("could not perform assertion on model in update")}, boba.Quit
|
||||
}
|
||||
|
||||
var cmd boba.Cmd
|
||||
|
||||
switch msg := msg.(type) {
|
||||
|
||||
case boba.KeyMsg:
|
||||
switch msg.String() {
|
||||
case "ctrl+c":
|
||||
m.state = stateQuitting
|
||||
return m, boba.Quit
|
||||
default:
|
||||
return m, nil
|
||||
}
|
||||
|
||||
case fatalErrMsg:
|
||||
|
@ -92,16 +95,20 @@ func update(msg boba.Msg, mdl boba.Model) (boba.Model, boba.Cmd) {
|
|||
return m, keygen.GenerateKeys
|
||||
}
|
||||
|
||||
// The keygen didn't work
|
||||
// The keygen didn't work and we can't auth
|
||||
m.err = errors.New("SSH authentication failed")
|
||||
return m, boba.Quit
|
||||
|
||||
case spinner.TickMsg:
|
||||
if m.state == stateInitCharmClient {
|
||||
var cmd boba.Cmd
|
||||
switch m.state {
|
||||
case stateInitCharmClient:
|
||||
m.spinner, cmd = spinner.Update(msg, m.spinner)
|
||||
return m, cmd
|
||||
}
|
||||
return m, cmd
|
||||
|
||||
case stashSpinnerTickMsg:
|
||||
m.stash, cmd = stashUpdate(msg, m.stash)
|
||||
return m, cmd
|
||||
|
||||
case keygen.DoneMsg:
|
||||
m.state = stateKeygenFinished
|
||||
|
@ -110,21 +117,25 @@ func update(msg boba.Msg, mdl boba.Model) (boba.Model, boba.Cmd) {
|
|||
case newCharmClientMsg:
|
||||
m.cc = msg
|
||||
m.state = stateShowStash
|
||||
return m, nil
|
||||
m.stash, cmd = stashInit(m.cc)
|
||||
return m, cmd
|
||||
}
|
||||
|
||||
default:
|
||||
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
|
||||
}
|
||||
m.keygen = keygenModel
|
||||
return m, cmd
|
||||
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
|
||||
}
|
||||
return m, nil
|
||||
m.keygen = keygenModel
|
||||
return m, cmd
|
||||
|
||||
case stateShowStash:
|
||||
m.stash, cmd = stashUpdate(msg, m.stash)
|
||||
return m, cmd
|
||||
}
|
||||
|
||||
return m, nil
|
||||
|
@ -139,7 +150,7 @@ func view(mdl boba.Model) string {
|
|||
}
|
||||
|
||||
if m.err != nil {
|
||||
return m.err.Error()
|
||||
return m.err.Error() + "\n"
|
||||
}
|
||||
|
||||
var s string
|
||||
|
@ -151,7 +162,9 @@ func view(mdl boba.Model) string {
|
|||
case stateKeygenFinished:
|
||||
s += spinner.View(m.spinner) + " Re-initializing..."
|
||||
case stateShowStash:
|
||||
s += "Ready."
|
||||
s += stashView(m.stash)
|
||||
case stateQuitting:
|
||||
s += "Thanks for using Glow!"
|
||||
}
|
||||
return s + "\n"
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue