Skip to content

Commit

Permalink
Fix display of multiline summary (#124)
Browse files Browse the repository at this point in the history
* DRYup append of simple fields

* Extract out functionality of appending multi-line string

* Enhance indentation for multi-line summary
  • Loading branch information
goyalmunish authored May 28, 2022
1 parent 25c24a6 commit efbdfb3
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 14 deletions.
44 changes: 32 additions & 12 deletions internal/model/functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,26 @@ import (
"strings"
)

// appendMultiLineField prints a multi-line string with first line as its heading
func appendMultiLineField(fieldName, multiLineString string, appendTo []string) []string {
data := strings.Split(multiLineString, "\n")
heading, subItems := data[0], data[1:]
appendTo = append(appendTo, fmt.Sprintf(" | %12v: %v\n", fieldName, heading))
for _, e := range subItems {
e = strings.TrimSpace(e)
if e != "" {
appendTo = append(appendTo, fmt.Sprintf(" | %18v %v\n", "", e))
}
}
return appendTo
}

// appendSimpleField appends a simple (string or similar) field
func appendSimpleField(fieldName, fieldValue interface{}, appendTo []string) []string {
appendTo = append(appendTo, fmt.Sprintf(" | %12v: %v\n", fieldName, fieldValue))
return appendTo
}

// printNoteField function prints the given field of a note.
func printNoteField(fieldName string, fieldValue interface{}) string {
var strs []string
Expand All @@ -23,23 +43,23 @@ func printNoteField(fieldName string, fieldValue interface{}) string {
if items != nil {
for _, v := range items {
if strings.Contains(v, "\n") {
// the string is multi-line
data := strings.Split(v, "\n")
heading, subItems := data[0], data[1:]
strs = append(strs, fmt.Sprintf(" | %12v: %v\n", "", heading))
for _, e := range subItems {
e = strings.TrimSpace(e)
if e != "" {
strs = append(strs, fmt.Sprintf(" | %18v %v\n", "", e))
}
}
// useful for multi-line comments
strs = appendMultiLineField("", v, strs)
} else {
strs = append(strs, fmt.Sprintf(" | %12v: %v\n", "", v))
strs = appendSimpleField("", v, strs)
}
}
}
} else if fieldDynamicType == "string" {
value := fieldValue.(string)
if strings.Contains(value, "\n") {
// useful for multi-line summary
strs = appendMultiLineField(fieldName, value, strs)
} else {
strs = appendSimpleField(fieldName, fieldValue, strs)
}
} else {
strs = append(strs, fmt.Sprintf(" | %12v: %v\n", fieldName, fieldValue))
strs = appendSimpleField(fieldName, fieldValue, strs)
}
return strings.Join(strs, "")
}
Expand Down
6 changes: 4 additions & 2 deletions internal/model/model_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ func TestNotesByDueDate(t *testing.T) {
func TestNoteStrings(t *testing.T) {
utils.Location = utils.UTCLocation()
comments := model.Comments{&model.Comment{Text: "c1:\n- line 1\n\n- line 2\n- line 3"}, &model.Comment{Text: "c2"}, &model.Comment{Text: "c3"}}
note := &model.Note{Text: "dummy text", Comments: comments, Status: "pending", TagIds: []int{1, 2}, CompleteBy: 1609669235}
note := &model.Note{Text: "dummy text", Comments: comments, Status: "pending", Summary: "summary heading:\n- line 1\n- line 2", TagIds: []int{1, 2}, CompleteBy: 1609669235}
want := `[ | Text: dummy text
| Comments:
| : [nil] c1:
Expand All @@ -217,7 +217,9 @@ func TestNoteStrings(t *testing.T) {
| - line 3
| : [nil] c2
| : [nil] c3
| Summary:
| Summary: summary heading:
| - line 1
| - line 2
| Status: pending
| Tags: [1 2]
| IsMain: false
Expand Down

0 comments on commit efbdfb3

Please sign in to comment.