Skip to content

Commit

Permalink
Display repeat nature (#117)
Browse files Browse the repository at this point in the history
* Display repeat type for each note

* Add tests for TestRepeatType

* Fix test

* Update comment
  • Loading branch information
goyalmunish authored May 13, 2022
1 parent e8d15db commit f491eac
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 7 deletions.
22 changes: 18 additions & 4 deletions internal/model/model_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -286,19 +286,19 @@ func TestExternalTexts(t *testing.T) {
comments = model.Comments{&model.Comment{Text: "c1"}, &model.Comment{Text: "foo bar"}, &model.Comment{Text: "c3"}, &model.Comment{Text: "baz"}}
notes = append(notes, &model.Note{Text: "cute brown dog", Comments: comments, Status: "done", TagIds: []int{1, 2}, CompleteBy: 1609669232})
// case 2
got := notes.ExternalTexts(0)
got := notes.ExternalTexts(0, 0, 0)
want := "[beautiful little cat {C:01, S:P, D:03-Jan-21} cute brown dog {C:04, S:D, D:03-Jan-21}]"
utils.AssertEqual(t, got, want)
// case 3
got = notes.ExternalTexts(5)
got = notes.ExternalTexts(5, 0, 0)
want = "[be... {C:01, S:P, D:03-Jan-21} cu... {C:04, S:D, D:03-Jan-21}]"
utils.AssertEqual(t, got, want)
// case 4
got = notes.ExternalTexts(15)
got = notes.ExternalTexts(15, 0, 0)
want = "[beautiful li... {C:01, S:P, D:03-Jan-21} cute brown dog {C:04, S:D, D:03-Jan-21}]"
utils.AssertEqual(t, got, want)
// case 5
got = notes.ExternalTexts(25)
got = notes.ExternalTexts(25, 0, 0)
want = "[beautiful little cat {C:01, S:P, D:03-Jan-21} cute brown dog {C:04, S:D, D:03-Jan-21}]"
utils.AssertEqual(t, got, want)
}
Expand Down Expand Up @@ -462,6 +462,20 @@ func TestUpdateStatus(t *testing.T) {
utils.AssertEqual(t, note1.Status, "pending")
}

func TestRepeatType(t *testing.T) {
repeatAnnuallyTagId := 3
repeatMonthlyTagId := 4
// create notes
note1 := model.Note{Text: "original text1", Status: "pending", TagIds: []int{1, 4}, BaseStruct: model.BaseStruct{UpdatedAt: 1600000001}}
note2 := model.Note{Text: "original text2", Status: "done", TagIds: []int{3, 5}, BaseStruct: model.BaseStruct{UpdatedAt: 1600000001}}
note3 := model.Note{Text: "original text3", Status: "done", TagIds: []int{2, 6}, BaseStruct: model.BaseStruct{UpdatedAt: 1600000001}}
// assert repeat type
utils.AssertEqual(t, note1.RepeatType(repeatAnnuallyTagId, repeatMonthlyTagId), "M")
utils.AssertEqual(t, note2.RepeatType(repeatAnnuallyTagId, repeatMonthlyTagId), "A")
utils.AssertEqual(t, note3.RepeatType(repeatAnnuallyTagId, repeatMonthlyTagId), "-")
utils.AssertEqual(t, note3.RepeatType(0, 0), "-")
}

func TestToggleMainFlag(t *testing.T) {
// create notes
note1 := model.Note{Text: "original text", Status: "pending", TagIds: []int{1, 4}, BaseStruct: model.BaseStruct{UpdatedAt: 1600000001}}
Expand Down
12 changes: 12 additions & 0 deletions internal/model/note.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,18 @@ func (note *Note) UpdateCompleteBy(text string) error {
return nil
}

// RepeatType return - (Not-repeat), A (Annual-Repeat), or M (Monthly-Repeat) string
// representing repeat-type of the note
func (note *Note) RepeatType(repeatAnnuallyTagId int, repeatMonthlyTagId int) string {
repeat := "-" // non-repeat
if utils.IntPresentInSlice(repeatAnnuallyTagId, note.TagIds) {
repeat = "A"
} else if utils.IntPresentInSlice(repeatMonthlyTagId, note.TagIds) {
repeat = "M"
}
return repeat
}

// ToggleMainFlag toggles note's main flag.
func (note *Note) ToggleMainFlag() error {
note.IsMain = !(note.IsMain)
Expand Down
8 changes: 6 additions & 2 deletions internal/model/notes.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ func (c Notes) Less(i, j int) bool { return c[i].UpdatedAt > c[j].UpdatedAt }
// ExternalTexts returns display text (that is, external representation) of list of notes
// with width of each note is truncated to maxStrLen.
// It returns empty []string if there are no notes.
func (notes Notes) ExternalTexts(maxStrLen int) []string {
// Note: You may use repeatAnnuallyTagId and repeatMonthlyTagId as 0, if they are not required
// In the output: R means "repeat-type", C means "number of comments", S means "status", and D means "due date"
func (notes Notes) ExternalTexts(maxStrLen int, repeatAnnuallyTagId int, repeatMonthlyTagId int) []string {
// assuming there are at least (on average) 100s of notes
allTexts := make([]string, 0, 100)
for _, note := range notes {
Expand All @@ -31,7 +33,9 @@ func (notes Notes) ExternalTexts(maxStrLen int) []string {
noteText = fmt.Sprintf("%v%v", noteText[0:(maxStrLen-3)], "...")
}
}
noteText = fmt.Sprintf("%*v {C:%02d, S:%v, D:%v}", -maxStrLen, noteText, len(note.Comments), strings.ToUpper(note.Status[0:1]), utils.UnixTimestampToShortTimeStr(note.CompleteBy))
noteText = fmt.Sprintf(
"%*v {R: %s, C:%02d, S:%v, D:%v}", -maxStrLen, noteText,
note.RepeatType(repeatAnnuallyTagId, repeatMonthlyTagId), len(note.Comments), strings.ToUpper(note.Status[0:1]), utils.UnixTimestampToShortTimeStr(note.CompleteBy))
allTexts = append(allTexts, noteText)
}
return allTexts
Expand Down
4 changes: 3 additions & 1 deletion internal/model/reminder_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -641,7 +641,9 @@ func (reminderData *ReminderData) PrintNotesAndAskOptions(notes Notes, tagID int
case "default":
sort.Sort(Notes(notes))
}
texts := notes.ExternalTexts(utils.TerminalWidth() - 50)
repeatAnnuallyTag := reminderData.TagFromSlug("repeat-annually")
repeatMonthlyTag := reminderData.TagFromSlug("repeat-monthly")
texts := notes.ExternalTexts(utils.TerminalWidth()-50, repeatAnnuallyTag.Id, repeatMonthlyTag.Id)
// ask user to select a note
promptText := ""
if tagID >= 0 {
Expand Down

0 comments on commit f491eac

Please sign in to comment.