-
Notifications
You must be signed in to change notification settings - Fork 0
/
report.go
283 lines (239 loc) · 5.93 KB
/
report.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
package tui
import (
"fmt"
"os"
"os/exec"
"strconv"
"time"
"github.com/gdamore/tcell/v2"
"github.com/rivo/tview"
"github.com/kraserh/energozvit/internal/storage"
)
type contentReport struct {
tui *Tui
date time.Time
data []*storage.Report
table *tview.Table
}
func newContentReport(t *Tui) *contentReport {
content := new(contentReport)
content.tui = t
content.date = t.stor.GetNextDate().AddDate(0, -1, 0)
content.data = t.stor.GetReports(content.date)
content.table = tview.NewTable().
SetSelectable(false, false)
content.setKeybinding()
return content
}
func (c *contentReport) GetName() string {
return "reports"
}
func (c *contentReport) GetMenuName() string {
return "Звіт"
}
func (c *contentReport) GetTitle() string {
return fmt.Sprintf("%d-%02d", c.date.Year(), c.date.Month())
}
func (c *contentReport) GetTable() *tview.Table {
return c.table
}
func (c *contentReport) GetCell(row, column int) *tview.TableCell {
// header
var colName = []string{"Назва", "Номер", "Зона", "Теперешні",
"Попередні", "Різниця", "Всього", "Примітка"}
row -= 1 // -1 header
var v string
cell := new(tview.TableCell)
if row < 0 {
// header row
v = colName[column]
cell = tview.NewTableCell(v)
} else if row < len(c.data) {
// data rows
switch column {
case 0:
v = c.data[row].Name
cell = tview.NewTableCell(v)
case 1:
v = c.data[row].Serial
cell = tview.NewTableCell(v)
case 2:
v = strconv.Itoa(c.data[row].Zone)
cell = tview.NewTableCell(v)
case 3:
v = strconv.Itoa(c.data[row].CurKwh)
cell = tview.NewTableCell(v).
SetAlign(tview.AlignRight)
case 4:
v = strconv.Itoa(c.data[row].PreKwh)
cell = tview.NewTableCell(v).
SetAlign(tview.AlignRight)
case 5:
v = strconv.Itoa(c.data[row].Diff)
cell = tview.NewTableCell(v).
SetAlign(tview.AlignRight)
case 6:
v = strconv.Itoa(c.data[row].Energy)
cell = tview.NewTableCell(v).
SetAlign(tview.AlignRight)
case 7:
v = c.data[row].Annotation
cell = tview.NewTableCell(v)
}
} else {
// total row
switch {
case column == 6 && row == len(c.data):
cell = tview.NewTableCell("------").
SetAlign(tview.AlignRight)
case column == 6 && row == len(c.data)+1:
v = strconv.Itoa(c.tui.stor.GetTotal(c.date, c.date))
cell = tview.NewTableCell(v).
SetAlign(tview.AlignRight)
default:
cell = tview.NewTableCell("")
}
}
return cell
}
func (c *contentReport) GetRowCount() int {
return len(c.data) + 3 // +1 header row and +2 total row
}
func (c *contentReport) GetColumnCount() int {
return 8
}
func (c *contentReport) GetKeybindingString() string {
return "m/M: Місяць, y/Y: Рік, z: Останній звіт a: Додатково"
}
func (c *contentReport) NeedToSave() bool {
return false
}
func (c *contentReport) RereadTable() {
c.lastDate()
}
func (c *contentReport) setKeybinding() {
c.table.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
switch event.Rune() {
case 'm':
c.prevMonth()
case 'M':
c.nextMonth()
case 'y':
c.prevYear()
case 'Y':
c.nextYear()
case 'z':
c.lastDate()
case 'a':
c.additional()
}
return event
})
}
func (c *contentReport) nextMonth() {
c.date = c.date.AddDate(0, 1, 0)
c.data = c.tui.stor.GetReports(c.date)
c.tui.updateTable(c)
}
func (c *contentReport) prevMonth() {
c.date = c.date.AddDate(0, -1, 0)
c.data = c.tui.stor.GetReports(c.date)
c.tui.updateTable(c)
}
func (c *contentReport) nextYear() {
c.date = c.date.AddDate(1, 0, 0)
c.data = c.tui.stor.GetReports(c.date)
c.tui.updateTable(c)
}
func (c *contentReport) prevYear() {
c.date = c.date.AddDate(-1, 0, 0)
c.data = c.tui.stor.GetReports(c.date)
c.tui.updateTable(c)
}
func (c *contentReport) lastDate() {
c.date = c.tui.stor.GetNextDate().AddDate(0, -1, 0)
c.data = c.tui.stor.GetReports(c.date)
c.tui.updateTable(c)
}
func (c *contentReport) additional() {
dialog := newDialogAdditional(c.tui, c.date)
c.tui.addAndSwitchToDialog(dialog)
}
////////////////////////////////////////////////////////////////////////
type dialogAdditional struct {
list *tview.List
}
func newDialogAdditional(t *Tui, date time.Time) *dialogAdditional {
dialog := &dialogAdditional{
list: tview.NewList(),
}
files, err := os.ReadDir(".")
if err != nil {
panic(err)
}
var cmdFiles []string
for _, file := range files {
fileInfo, err := os.Stat(file.Name())
if err != nil {
continue
}
mode := fileInfo.Mode()
if mode.IsRegular() && ((mode.Perm() & 0111) != 0) {
cmdFiles = append(cmdFiles, file.Name())
}
}
if len(cmdFiles) > 0 {
list := dialog.list
for i, name := range cmdFiles {
shotcut := rune(i + int('1'))
list.AddItem(name, "", shotcut, nil)
}
list.SetSelectedFunc(func(_ int, cmdName, _ string, _ rune) {
yymm := fmt.Sprintf("%d-%02d",
date.Year(), int(date.Month()))
t.execCommand("./"+cmdName, yymm)
t.closeDialog(dialog)
})
list.SetDoneFunc(func() {
t.closeDialog(dialog)
})
}
return dialog
}
func (d *dialogAdditional) GetTitle() string {
return "Додаткові команди"
}
func (d *dialogAdditional) GetPrimitive() tview.Primitive {
return d.list
}
func (d *dialogAdditional) GetBox() *tview.Box {
return d.list.Box
}
func (d *dialogAdditional) SetOkFunc(f func()) {
}
func (d *dialogAdditional) SetCancelFunc(f func()) {
}
// Виконує команду операційної системи
// Перший аргумент завжди зберігай імʼя бази даних
func (t *Tui) execCommand(cmdName string, args ...string) {
pwd, err := os.Getwd()
if err != nil {
panic(err)
}
argsMod := []string{t.stor.GetFilepath()}
argsMod = append(argsMod, args...)
t.app.Suspend(func() {
cmd := exec.Command(cmdName, argsMod...)
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err = cmd.Run()
if err != nil {
t.ErrorShow(err)
}
})
err = os.Chdir(pwd)
if err != nil {
panic(err)
}
}