-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathui_view.go
110 lines (91 loc) · 2.25 KB
/
ui_view.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
package gitdb
import (
"fmt"
"sort"
"strconv"
"github.com/bouggo/log"
"github.com/gogitdb/gitdb/v2/internal/db"
)
//table represents a tabular view
type table struct {
Headers []string
Rows [][]string
}
//tablulate returns a tabular representation of a Block
func tablulate(b *db.Block) *table {
t := &table{}
var jsonMap map[string]interface{}
for i, record := range b.Records() {
if err := record.Hydrate(&jsonMap); err != nil {
log.Error(err.Error())
continue
}
var row []string
if i == 0 {
t.Headers = sortHeaderFields(jsonMap)
}
for _, key := range t.Headers {
val := fmt.Sprintf("%v", jsonMap[key])
if len(val) > 40 {
val = val[0:40]
}
row = append(row, val)
}
t.Rows = append(t.Rows, row)
}
return t
}
func sortHeaderFields(recMap map[string]interface{}) []string {
// To store the keys in slice in sorted order
var keys []string
for k := range recMap {
keys = append(keys, k)
}
sort.Strings(keys)
return keys
}
//pager is used to paginate records for the UI
type pager struct {
blockPage int
recordPage int
totalBlocks int
totalRecords int
}
//set page of Pager
func (p *pager) set(blockFlag string, recordFlag string) {
log.Test("Setting pager: " + blockFlag + "," + recordFlag)
p.blockPage, _ = strconv.Atoi(blockFlag)
p.recordPage, _ = strconv.Atoi(recordFlag)
}
//NextRecordURI returns the uri for the next record
func (p *pager) NextRecordURI() string {
recordPage := p.recordPage
if p.recordPage < p.totalRecords-1 {
recordPage = p.recordPage + 1
}
return fmt.Sprintf("b%d/r%d", p.blockPage, recordPage)
}
//PrevRecordURI returns uri for the previous record
func (p *pager) PrevRecordURI() string {
recordPage := p.recordPage
if p.recordPage > 0 {
recordPage = p.recordPage - 1
}
return fmt.Sprintf("b%d/r%d", p.blockPage, recordPage)
}
//NextBlockURI returns uri for the next block
func (p *pager) NextBlockURI() string {
blockPage := p.blockPage
if p.blockPage < p.totalBlocks-1 {
blockPage = p.blockPage + 1
}
return fmt.Sprintf("b%d/r%d", blockPage, 0)
}
//PrevBlockURI returns uri for the previous block
func (p *pager) PrevBlockURI() string {
blockPage := p.blockPage
if p.blockPage > 0 {
blockPage = p.blockPage - 1
}
return fmt.Sprintf("b%d/r%d", blockPage, 0)
}