From 2f80bdc65de44581c4497107a092c73f39ae0b62 Mon Sep 17 00:00:00 2001 From: Seth Rider Date: Sat, 5 Aug 2023 20:18:16 +0100 Subject: [PATCH] add description (show, add, modify) adds --description option to add and modify commands description listed in `show` command --- add.go | 1 + format.go | 7 +++++++ lib/interface.go | 4 ++++ lib/item.go | 30 ++++++++++++++++++++++++++---- main.go | 8 ++++++++ modify.go | 1 + show.go | 15 ++++++++------- 7 files changed, 55 insertions(+), 11 deletions(-) diff --git a/add.go b/add.go index 5f4932a..fcdb0df 100644 --- a/add.go +++ b/add.go @@ -24,6 +24,7 @@ func Add(c *cli.Context) error { } item.Content = c.Args().First() + item.Description = c.String("description") item.Priority = priorityMapping[c.Int("priority")] item.ProjectID = c.String("project-id") if item.ProjectID == "" { diff --git a/format.go b/format.go index 985ccea..799f616 100644 --- a/format.go +++ b/format.go @@ -65,6 +65,13 @@ func ContentFormat(item todoist.ContentCarrier) string { return todoist.GetContentTitle(item) } +func DescriptionFormat(item todoist.DescriptionCarrier) string { + if todoist.HasURL(item) { + return color.New(color.Underline).SprintFunc()(todoist.GetDescription(item)) + } + return todoist.GetDescription(item) +} + func PriorityFormat(priority int) string { priorityColor := color.New(color.Bold) var p int diff --git a/lib/interface.go b/lib/interface.go index 2540f04..69ab999 100644 --- a/lib/interface.go +++ b/lib/interface.go @@ -66,6 +66,10 @@ type ContentCarrier interface { GetContent() string } +type DescriptionCarrier interface { + GetDescription() string +} + type ProjectIDCarrier interface { GetProjectID() string } diff --git a/lib/item.go b/lib/item.go index bc807d2..738381f 100644 --- a/lib/item.go +++ b/lib/item.go @@ -28,14 +28,19 @@ type Due struct { type BaseItem struct { HaveID HaveProjectID - Content string `json:"content"` - UserID string `json:"user_id"` + Content string `json:"content"` + Description string `json:"description"` + UserID string `json:"user_id"` } func (bitem BaseItem) GetContent() string { return bitem.Content } +func (bitem BaseItem) GetDescription() string { + return bitem.Description +} + type CompletedItem struct { BaseItem CompletedData string `json:"completed_at"` @@ -132,6 +137,10 @@ func GetContentTitle(item ContentCarrier) string { return linkRegex.ReplaceAllString(item.GetContent(), "$1") } +func GetDescription(item DescriptionCarrier) string { + return linkRegex.ReplaceAllString(item.GetDescription(), "$1") +} + func GetContentURL(item ContentCarrier) []string { if HasURL(item) { matches := linkRegex.FindAllStringSubmatch(item.GetContent(), -1) @@ -146,8 +155,15 @@ func GetContentURL(item ContentCarrier) []string { return []string{} } -func HasURL(item ContentCarrier) bool { - return linkRegex.MatchString(item.GetContent()) +func HasURL(item interface{}) bool { + switch item.(type) { + case ContentCarrier: + return linkRegex.MatchString(item.(ContentCarrier).GetContent()) + case DescriptionCarrier: + return linkRegex.MatchString(item.(DescriptionCarrier).GetDescription()) + default: + return false + } } func (item Item) AddParam() interface{} { @@ -155,6 +171,9 @@ func (item Item) AddParam() interface{} { if item.Content != "" { param["content"] = item.Content } + if item.Description != "" { + param["description"] = item.Description + } if item.DateString != "" { param["date_string"] = item.DateString } @@ -183,6 +202,9 @@ func (item Item) UpdateParam() interface{} { if item.Content != "" { param["content"] = item.Content } + if item.Description != "" { + param["description"] = item.Description + } if item.DateString != "" { param["date_string"] = item.DateString } diff --git a/main.go b/main.go index 3fc9175..e2631fb 100644 --- a/main.go +++ b/main.go @@ -56,6 +56,12 @@ func main() { Value: 4, Usage: "priority (1-4)", } + descriptionFlag := cli.StringFlag{ + Name: "description", + Aliases: []string{}, + Value: "", + Usage: "task description", + } labelNamesFlag := cli.StringFlag{ Name: "label-names", Aliases: []string{"L"}, @@ -262,6 +268,7 @@ func main() { Action: Add, Flags: []cli.Flag{ &priorityFlag, + &descriptionFlag, &labelNamesFlag, &projectIDFlag, &projectNameFlag, @@ -278,6 +285,7 @@ func main() { Flags: []cli.Flag{ &contentFlag, &priorityFlag, + &descriptionFlag, &labelNamesFlag, &projectIDFlag, &projectNameFlag, diff --git a/modify.go b/modify.go index 68b8e41..97b40fc 100644 --- a/modify.go +++ b/modify.go @@ -25,6 +25,7 @@ func Modify(c *cli.Context) error { return IdNotFound } item.Content = c.String("content") + item.Description = c.String("description") item.Priority = priorityMapping[c.Int("priority")] item.LabelNames = func(str string) []string { stringNames := strings.Split(str, ",") diff --git a/show.go b/show.go index 0f06a67..0565bdd 100644 --- a/show.go +++ b/show.go @@ -26,13 +26,14 @@ func Show(c *cli.Context) error { projectColorHash := GenerateColorHash(projectIds, colorList) records := [][]string{ - []string{"ID", IdFormat(item)}, - []string{"Content", ContentFormat(item)}, - []string{"Project", ProjectFormat(item.ProjectID, client.Store, projectColorHash, c)}, - []string{"Labels", item.LabelsString(client.Store)}, - []string{"Priority", PriorityFormat(item.Priority)}, - []string{"DueDate", DueDateFormat(item.DateTime(), item.AllDay)}, - []string{"URL", strings.Join(todoist.GetContentURL(item), ",")}, + {"ID", IdFormat(item)}, + {"Content", ContentFormat(item)}, + {"Description", DescriptionFormat(item)}, + {"Project", ProjectFormat(item.ProjectID, client.Store, projectColorHash, c)}, + {"Labels", item.LabelsString(client.Store)}, + {"Priority", PriorityFormat(item.Priority)}, + {"DueDate", DueDateFormat(item.DateTime(), item.AllDay)}, + {"URL", strings.Join(todoist.GetContentURL(item), ",")}, } defer writer.Flush()