From 82acc627831c6dfb1e1c3f73dbb8f8db3e1bba24 Mon Sep 17 00:00:00 2001 From: Christian Rocha Date: Mon, 24 Aug 2020 16:48:10 -0400 Subject: [PATCH] Move common commands to the main UI file --- ui/pager.go | 71 +---------------------------------------------------- ui/stash.go | 2 +- ui/ui.go | 68 ++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 70 insertions(+), 71 deletions(-) diff --git a/ui/pager.go b/ui/pager.go index 8b2949a..6eb2099 100644 --- a/ui/pager.go +++ b/ui/pager.go @@ -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) { diff --git a/ui/stash.go b/ui/stash.go index 81080a4..1062df9 100644 --- a/ui/stash.go +++ b/ui/stash.go @@ -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 { diff --git a/ui/ui.go b/ui/ui.go index eddfd63..fbc6352 100644 --- a/ui/ui.go +++ b/ui/ui.go @@ -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