copy langext & termext from ffsclient
This commit is contained in:
parent
0eaeb5ac4f
commit
3717eeb515
@ -1,7 +1,7 @@
|
||||
package dataext
|
||||
|
||||
import (
|
||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||
"gogs.mikescher.com/BlackForestBytes/goext/langext"
|
||||
"math/rand"
|
||||
"strconv"
|
||||
"testing"
|
||||
@ -251,18 +251,21 @@ func randomKey() string {
|
||||
}
|
||||
|
||||
func randomVal() LRUData {
|
||||
v := primitive.NewObjectID()
|
||||
v, err := langext.NewHexUUID()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return &v
|
||||
}
|
||||
|
||||
func eq(a LRUData, b LRUData) bool {
|
||||
v1, ok1 := a.(*primitive.ObjectID)
|
||||
v2, ok2 := b.(*primitive.ObjectID)
|
||||
v1, ok1 := a.(*string)
|
||||
v2, ok2 := b.(*string)
|
||||
if ok1 && ok2 {
|
||||
if v1 == nil || v2 == nil {
|
||||
return false
|
||||
}
|
||||
return v1.Hex() == v2.Hex()
|
||||
return v1 == v2
|
||||
}
|
||||
|
||||
return false
|
||||
|
6
go.mod
6
go.mod
@ -1,3 +1,9 @@
|
||||
module gogs.mikescher.com/BlackForestBytes/goext
|
||||
|
||||
go 1.19
|
||||
|
||||
require (
|
||||
golang.org/x/term v0.1.0
|
||||
)
|
||||
|
||||
require golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1 // indirect
|
||||
|
43
go.sum
Normal file
43
go.sum
Normal file
@ -0,0 +1,43 @@
|
||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/golang/snappy v0.0.1/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
|
||||
github.com/google/go-cmp v0.5.2 h1:X2ev0eStA3AbceY54o37/0PQ/UWqKEiiO2dKL5OPaFM=
|
||||
github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
|
||||
github.com/klauspost/compress v1.13.6/go.mod h1:/3/Vjq9QcHkK5uEr5lBEmyoZ1iFhe47etQ6QUkpK6sk=
|
||||
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
|
||||
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
|
||||
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
|
||||
github.com/montanaflynn/stats v0.0.0-20171201202039-1bf9dbcd8cbe/go.mod h1:wL8QJuTMNUDYhXwkmfOly8iTdp5TEcJFWZD2D7SIkUc=
|
||||
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||
github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0=
|
||||
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||
github.com/tidwall/pretty v1.0.0/go.mod h1:XNkn88O1ChpSDQmQeStsy+sBenx6DDtFZJxhVysOjyk=
|
||||
github.com/xdg-go/pbkdf2 v1.0.0/go.mod h1:jrpuAogTd400dnrH08LKmI/xc1MbPOebTwRqcT5RDeI=
|
||||
github.com/xdg-go/scram v1.1.1/go.mod h1:RaEWvsqvNKKvBPvcKeFjrG2cJqOkHTiyTpzz23ni57g=
|
||||
github.com/xdg-go/stringprep v1.0.3/go.mod h1:W3f5j4i+9rC0kuIEJL0ky1VpHXQU3ocBgklLGvcBnW8=
|
||||
github.com/youmark/pkcs8 v0.0.0-20181117223130-1be2e3e5546d/go.mod h1:rHwXgn7JulP+udvsHwJoVG1YGAP6VLg4y9I5dyZdqmA=
|
||||
go.mongodb.org/mongo-driver v1.10.3 h1:XDQEvmh6z1EUsXuIkXE9TaVeqHw6SwS1uf93jFs0HBA=
|
||||
go.mongodb.org/mongo-driver v1.10.3/go.mod h1:z4XpeoU6w+9Vht+jAFyLgVrD+jGSQQe0+CBWFHNiHt8=
|
||||
golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
|
||||
golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
|
||||
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1 h1:SrN+KX8Art/Sf4HNj6Zcz06G7VEz+7w9tdXTPOZ7+l4=
|
||||
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
|
||||
golang.org/x/term v0.1.0 h1:g6Z6vPFA9dYBAF7DWcH6sCcOntplXsDKcliusYijMlw=
|
||||
golang.org/x/term v0.1.0/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
|
||||
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
||||
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
|
||||
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
||||
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
9
langext/bool.go
Normal file
9
langext/bool.go
Normal file
@ -0,0 +1,9 @@
|
||||
package langext
|
||||
|
||||
func FormatBool(v bool, strTrue string, strFalse string) string {
|
||||
if v {
|
||||
return strTrue
|
||||
} else {
|
||||
return strFalse
|
||||
}
|
||||
}
|
@ -1,6 +1,9 @@
|
||||
package langext
|
||||
|
||||
import "fmt"
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
func FormatBytesToSI(b uint64) string {
|
||||
const unit = 1000
|
||||
@ -14,3 +17,30 @@ func FormatBytesToSI(b uint64) string {
|
||||
}
|
||||
return fmt.Sprintf("%.1f %cB", float64(b)/float64(div), "kMGTPE"[exp])
|
||||
}
|
||||
|
||||
func BytesXOR(a []byte, b []byte) ([]byte, error) {
|
||||
if len(a) != len(b) {
|
||||
return nil, errors.New("length mismatch")
|
||||
}
|
||||
|
||||
r := make([]byte, len(a))
|
||||
|
||||
for i := 0; i < len(a); i++ {
|
||||
r[i] = a[i] ^ b[i]
|
||||
}
|
||||
|
||||
return r, nil
|
||||
}
|
||||
|
||||
func FormatBytes(b int64) string {
|
||||
const unit = 1024
|
||||
if b < unit {
|
||||
return fmt.Sprintf("%d B", b)
|
||||
}
|
||||
div, exp := int64(unit), 0
|
||||
for n := b / unit; n >= unit; n /= unit {
|
||||
div *= unit
|
||||
exp++
|
||||
}
|
||||
return fmt.Sprintf("%.1f %ciB", float64(b)/float64(div), "KMGTPE"[exp])
|
||||
}
|
||||
|
5
langext/generics.go
Normal file
5
langext/generics.go
Normal file
@ -0,0 +1,5 @@
|
||||
package langext
|
||||
|
||||
type IntConstraint interface {
|
||||
int | int8 | int16 | int32 | int64 | uint | uint8 | uint16 | uint32 | uint64
|
||||
}
|
65
langext/json.go
Normal file
65
langext/json.go
Normal file
@ -0,0 +1,65 @@
|
||||
package langext
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
type H map[string]any
|
||||
|
||||
type A []any
|
||||
|
||||
func TryPrettyPrintJson(str string) string {
|
||||
var prettyJSON bytes.Buffer
|
||||
if err := json.Indent(&prettyJSON, []byte(str), "", " "); err != nil {
|
||||
return str
|
||||
}
|
||||
return prettyJSON.String()
|
||||
}
|
||||
|
||||
func PrettyPrintJson(str string) (string, bool) {
|
||||
var prettyJSON bytes.Buffer
|
||||
if err := json.Indent(&prettyJSON, []byte(str), "", " "); err != nil {
|
||||
return str, false
|
||||
}
|
||||
return prettyJSON.String(), true
|
||||
}
|
||||
|
||||
func PatchJson[JV string | []byte](rawjson JV, key string, value any) (JV, error) {
|
||||
var err error
|
||||
|
||||
var jsonpayload map[string]any
|
||||
err = json.Unmarshal([]byte(rawjson), &jsonpayload)
|
||||
if err != nil {
|
||||
return *new(JV), fmt.Errorf("failed to unmarshal payload: %w", err)
|
||||
}
|
||||
|
||||
jsonpayload[key] = value
|
||||
|
||||
newjson, err := json.Marshal(jsonpayload)
|
||||
if err != nil {
|
||||
return *new(JV), fmt.Errorf("failed to re-marshal payload: %w", err)
|
||||
}
|
||||
|
||||
return JV(newjson), nil
|
||||
}
|
||||
|
||||
func PatchRemJson[JV string | []byte](rawjson JV, key string) (JV, error) {
|
||||
var err error
|
||||
|
||||
var jsonpayload map[string]any
|
||||
err = json.Unmarshal([]byte(rawjson), &jsonpayload)
|
||||
if err != nil {
|
||||
return *new(JV), fmt.Errorf("failed to unmarshal payload: %w", err)
|
||||
}
|
||||
|
||||
delete(jsonpayload, key)
|
||||
|
||||
newjson, err := json.Marshal(jsonpayload)
|
||||
if err != nil {
|
||||
return *new(JV), fmt.Errorf("failed to re-marshal payload: %w", err)
|
||||
}
|
||||
|
||||
return JV(newjson), nil
|
||||
}
|
9
langext/maps.go
Normal file
9
langext/maps.go
Normal file
@ -0,0 +1,9 @@
|
||||
package langext
|
||||
|
||||
func MapKeyArr[T comparable, V any](v map[T]V) []T {
|
||||
result := make([]T, 0, len(v))
|
||||
for k := range v {
|
||||
result = append(result, k)
|
||||
}
|
||||
return result
|
||||
}
|
11
langext/os.go
Normal file
11
langext/os.go
Normal file
@ -0,0 +1,11 @@
|
||||
package langext
|
||||
|
||||
import "os"
|
||||
|
||||
func FileExists(filename string) bool {
|
||||
info, err := os.Stat(filename)
|
||||
if os.IsNotExist(err) {
|
||||
return false
|
||||
}
|
||||
return !info.IsDir()
|
||||
}
|
38
langext/rand.go
Normal file
38
langext/rand.go
Normal file
@ -0,0 +1,38 @@
|
||||
package langext
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"io"
|
||||
"math"
|
||||
"math/big"
|
||||
)
|
||||
|
||||
func RandBytes(size int) []byte {
|
||||
b := make([]byte, size)
|
||||
_, err := io.ReadFull(rand.Reader, b)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return b
|
||||
}
|
||||
|
||||
func RandBase62(rlen int) string {
|
||||
ecs := "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
|
||||
|
||||
bi52 := big.NewInt(int64(len(ecs)))
|
||||
|
||||
randMax := big.NewInt(math.MaxInt64)
|
||||
|
||||
r := ""
|
||||
|
||||
for i := 0; i < rlen; i++ {
|
||||
v, err := rand.Int(rand.Reader, randMax)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
r += string(ecs[v.Mod(v, bi52).Int64()])
|
||||
}
|
||||
|
||||
return r
|
||||
}
|
@ -61,3 +61,49 @@ func ConvertStringerArray[T fmt.Stringer](inarr []T) []string {
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func StrRunePadLeft(str string, pad string, padlen int) string {
|
||||
if pad == "" {
|
||||
pad = " "
|
||||
}
|
||||
|
||||
if len([]rune(str)) >= padlen {
|
||||
return str
|
||||
}
|
||||
|
||||
return strings.Repeat(pad, padlen-len([]rune(str)))[0:(padlen-len([]rune(str)))] + str
|
||||
}
|
||||
|
||||
func StrRunePadRight(str string, pad string, padlen int) string {
|
||||
if pad == "" {
|
||||
pad = " "
|
||||
}
|
||||
|
||||
if len([]rune(str)) >= padlen {
|
||||
return str
|
||||
}
|
||||
|
||||
return str + strings.Repeat(pad, padlen-len([]rune(str)))[0:(padlen-len([]rune(str)))]
|
||||
}
|
||||
|
||||
func Indent(str string, pad string) string {
|
||||
eonl := strings.HasSuffix(str, "\n")
|
||||
r := ""
|
||||
for _, v := range strings.Split(str, "\n") {
|
||||
r += pad + v + "\n"
|
||||
}
|
||||
|
||||
if eonl {
|
||||
r = r[0 : len(r)-1]
|
||||
}
|
||||
|
||||
return r
|
||||
}
|
||||
|
||||
func NumToStringOpt[V IntConstraint](v *V, fallback string) string {
|
||||
if v == nil {
|
||||
return fallback
|
||||
} else {
|
||||
return fmt.Sprintf("%d", v)
|
||||
}
|
||||
}
|
||||
|
134
langext/uuid.go
Normal file
134
langext/uuid.go
Normal file
@ -0,0 +1,134 @@
|
||||
package langext
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"encoding/hex"
|
||||
"io"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func NewUUID() ([16]byte, error) {
|
||||
var uuid [16]byte
|
||||
_, err := io.ReadFull(rand.Reader, uuid[:])
|
||||
if err != nil {
|
||||
return [16]byte{}, err
|
||||
}
|
||||
uuid[6] = (uuid[6] & 0x0f) | 0x40 // Version 4
|
||||
uuid[8] = (uuid[8] & 0x3f) | 0x80 // Variant is 10
|
||||
return uuid, nil
|
||||
}
|
||||
|
||||
func NewHexUUID() (string, error) {
|
||||
uuid, err := NewUUID()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
// Result: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
|
||||
|
||||
var dst = make([]byte, 36)
|
||||
|
||||
hex.Encode(dst, uuid[:4])
|
||||
dst[8] = '-'
|
||||
hex.Encode(dst[9:13], uuid[4:6])
|
||||
dst[13] = '-'
|
||||
hex.Encode(dst[14:18], uuid[6:8])
|
||||
dst[18] = '-'
|
||||
hex.Encode(dst[19:23], uuid[8:10])
|
||||
dst[23] = '-'
|
||||
hex.Encode(dst[24:], uuid[10:])
|
||||
|
||||
return string(dst), nil
|
||||
}
|
||||
|
||||
func NewUpperHexUUID() (string, error) {
|
||||
uuid, err := NewUUID()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
// Result: XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX
|
||||
|
||||
var dst = make([]byte, 36)
|
||||
|
||||
hex.Encode(dst, uuid[:4])
|
||||
dst[8] = '-'
|
||||
hex.Encode(dst[9:13], uuid[4:6])
|
||||
dst[13] = '-'
|
||||
hex.Encode(dst[14:18], uuid[6:8])
|
||||
dst[18] = '-'
|
||||
hex.Encode(dst[19:23], uuid[8:10])
|
||||
dst[23] = '-'
|
||||
hex.Encode(dst[24:], uuid[10:])
|
||||
|
||||
return strings.ToUpper(string(dst)), nil
|
||||
}
|
||||
|
||||
func NewRawHexUUID() (string, error) {
|
||||
uuid, err := NewUUID()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
// Result: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
|
||||
|
||||
var dst = make([]byte, 32)
|
||||
|
||||
hex.Encode(dst, uuid[:4])
|
||||
hex.Encode(dst[8:12], uuid[4:6])
|
||||
hex.Encode(dst[12:16], uuid[6:8])
|
||||
hex.Encode(dst[16:20], uuid[8:10])
|
||||
hex.Encode(dst[20:], uuid[10:])
|
||||
|
||||
return strings.ToUpper(string(dst)), nil
|
||||
}
|
||||
|
||||
func NewBracesUUID() (string, error) {
|
||||
uuid, err := NewUUID()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
// Result: {XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}
|
||||
|
||||
var dst = make([]byte, 38)
|
||||
|
||||
dst[0] = '{'
|
||||
hex.Encode(dst, uuid[:5])
|
||||
dst[9] = '-'
|
||||
hex.Encode(dst[10:14], uuid[4:6])
|
||||
dst[14] = '-'
|
||||
hex.Encode(dst[15:19], uuid[6:8])
|
||||
dst[19] = '-'
|
||||
hex.Encode(dst[20:24], uuid[8:10])
|
||||
dst[24] = '-'
|
||||
hex.Encode(dst[25:], uuid[10:])
|
||||
dst[37] = '}'
|
||||
|
||||
return strings.ToUpper(string(dst)), nil
|
||||
}
|
||||
|
||||
func NewParensUUID() (string, error) {
|
||||
uuid, err := NewUUID()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
// Result: (XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX)
|
||||
|
||||
var dst = make([]byte, 38)
|
||||
|
||||
dst[0] = '('
|
||||
hex.Encode(dst, uuid[:5])
|
||||
dst[9] = '-'
|
||||
hex.Encode(dst[10:14], uuid[4:6])
|
||||
dst[14] = '-'
|
||||
hex.Encode(dst[15:19], uuid[6:8])
|
||||
dst[19] = '-'
|
||||
hex.Encode(dst[20:24], uuid[8:10])
|
||||
dst[24] = '-'
|
||||
hex.Encode(dst[25:], uuid[10:])
|
||||
dst[37] = ')'
|
||||
|
||||
return strings.ToUpper(string(dst)), nil
|
||||
}
|
45
termext/colors.go
Normal file
45
termext/colors.go
Normal file
@ -0,0 +1,45 @@
|
||||
package termext
|
||||
|
||||
const (
|
||||
colorReset = "\033[0m"
|
||||
colorRed = "\033[31m"
|
||||
colorGreen = "\033[32m"
|
||||
colorYellow = "\033[33m"
|
||||
colorBlue = "\033[34m"
|
||||
colorPurple = "\033[35m"
|
||||
colorCyan = "\033[36m"
|
||||
colorGray = "\033[37m"
|
||||
colorWhite = "\033[97m"
|
||||
)
|
||||
|
||||
func Red(v string) string {
|
||||
return colorRed + v + colorReset
|
||||
}
|
||||
|
||||
func Green(v string) string {
|
||||
return colorGreen + v + colorReset
|
||||
}
|
||||
|
||||
func Yellow(v string) string {
|
||||
return colorYellow + v + colorReset
|
||||
}
|
||||
|
||||
func Blue(v string) string {
|
||||
return colorBlue + v + colorReset
|
||||
}
|
||||
|
||||
func Purple(v string) string {
|
||||
return colorPurple + v + colorReset
|
||||
}
|
||||
|
||||
func Cyan(v string) string {
|
||||
return colorCyan + v + colorReset
|
||||
}
|
||||
|
||||
func Gray(v string) string {
|
||||
return colorGray + v + colorReset
|
||||
}
|
||||
|
||||
func White(v string) string {
|
||||
return colorWhite + v + colorReset
|
||||
}
|
5
termext/osutil.go
Normal file
5
termext/osutil.go
Normal file
@ -0,0 +1,5 @@
|
||||
package termext
|
||||
|
||||
func enableColor() bool {
|
||||
return true
|
||||
}
|
24
termext/osutil_windows.go
Normal file
24
termext/osutil_windows.go
Normal file
@ -0,0 +1,24 @@
|
||||
package termext
|
||||
|
||||
func enableColor() bool {
|
||||
handle, err := windows.GetStdHandle(windows.STD_OUTPUT_HANDLE)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
|
||||
var mode uint32
|
||||
err = windows.GetConsoleMode(handle, &mode)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
|
||||
if mode&windows.ENABLE_VIRTUAL_TERMINAL_PROCESSING != windows.ENABLE_VIRTUAL_TERMINAL_PROCESSING {
|
||||
mode = mode | windows.ENABLE_VIRTUAL_TERMINAL_PROCESSING
|
||||
err = windows.SetConsoleMode(handle, mode)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
90
termext/termcolor.go
Normal file
90
termext/termcolor.go
Normal file
@ -0,0 +1,90 @@
|
||||
package termext
|
||||
|
||||
import (
|
||||
"golang.org/x/term"
|
||||
"os"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// -> partly copied from [ https://github.com/jwalton/go-supportscolor/tree/master ]
|
||||
|
||||
func SupportsColors() bool {
|
||||
|
||||
if isatty := term.IsTerminal(int(os.Stdout.Fd())); !isatty {
|
||||
return false
|
||||
}
|
||||
|
||||
termenv := os.Getenv("TERM")
|
||||
|
||||
if termenv == "dumb" {
|
||||
return false
|
||||
}
|
||||
|
||||
if osColorEnabled := enableColor(); !osColorEnabled {
|
||||
return false
|
||||
}
|
||||
|
||||
if _, ci := os.LookupEnv("CI"); ci {
|
||||
var ciEnvNames = []string{"TRAVIS", "CIRCLECI", "APPVEYOR", "GITLAB_CI", "GITHUB_ACTIONS", "BUILDKITE", "DRONE"}
|
||||
for _, ciEnvName := range ciEnvNames {
|
||||
_, exists := os.LookupEnv(ciEnvName)
|
||||
if exists {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
if os.Getenv("CI_NAME") == "codeship" {
|
||||
return true
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
if teamCityVersion, isTeamCity := os.LookupEnv("TEAMCITY_VERSION"); isTeamCity {
|
||||
versionRegex := regexp.MustCompile(`^(9\.(0*[1-9]\d*)\.|\d{2,}\.)`)
|
||||
if versionRegex.MatchString(teamCityVersion) {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
if os.Getenv("COLORTERM") == "truecolor" {
|
||||
return true
|
||||
}
|
||||
|
||||
if termProgram, termProgramPreset := os.LookupEnv("TERM_PROGRAM"); termProgramPreset {
|
||||
switch termProgram {
|
||||
case "iTerm.app":
|
||||
termProgramVersion := strings.Split(os.Getenv("TERM_PROGRAM_VERSION"), ".")
|
||||
version, err := strconv.ParseInt(termProgramVersion[0], 10, 64)
|
||||
if err == nil && version >= 3 {
|
||||
return true
|
||||
}
|
||||
return true
|
||||
case "Apple_Terminal":
|
||||
return true
|
||||
|
||||
default:
|
||||
// No default
|
||||
}
|
||||
}
|
||||
|
||||
var term256Regex = regexp.MustCompile("(?i)-256(color)?$")
|
||||
if term256Regex.MatchString(termenv) {
|
||||
return true
|
||||
}
|
||||
|
||||
var termBasicRegex = regexp.MustCompile("(?i)^screen|^xterm|^vt100|^vt220|^rxvt|color|ansi|cygwin|linux")
|
||||
|
||||
if termBasicRegex.MatchString(termenv) {
|
||||
return true
|
||||
}
|
||||
|
||||
if _, colorTerm := os.LookupEnv("COLORTERM"); colorTerm {
|
||||
return true
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
@ -1,6 +1,9 @@
|
||||
package timeext
|
||||
|
||||
import "time"
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
|
||||
func FromSeconds(v int) time.Duration {
|
||||
return time.Duration(int64(v) * int64(time.Second))
|
||||
@ -53,3 +56,36 @@ func FromMilliseconds(v int) time.Duration {
|
||||
func FromMillisecondsFloat(v float64) time.Duration {
|
||||
return time.Duration(int64(v * float64(time.Millisecond)))
|
||||
}
|
||||
|
||||
func FormatNaturalDurationEnglish(iv time.Duration) string {
|
||||
if sec := int64(iv.Seconds()); sec < 180 {
|
||||
if sec == 1 {
|
||||
return "1 second ago"
|
||||
} else {
|
||||
return fmt.Sprintf("%d seconds ago", sec)
|
||||
}
|
||||
}
|
||||
|
||||
if min := int64(iv.Minutes()); min < 180 {
|
||||
return fmt.Sprintf("%d minutes ago", min)
|
||||
}
|
||||
|
||||
if hours := int64(iv.Hours()); hours < 72 {
|
||||
return fmt.Sprintf("%d hours ago", hours)
|
||||
}
|
||||
|
||||
if days := int64(iv.Hours() / 24.0); days < 21 {
|
||||
return fmt.Sprintf("%d days ago", days)
|
||||
}
|
||||
|
||||
if weeks := int64(iv.Hours() / 24.0 / 7.0); weeks < 12 {
|
||||
return fmt.Sprintf("%d weeks ago", weeks)
|
||||
}
|
||||
|
||||
if months := int64(iv.Hours() / 24.0 / 7.0 / 30); months < 36 {
|
||||
return fmt.Sprintf("%d months ago", months)
|
||||
}
|
||||
|
||||
years := int64(iv.Hours() / 24.0 / 7.0 / 365)
|
||||
return fmt.Sprintf("%d years ago", years)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user