forked from wtfutil/wtf
/
widget.go
137 lines (105 loc) · 2.95 KB
/
widget.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
128
129
130
131
132
133
134
135
136
137
package gitlabtodo
import (
"fmt"
"github.com/rivo/tview"
"github.com/doctorfree/wtf/utils"
"github.com/doctorfree/wtf/view"
gitlab "github.com/xanzy/go-gitlab"
)
type Widget struct {
view.ScrollableWidget
todos []*gitlab.Todo
gitlabClient *gitlab.Client
settings *Settings
err error
}
func NewWidget(tviewApp *tview.Application, redrawChan chan bool, pages *tview.Pages, settings *Settings) *Widget {
widget := &Widget{
ScrollableWidget: view.NewScrollableWidget(tviewApp, redrawChan, pages, settings.Common),
settings: settings,
}
widget.gitlabClient, _ = gitlab.NewClient(settings.apiKey, gitlab.WithBaseURL(settings.domain))
widget.SetRenderFunction(widget.Render)
widget.initializeKeyboardControls()
return widget
}
/* -------------------- Exported Functions -------------------- */
func (widget *Widget) Refresh() {
if widget.Disabled() {
return
}
todos, err := widget.getTodos()
widget.todos = todos
widget.err = err
widget.SetItemCount(len(todos))
widget.Render()
}
// Render sets up the widget data for redrawing to the screen
func (widget *Widget) Render() {
widget.Redraw(widget.content)
}
/* -------------------- Unexported Functions -------------------- */
func (widget *Widget) content() (string, string, bool) {
title := fmt.Sprintf("GitLab ToDos (%d)", len(widget.todos))
if widget.err != nil {
return title, widget.err.Error(), true
}
if widget.todos == nil {
return title, "No ToDos to display", false
}
str := widget.contentFrom(widget.todos)
return title, str, false
}
func (widget *Widget) getTodos() ([]*gitlab.Todo, error) {
opts := gitlab.ListTodosOptions{}
todos, _, err := widget.gitlabClient.Todos.ListTodos(&opts)
if err != nil {
return nil, err
}
return todos, nil
}
// trim the todo body so it fits on a single line
func (widget *Widget) trimTodoBody(body string) string {
r := []rune(body)
// Cut at first occurrence of a newline
for i, a := range r {
if a == '\n' {
return string(r[:i])
}
}
return body
}
func (widget *Widget) contentFrom(todos []*gitlab.Todo) string {
var str string
for idx, todo := range todos {
row := fmt.Sprintf(`[%s]%2d. `, widget.RowColor(idx), idx+1)
if widget.settings.showProject {
row = fmt.Sprintf(`%s%s `, row, todo.Project.Path)
}
row = fmt.Sprintf(`%s[mediumpurple](%s)[%s] %s`,
row,
todo.Author.Username,
widget.RowColor(idx),
widget.trimTodoBody(todo.Body),
)
str += utils.HighlightableHelper(widget.View, row, idx, len(todo.Body))
}
return str
}
func (widget *Widget) markAsDone() {
sel := widget.GetSelected()
if sel >= 0 && widget.todos != nil && sel < len(widget.todos) {
todo := widget.todos[sel]
_, err := widget.gitlabClient.Todos.MarkTodoAsDone(todo.ID)
if err == nil {
widget.Refresh()
}
}
}
func (widget *Widget) openTodo() {
sel := widget.GetSelected()
if sel >= 0 && widget.todos != nil && sel < len(widget.todos) {
todo := widget.todos[sel]
utils.OpenFile(todo.TargetURL)
}
}