Move common commands to the main UI file

This commit is contained in:
Christian Rocha 2020-08-24 16:48:10 -04:00 committed by Christian Muehlhaeuser
commit 82acc62783
3 changed files with 70 additions and 71 deletions

View file

@ -1,12 +1,9 @@
package ui
import (
"errors"
"fmt"
"io/ioutil"
"log"
"math"
"path"
"strings"
"time"
@ -465,7 +462,7 @@ func pagerHelpView(m pagerModel, width int) (s string) {
return helpViewStyle(s)
}
// CMD
// COMMANDS
func renderWithGlamour(m pagerModel, md string) tea.Cmd {
return func() tea.Msg {
@ -480,72 +477,6 @@ func renderWithGlamour(m pagerModel, md string) tea.Cmd {
}
}
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 {
if debug {
log.Println("error saving note:", err)
}
return errMsg{err}
}
return noteSavedMsg(&charm.Markdown{ID: id, Note: note})
}
}
func stashDocument(cc *charm.Client, md markdown) tea.Cmd {
return func() tea.Msg {
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}
}
}
// Is the document missing a body? If so, it likely means it needs to
// be loaded. If the document body is really empty then we'll still
// stash it.
if len(md.Body) == 0 {
data, err := ioutil.ReadFile(md.localPath)
if err != nil {
if debug {
log.Println("error loading doucument body for stashing:", err)
}
return stashErrMsg{err}
}
md.Body = string(data)
}
// Turn local markdown into a newly stashed (converted) markdown
md.markdownType = convertedMarkdown
md.CreatedAt = time.Now()
// Set the note as the filename without the extension
p := md.localPath
md.Note = strings.Replace(path.Base(p), path.Ext(p), "", 1)
newMd, err := cc.StashMarkdown(md.Note, md.Body)
if err != nil {
if debug {
log.Println("error stashing document:", err)
}
return stashErrMsg{err}
}
// We really just need to know the ID so we can operate on this newly
// stashed markdown.
md.ID = newMd.ID
return stashSuccessMsg(md)
}
}
// This is where the magic happens.
func glamourRender(m pagerModel, markdown string) (string, error) {

View file

@ -810,7 +810,7 @@ func stashHelpViewBuilder(windowWidth int, sections ...string) string {
return s
}
// CMD
// COMMANDS
func loadRemoteMarkdown(cc *charm.Client, id int, t markdownType) tea.Cmd {
return func() tea.Msg {

View file

@ -3,8 +3,10 @@ package ui
import (
"errors"
"fmt"
"io/ioutil"
"log"
"os"
"path"
"strings"
"time"
@ -501,6 +503,72 @@ func generateSSHKeys() tea.Msg {
return keygenSuccessMsg{}
}
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 {
if debug {
log.Println("error saving note:", err)
}
return errMsg{err}
}
return noteSavedMsg(&charm.Markdown{ID: id, Note: note})
}
}
func stashDocument(cc *charm.Client, md markdown) tea.Cmd {
return func() tea.Msg {
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}
}
}
// Is the document missing a body? If so, it likely means it needs to
// be loaded. If the document body is really empty then we'll still
// stash it.
if len(md.Body) == 0 {
data, err := ioutil.ReadFile(md.localPath)
if err != nil {
if debug {
log.Println("error loading doucument body for stashing:", err)
}
return stashErrMsg{err}
}
md.Body = string(data)
}
// Turn local markdown into a newly stashed (converted) markdown
md.markdownType = convertedMarkdown
md.CreatedAt = time.Now()
// Set the note as the filename without the extension
p := md.localPath
md.Note = strings.Replace(path.Base(p), path.Ext(p), "", 1)
newMd, err := cc.StashMarkdown(md.Note, md.Body)
if err != nil {
if debug {
log.Println("error stashing document:", err)
}
return stashErrMsg{err}
}
// We really just need to know the ID so we can operate on this newly
// stashed markdown.
md.ID = newMd.ID
return stashSuccessMsg(md)
}
}
func waitForStatusMessageTimeout(appCtx applicationContext, t *time.Timer) tea.Cmd {
return func() tea.Msg {
<-t.C