color to output of registry command

This commit is contained in:
Lorenz Hohermuth 2024-05-16 21:58:07 +02:00
parent cca5052df8
commit 66c081b766
5 changed files with 57 additions and 6 deletions

View File

@ -4,12 +4,14 @@ Copyright © 2024 NAME HERE <EMAIL ADDRESS>
package cmd
import (
"fmt"
"os"
"strconv"
"text/tabwriter"
"unicode/utf8"
"github.com/LorenzHohermuth/origami/internal/registry"
"github.com/LorenzHohermuth/origami/internal/splitter"
"github.com/fatih/color"
"github.com/spf13/cobra"
)
@ -27,8 +29,17 @@ var registryCmd = &cobra.Command{
tw := new(tabwriter.Writer)
tw.Init(os.Stdout, 0, 8, 0, '\t', 0)
red := color.New(color.FgRed)
green := color.New(color.FgGreen)
formatterString := "value: \"%s\" key: %." + strconv.Itoa(tr.BitLength) + "b \n"
for v, k := range tr.Map {
fmt.Fprintf(tw, "value: \"%s\" \t key: %d \n", v, k)
if utf8.RuneCountInString(binary(v)) < tr.BitLength {
red.Fprintf(tw, formatterString, v, k)
} else {
green.Fprintf(tw, formatterString, v, k)
}
}
tw.Flush()
},

11
cmd/utils.go Normal file
View File

@ -0,0 +1,11 @@
package cmd
import "fmt"
func binary(s string) string {
res := ""
for _, c := range s {
res = fmt.Sprintf("%s%.8b", res, c)
}
return res
}

5
go.mod
View File

@ -5,6 +5,11 @@ go 1.22.2
require github.com/spf13/cobra v1.8.0
require (
github.com/fatih/color v1.17.0 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/wayneashleyberry/truecolor v1.0.1 // indirect
golang.org/x/sys v0.18.0 // indirect
)

13
go.sum
View File

@ -1,10 +1,23 @@
github.com/cpuguy83/go-md2man/v2 v2.0.3/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/fatih/color v1.17.0 h1:GlRw1BRJxkpqUCBKzKOw098ed57fEsKeNjpTe3cSjK4=
github.com/fatih/color v1.17.0/go.mod h1:YZ7TlrGPkiz6ku9fK3TLD/pl3CpsiFyu8N92HLgmosI=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/spf13/cobra v1.8.0 h1:7aJaZx1B85qltLMc546zn58BxxfZdR/W22ej9CFoEf0=
github.com/spf13/cobra v1.8.0/go.mod h1:WXLWApfZ71AjXPya3WOlMsY9yMs7YeiHhFVlvLyhcho=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/wayneashleyberry/truecolor v1.0.1 h1:REnJBycjnvg0AFErbLx2GCmLLar8brlqm62kOKnRsGs=
github.com/wayneashleyberry/truecolor v1.0.1/go.mod h1:fyL3jRES70g94n+Eu+XLhXYvcseza55ph8zlkmUKW7Q=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.18.0 h1:DBdB3niSjOA/O0blCZBqDefyWNYveAYMNF1Wum0DYQ4=
golang.org/x/sys v0.18.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

View File

@ -1,13 +1,24 @@
package registry
type TokenRegistry struct{
Map map[string]int64
import (
"strconv"
"unicode/utf8"
)
type TokenRegistry struct {
BitLength int
Map map[string]int64
}
func (tr TokenRegistry) DistributeTokens(list [][]string) {
func (tr *TokenRegistry) DistributeTokens(list [][]string) {
var highestNum int64
for _, file := range list {
for _, value := range file {
tr.Map[value] = int64(len(tr.Map))
num := int64(len(tr.Map))
highestNum = num
tr.Map[value] = num
}
}
binHightNum := strconv.FormatInt(highestNum, 2)
tr.BitLength = utf8.RuneCountInString(binHightNum)
}