From 4ca3d8a29fb6ef2e55d1c116a17c578fa4beba91 Mon Sep 17 00:00:00 2001 From: Michael McLoughlin Date: Sun, 8 May 2022 15:22:26 -0700 Subject: [PATCH] doc: format stars count (#281) Use the k format for thousands, just like Github itself. --- README.md | 8 ++++---- internal/cmd/docgen/main.go | 11 +++++++++++ internal/cmd/docgen/templates/readme.tmpl | 2 +- 3 files changed, 16 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 139020c..579a552 100644 --- a/README.md +++ b/README.md @@ -193,19 +193,19 @@ Implementations of full algorithms: Popular projects using `avo`: [golang / **go**](https://github.com/golang/go) -:star: 98831 +:star: 98.8k > The Go programming language [klauspost / **compress**](https://github.com/klauspost/compress) -:star: 2894 +:star: 2.9k > Optimized Go Compression Packages [golang / **crypto**](https://github.com/golang/crypto) -:star: 2416 +:star: 2.4k > [mirror] Go supplementary cryptography libraries [klauspost / **reedsolomon**](https://github.com/klauspost/reedsolomon) -:star: 1444 +:star: 1.4k > Reed-Solomon Erasure Coding in Go [segmentio / **asm**](https://github.com/segmentio/asm) diff --git a/internal/cmd/docgen/main.go b/internal/cmd/docgen/main.go index c93ddbe..0fe1aa2 100644 --- a/internal/cmd/docgen/main.go +++ b/internal/cmd/docgen/main.go @@ -10,6 +10,7 @@ import ( "fmt" "io/ioutil" "log" + "math" "net/url" "os" "regexp" @@ -45,6 +46,7 @@ func mainerr() (err error) { "include": include, "snippet": snippet, "avatar": avatar, + "stars": stars, }) // Load template. @@ -191,3 +193,12 @@ func avatar(owner string, size int) (string, error) { format := `` return fmt.Sprintf(format, src.String(), size, size), nil } + +// stars formats a Github star count, rounding to thousands in the same style as Github. +func stars(n int) string { + if n < 1000 { + return strconv.Itoa(n) + } + k := math.Round(float64(n)/100.0) / 10.0 + return strconv.FormatFloat(k, 'f', -1, 64) + "k" +} diff --git a/internal/cmd/docgen/templates/readme.tmpl b/internal/cmd/docgen/templates/readme.tmpl index b5918c9..66ea5e2 100644 --- a/internal/cmd/docgen/templates/readme.tmpl +++ b/internal/cmd/docgen/templates/readme.tmpl @@ -108,7 +108,7 @@ Popular projects using `avo`: {{ range .Projects.Top 10 -}} {{ if lt .Metadata.Stars 100 }}{{break}}{{ end -}} {{ avatar .Repository.Owner 24 }} [{{ .Repository.Owner }} / **{{ .Repository.Name }}**]({{ .Repository.URL }}) -:star: {{ .Metadata.Stars }} +:star: {{ stars .Metadata.Stars }} > {{ .Metadata.Description }} {{ end -}}