mirror of
https://github.com/charmbracelet/glow
synced 2024-12-14 14:12:27 +00:00
Handle stash failures
This commit is contained in:
parent
426f4787e5
commit
617f0980d8
3 changed files with 30 additions and 15 deletions
|
@ -49,10 +49,6 @@ var (
|
|||
|
||||
type contentRenderedMsg string
|
||||
type noteSavedMsg *charm.Markdown
|
||||
type stashSuccessMsg markdown
|
||||
type stashErrMsg struct{ err error }
|
||||
|
||||
func (s stashErrMsg) Error() string { return s.err.Error() }
|
||||
|
||||
type pagerState int
|
||||
|
||||
|
@ -316,8 +312,8 @@ func (m pagerModel) update(msg tea.Msg) (pagerModel, tea.Cmd) {
|
|||
m.stashedDocument = &md
|
||||
}
|
||||
|
||||
case stashErrMsg:
|
||||
// TODO
|
||||
case stashFailMsg:
|
||||
delete(m.general.filesStashed, msg.markdown.localID)
|
||||
|
||||
case statusMessageTimeoutMsg:
|
||||
m.state = pagerStateBrowse
|
||||
|
|
|
@ -572,7 +572,6 @@ func (m stashModel) update(msg tea.Msg) (stashModel, tea.Cmd) {
|
|||
}
|
||||
}
|
||||
|
||||
// Something was stashed. Add it to the stash listing.
|
||||
case stashSuccessMsg:
|
||||
md := markdown(msg)
|
||||
m.addMarkdowns(&md)
|
||||
|
@ -582,6 +581,9 @@ func (m stashModel) update(msg tea.Msg) (stashModel, tea.Cmd) {
|
|||
}
|
||||
cmds = append(cmds, m.newStatusMessage("Stashed!"))
|
||||
|
||||
case stashFailMsg:
|
||||
cmds = append(cmds, m.newStatusMessage("Couldn’t stash :("))
|
||||
|
||||
case statusMessageTimeoutMsg:
|
||||
if applicationContext(msg) == stashContext {
|
||||
m.hideStatusMessage()
|
||||
|
@ -727,6 +729,10 @@ func (m *stashModel) handleDocumentBrowsing(msg tea.Msg) tea.Cmd {
|
|||
|
||||
md := m.selectedMarkdown()
|
||||
|
||||
if !stashableDocTypes.Contains(md.markdownType) {
|
||||
break
|
||||
}
|
||||
|
||||
if _, alreadyStashed := m.general.filesStashed[md.localID]; alreadyStashed {
|
||||
cmds = append(cmds, m.newStatusMessage("Already stashed"))
|
||||
break
|
||||
|
|
29
ui/ui.go
29
ui/ui.go
|
@ -93,6 +93,13 @@ type stashLoadErrMsg struct{ err error }
|
|||
type gotNewsMsg []*charm.Markdown
|
||||
type statusMessageTimeoutMsg applicationContext
|
||||
type newsLoadErrMsg struct{ err error }
|
||||
type stashSuccessMsg markdown
|
||||
type stashFailMsg struct {
|
||||
err error
|
||||
markdown markdown
|
||||
}
|
||||
|
||||
//type stashErrMsg struct{ err error }
|
||||
|
||||
func (e errMsg) Error() string { return e.err.Error() }
|
||||
func (e errMsg) Unwrap() error { return e.err }
|
||||
|
@ -422,9 +429,10 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
|||
cmds = append(cmds, findNextLocalFile(m))
|
||||
|
||||
case stashSuccessMsg:
|
||||
// Something was stashed. Update the stash listing but don't run an
|
||||
// actual update on the stash since we don't want to trigger the status
|
||||
// message and generally don't want any other effects.
|
||||
// Something was stashed outside the file listing view. Update the
|
||||
// stash listing but don't run an actual update on the stash since we
|
||||
// don't want to trigger the status message and generally don't want
|
||||
// any other effects.
|
||||
if m.state == stateShowDocument {
|
||||
md := markdown(msg)
|
||||
m.stash.addMarkdowns(&md)
|
||||
|
@ -435,6 +443,9 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
|||
}
|
||||
}
|
||||
|
||||
case stashFailMsg:
|
||||
delete(m.general.filesStashed, msg.markdown.localID)
|
||||
|
||||
case filteredMarkdownMsg:
|
||||
if m.state == stateShowDocument {
|
||||
newStashModel, cmd := m.stash.update(msg)
|
||||
|
@ -653,7 +664,7 @@ func stashDocument(cc *charm.Client, md markdown) tea.Cmd {
|
|||
if debug {
|
||||
log.Println("error stashing document:", err)
|
||||
}
|
||||
return stashErrMsg{err}
|
||||
return stashFailMsg{err, md}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -669,21 +680,23 @@ func stashDocument(cc *charm.Client, md markdown) tea.Cmd {
|
|||
if debug {
|
||||
log.Println("error loading document body for stashing:", err)
|
||||
}
|
||||
return stashErrMsg{err}
|
||||
return stashFailMsg{err, md}
|
||||
}
|
||||
md.Body = string(data)
|
||||
|
||||
case NewsDoc:
|
||||
newMD, err := loadMarkdownFromCharm(cc, md.ID, md.markdownType)
|
||||
if err != nil {
|
||||
return stashErrMsg{err}
|
||||
return stashFailMsg{err, md}
|
||||
}
|
||||
md.Body = newMD.Body
|
||||
|
||||
default:
|
||||
err := fmt.Errorf("user is attempting to stash an unsupported markdown type: %s", md.markdownType)
|
||||
if debug {
|
||||
log.Printf("user is attempting to stash an unsupported markdown type: %s", md.markdownType)
|
||||
log.Println(err)
|
||||
}
|
||||
return stashFailMsg{err, md}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -698,7 +711,7 @@ func stashDocument(cc *charm.Client, md markdown) tea.Cmd {
|
|||
if debug {
|
||||
log.Println("error stashing document:", err)
|
||||
}
|
||||
return stashErrMsg{err}
|
||||
return stashFailMsg{err, md}
|
||||
}
|
||||
|
||||
// The server sends the whole stashed document back, but we really just
|
||||
|
|
Loading…
Reference in a new issue