Skip to content

Commit

Permalink
continue to improve
Browse files Browse the repository at this point in the history
  • Loading branch information
boyter committed Aug 1, 2023
1 parent c5ddd28 commit 3e49665
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 3 deletions.
9 changes: 7 additions & 2 deletions cmd/badges/main.go
Expand Up @@ -18,7 +18,7 @@ func main() {
//pageSize := 20
title := "Total lines"
text_length := "250"
s := "1.5k"
s := formatCount(30000)

w.Header().Set("Content-Type", "image/svg+xml;charset=utf-8")
w.Write([]byte(`<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="100" height="20"><linearGradient id="b" x2="0" y2="100%"><stop offset="0" stop-color="#bbb" stop-opacity=".1"/><stop offset="1" stop-opacity=".1"/></linearGradient><clipPath id="a"><rect width="100" height="20" rx="3" fill="#fff"/></clipPath><g clip-path="url(#a)"><path fill="#555" d="M0 0h69v20H0z"/><path fill="#4c1" d="M69 0h31v20H69z"/><path fill="url(#b)" d="M0 0h100v20H0z"/></g><g fill="#fff" text-anchor="middle" font-family="DejaVu Sans,Verdana,Geneva,sans-serif" font-size="110"> <text x="355" y="150" fill="#010101" fill-opacity=".3" transform="scale(.1)" textLength="590">` + title + `</text><text x="355" y="140" transform="scale(.1)" textLength="590">` + title + `</text><text x="835" y="150" fill="#010101" fill-opacity=".3" transform="scale(.1)" textLength="` + text_length + `">` + s + `</text><text x="835" y="140" transform="scale(.1)" textLength="` + text_length + `">` + s + `</text></g> </svg>`))
Expand Down Expand Up @@ -78,7 +78,12 @@ func formatCount(count float64) string {

for _, v := range ranges {
if count >= v.val {
t := math.Round(count / v.val)
t := fmt.Sprintf("%.1f", math.Round(count/v.val))

if len(t) > 3 {
t = t[:strings.Index(t, ".")]
}

return fmt.Sprintf("%v%v", t, v.sym)
}
}
Expand Down
37 changes: 36 additions & 1 deletion cmd/badges/main_test.go
@@ -1,6 +1,10 @@
package main

import "testing"
import (
"fmt"
"math"
"testing"
)

func Test_processPath(t *testing.T) {
type args struct {
Expand Down Expand Up @@ -28,6 +32,9 @@ func Test_processPath(t *testing.T) {
}

func Test_formatCount(t *testing.T) {

fmt.Println(math.Round(2500 / 1000))

type args struct {
count float64
}
Expand Down Expand Up @@ -57,6 +64,34 @@ func Test_formatCount(t *testing.T) {
},
want: "2.5k",
},
{
name: "",
args: args{
count: 436465,
},
want: "436k",
},
{
name: "",
args: args{
count: 263804,
},
want: "264k",
},
{
name: "",
args: args{
count: 86400,
},
want: "86k",
},
{
name: "",
args: args{
count: 81.99825581739397,
},
want: "82",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down

0 comments on commit 3e49665

Please sign in to comment.