-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlist_inbox_action.go
127 lines (116 loc) · 2.35 KB
/
list_inbox_action.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"sort"
"strings"
"github.com/tobstarr/wlcli/Godeps/_workspace/src/github.com/dynport/gocli"
"github.com/tobstarr/wlcli/wlclient"
)
type listInboxAction struct {
listID int
Tag string `cli:"opt --tag"`
Limit int `cli:"opt --limit default=25"`
All bool `cli:"opt --all"`
Full bool `cli:"opt --full"`
}
// this should be a generic inbox action
func (r *listInboxAction) Run() error {
cl, err := loadClient()
if err != nil {
return err
}
listID := r.listID
if listID == 0 {
ib, err := cl.Inbox()
if err != nil {
return err
}
listID = ib.ID
}
if dataOnStdin() {
b, err := ioutil.ReadAll(os.Stdin)
if err != nil {
return err
}
task, err := cl.CreateTask(&wlclient.Task{ListID: listID, Title: strings.TrimSpace(string(b))})
if err != nil {
return err
}
return json.NewEncoder(os.Stdout).Encode(task)
return nil
}
tasks, err := cl.Tasks(wlclient.ListID(listID))
if err != nil {
return err
}
pos, err := cl.TaskPositions(listID)
if err != nil {
return err
}
if len(pos) == 0 {
return fmt.Errorf("unable to get positions for list %d", listID)
}
if len(tasks) == 0 {
fmt.Printf("no tasks found. use `wlcli push` to create one\n")
return nil
}
sort.Sort(tasks)
t := gocli.NewTable()
tm := map[int]*wlclient.Task{}
for _, t := range tasks {
tm[t.ID] = t
}
positions := pos[0].Values
for i, p := range positions {
task, ok := tm[p]
if !ok {
continue
}
delete(tm, p)
if r.Tag != "" && !matchesTag(task.Title, r.Tag) {
continue
}
title := task.Title
if !r.Full {
title = truncate(title, 64)
}
if i < r.Limit && !r.All {
t.Add(task.ID, title)
}
}
for _, task := range tm {
if r.Tag != "" && !matchesTag(task.Title, r.Tag) {
continue
}
t.Add(task.ID, truncate(task.Title, 64))
}
fmt.Println(t)
return nil
}
func dataOnStdin() bool {
stat, err := os.Stdin.Stat()
if err != nil {
return false
}
return (stat.Mode() & os.ModeCharDevice) == 0
}
// this is naive!!!
func truncate(in string, max int) string {
parts := strings.Split(in, "")
if len(parts) < max {
return in
}
return strings.Join(parts[0:max], "")
}
func matchesTag(text, tag string) bool {
tag = strings.ToLower(tag)
for _, f := range strings.Fields(strings.ToLower(text)) {
if f == "#"+tag {
return true
}
}
return false
}