Eric Bower
·
29 Nov 24
tbl.go
1package common
2
3import (
4 "fmt"
5 "time"
6
7 "github.com/charmbracelet/lipgloss"
8 "github.com/charmbracelet/lipgloss/table"
9 "github.com/picosh/pico/db"
10)
11
12func UniqueVisitorsTbl(intervals []*db.VisitInterval, renderer *lipgloss.Renderer, maxWidth int) *table.Table {
13 headers := []string{"Date", "Unique Visitors"}
14 data := [][]string{}
15 sum := 0
16 for _, d := range intervals {
17 data = append(data, []string{
18 d.Interval.Format(time.DateOnly),
19 fmt.Sprintf("%d", d.Visitors),
20 })
21 sum += d.Visitors
22 }
23
24 data = append(data, []string{
25 "Total",
26 fmt.Sprintf("%d", sum),
27 })
28
29 t := table.New().
30 BorderStyle(renderer.NewStyle().BorderForeground(Indigo)).
31 Width(maxWidth).
32 Headers(headers...).
33 Rows(data...)
34 return t
35}
36
37func VisitUrlsTbl(urls []*db.VisitUrl, renderer *lipgloss.Renderer, maxWidth int) *table.Table {
38 headers := []string{"URL", "Count"}
39 data := [][]string{}
40 sum := 0
41 for _, d := range urls {
42 data = append(data, []string{
43 d.Url,
44 fmt.Sprintf("%d", d.Count),
45 })
46 sum += d.Count
47 }
48
49 data = append(data, []string{
50 "Total",
51 fmt.Sprintf("%d", sum),
52 })
53
54 t := table.New().
55 BorderStyle(renderer.NewStyle().BorderForeground(Indigo)).
56 Width(maxWidth).
57 Headers(headers...).
58 Rows(data...)
59 return t
60}