xonecas.io
8356
Wx

www-xonecas-io / files

www-xonecas-io / internal / site / rows.go

2.5 KB
1package site
2
3import (
4	"maps"
5	"slices"
6	"strings"
7	"time"
8	"unicode"
9
10	"xonecas.io/site/internal/project"
11)
12
13// heat is the fewest commits for each heat level after h0.
14var heat = []int{1, 15, 40, 100, 200, 500}
15
16// Rows turns the shown projects, oldest first, into list rows. Num is the order of discovery,
17// symbols are unique and go to the oldest project first, and the bar spans the whole timeline.
18func Rows(projects []project.Project) []ProjectRow {
19	first, last := projects[0].First, projects[0].Last
20	for _, p := range projects {
21		first, last = min(first, p.First), max(last, p.Last)
22	}
23	start, end := day(first), day(last)
24	span := max(end.Sub(start).Hours(), 24)
25	taken := map[string]bool{}
26	rows := make([]ProjectRow, len(projects))
27	for i, p := range projects {
28		r := ProjectRow{
29			El:     Element{Num: i + 1, Weight: p.Commits, Sym: symbol(p.Name, taken), Name: p.Name, Group: group(p.Tags), Post: slices.Contains(p.Tags, "post-AI")},
30			Desc:   p.Description,
31			From:   100 * day(p.First).Sub(start).Hours() / span,
32			Width:  100 * day(p.Last).Sub(day(p.First)).Hours() / span,
33			Tags:   p.Tags,
34			First:  p.First,
35			Last:   p.Last,
36			Months: strings.Join(slices.Sorted(maps.Keys(p.Months)), " "),
37			Topics: p.Topics,
38		}
39		for _, n := range heat {
40			if p.Commits >= n {
41				r.Heat++
42			}
43		}
44		if len(p.Languages) > 0 {
45			r.Lang = p.Languages[0].Name
46		}
47		rows[i] = r
48	}
49	return rows
50}
51
52func day(s string) time.Time {
53	t, _ := time.Parse(time.DateOnly, s)
54	return t
55}
56
57func group(tags []string) string {
58	fe, be := slices.Contains(tags, "frontend"), slices.Contains(tags, "backend")
59	switch {
60	case fe && be:
61		return "both"
62	case fe:
63		return "fe"
64	case be:
65		return "be"
66	}
67	return "none"
68}
69
70// symbol is the name's first letter and the first later letter that makes an unused pair,
71// trying the first letters of words before the rest.
72func symbol(name string, taken map[string]bool) string {
73	var initials, rest []rune
74	first, word := rune(0), false
75	for _, c := range strings.ToLower(name) {
76		switch {
77		case !unicode.IsLetter(c) && !unicode.IsDigit(c):
78			word = true
79		case first == 0:
80			first = c
81		case word:
82			initials, word = append(initials, c), false
83		default:
84			rest = append(rest, c)
85		}
86	}
87	for _, c := range append(initials, rest...) {
88		if s := string(unicode.ToUpper(first)) + string(c); !taken[s] {
89			taken[s] = true
90			return s
91		}
92	}
93	s := string(unicode.ToUpper(first))
94	taken[s] = true
95	return s
96}
97
98// TagCount is a tag and how many shown projects have it.
99type TagCount struct {
100	Name  string
101	Count int
102}