include previous version

This commit is contained in:
Erik Winter 2021-01-29 18:10:06 +01:00
parent e5e98cfc2c
commit fef1d4a799
2 changed files with 46 additions and 5 deletions

View File

@ -17,10 +17,11 @@ const (
FOLDER_INBOX = "INBOX"
FOLDER_NEW = "New"
QUOTE_PREFIX = ">"
FIELD_SEPARATOR = ":"
FIELD_ID = "id"
FIELD_ACTION = "action"
QUOTE_PREFIX = ">"
PREVIOUS_SEPARATOR = "Previous version:"
FIELD_SEPARATOR = ":"
FIELD_ID = "id"
FIELD_ACTION = "action"
)
// Task reperesents a task based on the data stored in a message
@ -115,6 +116,9 @@ func (t *Task) FormatBody() string {
body += fmt.Sprintf("%s\n", line)
}
if t.Message != nil {
body += fmt.Sprintf("\nPrevious version:\n\n%s\n", t.Message.Body)
}
return body
}
@ -124,12 +128,18 @@ func FieldFromBody(field, body string) (string, bool) {
lines := strings.Split(body, "\n")
for _, line := range lines {
line = strings.TrimSpace(strings.TrimPrefix(line, QUOTE_PREFIX))
if line == PREVIOUS_SEPARATOR {
return value, dirty
}
parts := strings.SplitN(line, FIELD_SEPARATOR, 2)
if len(parts) < 2 {
continue
}
fieldName := strings.ToLower(strings.TrimSpace(strings.TrimPrefix(parts[0], QUOTE_PREFIX)))
fieldName := strings.ToLower(strings.TrimSpace(parts[0]))
if fieldName == field {
if value == "" {
value = strings.TrimSpace(parts[1])

View File

@ -169,10 +169,17 @@ action:
task: &task.Task{
Id: id,
Action: action,
Message: &mstore.Message{
Body: "previous body",
},
},
exp: `
id: an id
action: an action
Previous version:
previous body
`,
},
} {
@ -250,6 +257,30 @@ field: valueb
body: "> field: value",
expValue: "value",
},
{
name: "previous body",
field: "field",
body: `
field: valuea
Previous version:
field: valueb
`,
expValue: "valuea",
},
{
name: "quoted previous body",
field: "field",
body: `
field: valuea
> Previous version:
>
> field: valueb
`,
expValue: "valuea",
},
} {
t.Run(tc.name, func(t *testing.T) {
actValue, actDirty := task.FieldFromBody(tc.field, tc.body)