Skip to content

Commit

Permalink
add description (show, add, modify)
Browse files Browse the repository at this point in the history
adds --description option to add and modify commands
description listed in `show` command
  • Loading branch information
psethwick committed Aug 5, 2023
1 parent af8652c commit 2f80bdc
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 11 deletions.
1 change: 1 addition & 0 deletions add.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 == "" {
Expand Down
7 changes: 7 additions & 0 deletions format.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions lib/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ type ContentCarrier interface {
GetContent() string
}

type DescriptionCarrier interface {
GetDescription() string
}

type ProjectIDCarrier interface {
GetProjectID() string
}
Expand Down
30 changes: 26 additions & 4 deletions lib/item.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down Expand Up @@ -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)
Expand All @@ -146,15 +155,25 @@ 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{} {
param := map[string]interface{}{}
if item.Content != "" {
param["content"] = item.Content
}
if item.Description != "" {
param["description"] = item.Description
}
if item.DateString != "" {
param["date_string"] = item.DateString
}
Expand Down Expand Up @@ -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
}
Expand Down
8 changes: 8 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"},
Expand Down Expand Up @@ -262,6 +268,7 @@ func main() {
Action: Add,
Flags: []cli.Flag{
&priorityFlag,
&descriptionFlag,
&labelNamesFlag,
&projectIDFlag,
&projectNameFlag,
Expand All @@ -278,6 +285,7 @@ func main() {
Flags: []cli.Flag{
&contentFlag,
&priorityFlag,
&descriptionFlag,
&labelNamesFlag,
&projectIDFlag,
&projectNameFlag,
Expand Down
1 change: 1 addition & 0 deletions modify.go
Original file line number Diff line number Diff line change
Expand Up @@ -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, ",")
Expand Down
15 changes: 8 additions & 7 deletions show.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down

0 comments on commit 2f80bdc

Please sign in to comment.