repos / pico

pico services - prose.sh, pastes.sh, imgs.sh, feeds.sh, pgs.sh
git clone https://github.com/picosh/pico.git

pico / tui / common
Eric Bower · 16 Jun 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) *table.Table {
13	headers := []string{"Date", "Unique Visitors"}
14	data := [][]string{}
15	for _, d := range intervals {
16		data = append(data, []string{
17			d.Interval.Format(time.RFC3339Nano),
18			fmt.Sprintf("%d", d.Visitors),
19		})
20	}
21
22	t := table.New().
23		Border(lipgloss.RoundedBorder()).
24		Headers(headers...).
25		Rows(data...)
26	return t
27}
28
29func VisitUrlsTbl(urls []*db.VisitUrl) *table.Table {
30	headers := []string{"URL", "Count"}
31	data := [][]string{}
32	for _, d := range urls {
33		data = append(data, []string{
34			d.Url,
35			fmt.Sprintf("%d", d.Count),
36		})
37	}
38
39	t := table.New().
40		Border(lipgloss.RoundedBorder()).
41		Headers(headers...).
42		Rows(data...)
43	return t
44}
45
46func VisitUrlsWithProjectTbl(projects []*db.Project, urls []*db.VisitUrl) *table.Table {
47	headers := []string{"Project", "URL", "Count"}
48	data := [][]string{}
49	for _, d := range urls {
50		if d.ProjectID == "" {
51			continue
52		}
53		projectName := ""
54		for _, project := range projects {
55			if project.ID == d.ProjectID {
56				projectName = project.Name
57			}
58		}
59		data = append(data, []string{
60			projectName,
61			d.Url,
62			fmt.Sprintf("%d", d.Count),
63		})
64	}
65
66	t := table.New().
67		Border(lipgloss.RoundedBorder()).
68		Headers(headers...).
69		Rows(data...)
70	return t
71}