This repository has been archived by the owner on Jan 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
contents.go
65 lines (53 loc) · 1.4 KB
/
contents.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
package main
import (
"encoding/json"
"io"
)
type Item struct {
Title string
Note string
}
type Content struct {
Titles []string
Items [][]Item
}
func NewContentIo(r io.Reader) *Content {
decoder := json.NewDecoder(r)
c := &Content{}
if err := decoder.Decode(c); err != nil {
return nil
}
return c
}
func NewContentDefault() *Content {
ret := &Content{}
ret.Titles = []string{"To Do", "Doing", "Done"}
ret.Items = make([][]Item, 3)
return ret
}
func (c *Content) GetNumLanes() int {
return len(c.Titles)
}
func (c *Content) GetLaneTitle(idx int) string {
return c.Titles[idx]
}
func (c *Content) GetLaneItems(idx int) []Item {
return c.Items[idx]
}
func (c *Content) MoveItem(fromlane, fromidx, tolane, toidx int) {
item := c.Items[fromlane][fromidx]
// https://github.com/golang/go/wiki/SliceTricks
c.Items[fromlane] = append(c.Items[fromlane][:fromidx], c.Items[fromlane][fromidx+1:]...)
c.Items[tolane] = append(c.Items[tolane][:toidx], append([]Item{item}, c.Items[tolane][toidx:]...)...)
}
func (c *Content) DelItem(lane, idx int) {
c.Items[lane] = append(c.Items[lane][:idx], c.Items[lane][idx+1:]...)
}
func (c *Content) AddItem(lane, idx int, title string) {
c.Items[lane] = append(c.Items[lane][:idx], append([]Item{Item{title, ""}}, c.Items[lane][idx:]...)...)
}
func (c *Content) Save(w io.Writer) {
encoder := json.NewEncoder(w)
encoder.SetIndent("", " ")
encoder.Encode(c)
}