In a filter, deleting stashed news removes those items from the filter

This commit is contained in:
Christian Rocha 2020-12-16 17:49:48 -05:00
parent 0c3df027cc
commit 3c74079669
4 changed files with 54 additions and 29 deletions

View file

@ -5,7 +5,8 @@ type DocType int
// Available document types.
const (
LocalDoc DocType = iota
NoDocType DocType = iota
LocalDoc
StashedDoc
ConvertedDoc
NewsDoc
@ -13,6 +14,7 @@ const (
func (d DocType) String() string {
return [...]string{
"none",
"local",
"stashed",
"converted",

View file

@ -3,6 +3,7 @@ package ui
import (
"log"
"math"
"path"
"strings"
"time"
"unicode"
@ -30,15 +31,16 @@ type markdown struct {
// which could match both an original or stashed document).
uniqueID ksuid.KSUID
// Some of the document's original values before this document was stashed.
// These are irrelevant if this document was not stashed in this session.
originalDocType DocType
originalTimestamp time.Time
originalNote string
// Full path of a local markdown file. Only relevant to local documents and
// those that have been stashed in this session.
localPath string
// Modified time of the local file. This will also be stored in
// Markdown.CreatedAt, however we also retain it here incase we need to
// convert this document back to a local document after it's been stashed.
localModTime time.Time
// Value we filter against. This exists so that we can maintain positions
// of filtered items if notes are edited while a filter is active. This
// field is ephemeral, and should only be referenced during filtering.
@ -54,6 +56,37 @@ func (m *markdown) generateIDs() {
m.uniqueID = ksuid.New()
}
// convertToStashed converts this document into its stashed state.
func (m *markdown) convertToStashed() {
if m.docType == ConvertedDoc {
if debug {
log.Println("not converting already converted document:", m)
}
return
}
m.originalDocType = m.docType
m.originalTimestamp = m.CreatedAt
m.originalNote = m.Note
if m.docType == LocalDoc {
m.Note = strings.Replace(path.Base(m.localPath), path.Ext(m.localPath), "", 1)
}
m.CreatedAt = time.Now()
m.docType = ConvertedDoc
}
// revert reverts this document from its stashed state.
func (m *markdown) revertFromStashed() {
if m.docType != ConvertedDoc {
log.Printf("not reverting document of type %s: %v", m.docType, m)
}
m.docType = m.originalDocType
m.CreatedAt = m.originalTimestamp
m.Note = m.originalNote
}
// Generate the value we're doing to filter against.
func (m *markdown) buildFilterValue() {
note, err := normalize(m.Note)

View file

@ -831,7 +831,7 @@ func (m *stashModel) handleDocumentBrowsing(msg tea.Msg) tea.Cmd {
if m.filterApplied() {
for _, v := range m.filteredMarkdowns {
if v.uniqueID == md.uniqueID {
convertMarkdownToStashed(v)
v.convertToStashed()
}
}
}
@ -941,12 +941,16 @@ func (m *stashModel) handleDeleteConfirmation(msg tea.Msg) tea.Cmd {
switch md.docType {
// If the document was stashed in this session, convert it
// back to "local" document
case ConvertedDoc:
md.docType = LocalDoc
md.Note = stripAbsolutePath(md.localPath, m.common.cwd)
md.CreatedAt = md.localModTime
// If the document was stashed in this session, convert it
// back to it's original document type
if md.originalDocType == LocalDoc {
md.revertFromStashed()
break
}
// Other documents fall through and delete as normal
fallthrough
// Otherwise, remove the document from the listing
default:

View file

@ -6,7 +6,6 @@ import (
"io/ioutil"
"log"
"os"
"path"
"strings"
"time"
@ -686,7 +685,7 @@ func stashDocument(cc *charm.Client, md markdown) tea.Cmd {
}
}
convertMarkdownToStashed(&md)
md.convertToStashed()
newMd, err := cc.StashMarkdown(md.Note, md.Body)
if err != nil {
@ -719,9 +718,8 @@ func waitForStatusMessageTimeout(appCtx applicationContext, t *time.Timer) tea.C
// a directory, but we trust that gitcha has already done that.
func localFileToMarkdown(cwd string, res gitcha.SearchResult) *markdown {
md := &markdown{
docType: LocalDoc,
localPath: res.Path,
localModTime: res.Info.ModTime(),
docType: LocalDoc,
localPath: res.Path,
Markdown: charm.Markdown{
Note: stripAbsolutePath(res.Path, cwd),
CreatedAt: res.Info.ModTime(),
@ -731,18 +729,6 @@ func localFileToMarkdown(cwd string, res gitcha.SearchResult) *markdown {
return md
}
// convertMarkdownToStashed performs some adjustments on the given markdown to
// that occur as part of stashing.
func convertMarkdownToStashed(md *markdown) {
// Set the note as the filename without the extension
if md.docType == LocalDoc {
md.Note = strings.Replace(path.Base(md.localPath), path.Ext(md.localPath), "", 1)
}
md.docType = ConvertedDoc
md.CreatedAt = time.Now()
}
func stripAbsolutePath(fullPath, cwd string) string {
return strings.Replace(fullPath, cwd+string(os.PathSeparator), "", -1)
}