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

View File

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