mirror of
https://github.com/therootcompany/golib.git
synced 2026-04-24 12:48:00 +00:00
style: format entry counts with comma thousands separators
3,406,727 scans cleanly; 3406727 does not. Go's fmt has no thousands-separator verb and golang.org/x/text/message pulls in a multi-MB Unicode tree for what is 15 lines inline, so each cmd gets its own commafy helper.
This commit is contained in:
parent
8ebc571928
commit
46b31b75c2
@ -13,6 +13,8 @@ import (
|
||||
"os"
|
||||
"os/signal"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
@ -29,6 +31,31 @@ const (
|
||||
version = "dev"
|
||||
)
|
||||
|
||||
// commafy formats n with comma thousands separators (e.g. 3406727 -> "3,406,727").
|
||||
func commafy(n int) string {
|
||||
s := strconv.Itoa(n)
|
||||
neg := ""
|
||||
if n < 0 {
|
||||
neg, s = "-", s[1:]
|
||||
}
|
||||
if len(s) <= 3 {
|
||||
return neg + s
|
||||
}
|
||||
var b strings.Builder
|
||||
head := len(s) % 3
|
||||
if head > 0 {
|
||||
b.WriteString(s[:head])
|
||||
b.WriteByte(',')
|
||||
}
|
||||
for i := head; i < len(s); i += 3 {
|
||||
b.WriteString(s[i : i+3])
|
||||
if i+3 < len(s) {
|
||||
b.WriteByte(',')
|
||||
}
|
||||
}
|
||||
return neg + b.String()
|
||||
}
|
||||
|
||||
// IPCheck holds the parsed CLI config and the loaded data sources used by
|
||||
// the HTTP handler.
|
||||
type IPCheck struct {
|
||||
@ -140,10 +167,10 @@ func main() {
|
||||
fmt.Fprintln(os.Stderr)
|
||||
log.Fatalf("blocklists: %v", err)
|
||||
}
|
||||
fmt.Fprintf(os.Stderr, "%s (inbound=%d, outbound=%d)\n",
|
||||
fmt.Fprintf(os.Stderr, "%s (inbound=%s, outbound=%s)\n",
|
||||
time.Since(t).Round(time.Millisecond),
|
||||
cfg.inbound.Value().Size(),
|
||||
cfg.outbound.Value().Size(),
|
||||
commafy(cfg.inbound.Value().Size()),
|
||||
commafy(cfg.outbound.Value().Size()),
|
||||
)
|
||||
|
||||
// GeoIP: download the City + ASN tar.gz archives via httpcache
|
||||
@ -201,9 +228,9 @@ func main() {
|
||||
fmt.Fprintln(os.Stderr)
|
||||
log.Fatalf("whitelist: %v", err)
|
||||
}
|
||||
fmt.Fprintf(os.Stderr, "%s (entries=%d)\n",
|
||||
fmt.Fprintf(os.Stderr, "%s (entries=%s)\n",
|
||||
time.Since(t).Round(time.Millisecond),
|
||||
cfg.whitelist.Value().Size(),
|
||||
commafy(cfg.whitelist.Value().Size()),
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
@ -16,6 +16,7 @@ import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
@ -24,6 +25,30 @@ import (
|
||||
|
||||
const version = "dev"
|
||||
|
||||
func commafy(n int) string {
|
||||
s := strconv.Itoa(n)
|
||||
neg := ""
|
||||
if n < 0 {
|
||||
neg, s = "-", s[1:]
|
||||
}
|
||||
if len(s) <= 3 {
|
||||
return neg + s
|
||||
}
|
||||
var b strings.Builder
|
||||
head := len(s) % 3
|
||||
if head > 0 {
|
||||
b.WriteString(s[:head])
|
||||
b.WriteByte(',')
|
||||
}
|
||||
for i := head; i < len(s); i += 3 {
|
||||
b.WriteString(s[i : i+3])
|
||||
if i+3 < len(s) {
|
||||
b.WriteByte(',')
|
||||
}
|
||||
}
|
||||
return neg + b.String()
|
||||
}
|
||||
|
||||
type Config struct {
|
||||
IP string
|
||||
}
|
||||
@ -95,9 +120,9 @@ func main() {
|
||||
fmt.Fprintf(os.Stderr, "error: %v\n", err)
|
||||
os.Exit(2)
|
||||
}
|
||||
fmt.Fprintf(os.Stderr, "%s (entries=%d)\n",
|
||||
fmt.Fprintf(os.Stderr, "%s (entries=%s)\n",
|
||||
time.Since(t).Round(time.Millisecond),
|
||||
cohort.Size(),
|
||||
commafy(cohort.Size()),
|
||||
)
|
||||
|
||||
if len(ips) == 0 {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user