diff --git a/color/color.go b/color/color.go index 4131dcf..ececc52 100644 --- a/color/color.go +++ b/color/color.go @@ -11,7 +11,7 @@ import ( ) type ( - inner func(interface{}, []string, *Color) string + inner func(any, []string, *Color) string ) // Color styles @@ -103,7 +103,7 @@ var ( ) func outer(n string) inner { - return func(msg interface{}, styles []string, c *Color) string { + return func(msg any, styles []string, c *Color) string { // TODO: Drop fmt to boost performance? if c.disabled { return fmt.Sprintf("%v", msg) @@ -159,117 +159,117 @@ func (c *Color) Enable() { } // Print is analogous to `fmt.Print` with termial detection. -func (c *Color) Print(args ...interface{}) { +func (c *Color) Print(args ...any) { fmt.Fprint(c.output, args...) } // Println is analogous to `fmt.Println` with termial detection. -func (c *Color) Println(args ...interface{}) { +func (c *Color) Println(args ...any) { fmt.Fprintln(c.output, args...) } // Printf is analogous to `fmt.Printf` with termial detection. -func (c *Color) Printf(format string, args ...interface{}) { +func (c *Color) Printf(format string, args ...any) { fmt.Fprintf(c.output, format, args...) } -func (c *Color) Black(msg interface{}, styles ...string) string { +func (c *Color) Black(msg any, styles ...string) string { return black(msg, styles, c) } -func (c *Color) Red(msg interface{}, styles ...string) string { +func (c *Color) Red(msg any, styles ...string) string { return red(msg, styles, c) } -func (c *Color) Green(msg interface{}, styles ...string) string { +func (c *Color) Green(msg any, styles ...string) string { return green(msg, styles, c) } -func (c *Color) Yellow(msg interface{}, styles ...string) string { +func (c *Color) Yellow(msg any, styles ...string) string { return yellow(msg, styles, c) } -func (c *Color) Blue(msg interface{}, styles ...string) string { +func (c *Color) Blue(msg any, styles ...string) string { return blue(msg, styles, c) } -func (c *Color) Magenta(msg interface{}, styles ...string) string { +func (c *Color) Magenta(msg any, styles ...string) string { return magenta(msg, styles, c) } -func (c *Color) Cyan(msg interface{}, styles ...string) string { +func (c *Color) Cyan(msg any, styles ...string) string { return cyan(msg, styles, c) } -func (c *Color) White(msg interface{}, styles ...string) string { +func (c *Color) White(msg any, styles ...string) string { return white(msg, styles, c) } -func (c *Color) Grey(msg interface{}, styles ...string) string { +func (c *Color) Grey(msg any, styles ...string) string { return grey(msg, styles, c) } -func (c *Color) BlackBg(msg interface{}, styles ...string) string { +func (c *Color) BlackBg(msg any, styles ...string) string { return blackBg(msg, styles, c) } -func (c *Color) RedBg(msg interface{}, styles ...string) string { +func (c *Color) RedBg(msg any, styles ...string) string { return redBg(msg, styles, c) } -func (c *Color) GreenBg(msg interface{}, styles ...string) string { +func (c *Color) GreenBg(msg any, styles ...string) string { return greenBg(msg, styles, c) } -func (c *Color) YellowBg(msg interface{}, styles ...string) string { +func (c *Color) YellowBg(msg any, styles ...string) string { return yellowBg(msg, styles, c) } -func (c *Color) BlueBg(msg interface{}, styles ...string) string { +func (c *Color) BlueBg(msg any, styles ...string) string { return blueBg(msg, styles, c) } -func (c *Color) MagentaBg(msg interface{}, styles ...string) string { +func (c *Color) MagentaBg(msg any, styles ...string) string { return magentaBg(msg, styles, c) } -func (c *Color) CyanBg(msg interface{}, styles ...string) string { +func (c *Color) CyanBg(msg any, styles ...string) string { return cyanBg(msg, styles, c) } -func (c *Color) WhiteBg(msg interface{}, styles ...string) string { +func (c *Color) WhiteBg(msg any, styles ...string) string { return whiteBg(msg, styles, c) } -func (c *Color) Reset(msg interface{}, styles ...string) string { +func (c *Color) Reset(msg any, styles ...string) string { return reset(msg, styles, c) } -func (c *Color) Bold(msg interface{}, styles ...string) string { +func (c *Color) Bold(msg any, styles ...string) string { return bold(msg, styles, c) } -func (c *Color) Dim(msg interface{}, styles ...string) string { +func (c *Color) Dim(msg any, styles ...string) string { return dim(msg, styles, c) } -func (c *Color) Italic(msg interface{}, styles ...string) string { +func (c *Color) Italic(msg any, styles ...string) string { return italic(msg, styles, c) } -func (c *Color) Underline(msg interface{}, styles ...string) string { +func (c *Color) Underline(msg any, styles ...string) string { return underline(msg, styles, c) } -func (c *Color) Inverse(msg interface{}, styles ...string) string { +func (c *Color) Inverse(msg any, styles ...string) string { return inverse(msg, styles, c) } -func (c *Color) Hidden(msg interface{}, styles ...string) string { +func (c *Color) Hidden(msg any, styles ...string) string { return hidden(msg, styles, c) } -func (c *Color) Strikeout(msg interface{}, styles ...string) string { +func (c *Color) Strikeout(msg any, styles ...string) string { return strikeout(msg, styles, c) } @@ -292,116 +292,116 @@ func Enable() { } // Print is analogous to `fmt.Print` with termial detection. -func Print(args ...interface{}) { +func Print(args ...any) { global.Print(args...) } // Println is analogous to `fmt.Println` with termial detection. -func Println(args ...interface{}) { +func Println(args ...any) { global.Println(args...) } // Printf is analogous to `fmt.Printf` with termial detection. -func Printf(format string, args ...interface{}) { +func Printf(format string, args ...any) { global.Printf(format, args...) } -func Black(msg interface{}, styles ...string) string { +func Black(msg any, styles ...string) string { return global.Black(msg, styles...) } -func Red(msg interface{}, styles ...string) string { +func Red(msg any, styles ...string) string { return global.Red(msg, styles...) } -func Green(msg interface{}, styles ...string) string { +func Green(msg any, styles ...string) string { return global.Green(msg, styles...) } -func Yellow(msg interface{}, styles ...string) string { +func Yellow(msg any, styles ...string) string { return global.Yellow(msg, styles...) } -func Blue(msg interface{}, styles ...string) string { +func Blue(msg any, styles ...string) string { return global.Blue(msg, styles...) } -func Magenta(msg interface{}, styles ...string) string { +func Magenta(msg any, styles ...string) string { return global.Magenta(msg, styles...) } -func Cyan(msg interface{}, styles ...string) string { +func Cyan(msg any, styles ...string) string { return global.Cyan(msg, styles...) } -func White(msg interface{}, styles ...string) string { +func White(msg any, styles ...string) string { return global.White(msg, styles...) } -func Grey(msg interface{}, styles ...string) string { +func Grey(msg any, styles ...string) string { return global.Grey(msg, styles...) } -func BlackBg(msg interface{}, styles ...string) string { +func BlackBg(msg any, styles ...string) string { return global.BlackBg(msg, styles...) } -func RedBg(msg interface{}, styles ...string) string { +func RedBg(msg any, styles ...string) string { return global.RedBg(msg, styles...) } -func GreenBg(msg interface{}, styles ...string) string { +func GreenBg(msg any, styles ...string) string { return global.GreenBg(msg, styles...) } -func YellowBg(msg interface{}, styles ...string) string { +func YellowBg(msg any, styles ...string) string { return global.YellowBg(msg, styles...) } -func BlueBg(msg interface{}, styles ...string) string { +func BlueBg(msg any, styles ...string) string { return global.BlueBg(msg, styles...) } -func MagentaBg(msg interface{}, styles ...string) string { +func MagentaBg(msg any, styles ...string) string { return global.MagentaBg(msg, styles...) } -func CyanBg(msg interface{}, styles ...string) string { +func CyanBg(msg any, styles ...string) string { return global.CyanBg(msg, styles...) } -func WhiteBg(msg interface{}, styles ...string) string { +func WhiteBg(msg any, styles ...string) string { return global.WhiteBg(msg, styles...) } -func Reset(msg interface{}, styles ...string) string { +func Reset(msg any, styles ...string) string { return global.Reset(msg, styles...) } -func Bold(msg interface{}, styles ...string) string { +func Bold(msg any, styles ...string) string { return global.Bold(msg, styles...) } -func Dim(msg interface{}, styles ...string) string { +func Dim(msg any, styles ...string) string { return global.Dim(msg, styles...) } -func Italic(msg interface{}, styles ...string) string { +func Italic(msg any, styles ...string) string { return global.Italic(msg, styles...) } -func Underline(msg interface{}, styles ...string) string { +func Underline(msg any, styles ...string) string { return global.Underline(msg, styles...) } -func Inverse(msg interface{}, styles ...string) string { +func Inverse(msg any, styles ...string) string { return global.Inverse(msg, styles...) } -func Hidden(msg interface{}, styles ...string) string { +func Hidden(msg any, styles ...string) string { return global.Hidden(msg, styles...) } -func Strikeout(msg interface{}, styles ...string) string { +func Strikeout(msg any, styles ...string) string { return global.Strikeout(msg, styles...) } diff --git a/log/log.go b/log/log.go index a223351..66b2785 100644 --- a/log/log.go +++ b/log/log.go @@ -34,7 +34,7 @@ type ( Lvl uint8 - JSON map[string]interface{} + JSON map[string]any ) const ( @@ -65,7 +65,7 @@ func New(prefix string) (l *Logger) { template: l.newTemplate(defaultHeader), color: color.New(), bufferPool: sync.Pool{ - New: func() interface{} { + New: func() any { return bytes.NewBuffer(make([]byte, 256)) }, }, @@ -137,11 +137,11 @@ func (l *Logger) SetHeader(h string) { l.template = l.newTemplate(h) } -func (l *Logger) Print(i ...interface{}) { +func (l *Logger) Print(i ...any) { l.log(0, fmt.Sprint(i...), false) } -func (l *Logger) Printf(format string, args ...interface{}) { +func (l *Logger) Printf(format string, args ...any) { l.log(0, fmt.Sprintf(format, args...), false) } @@ -149,11 +149,11 @@ func (l *Logger) Printj(j JSON) { l.logJSON(0, j) } -func (l *Logger) Debug(i ...interface{}) { +func (l *Logger) Debug(i ...any) { l.log(DEBUG, fmt.Sprint(i...), false) } -func (l *Logger) Debugf(format string, args ...interface{}) { +func (l *Logger) Debugf(format string, args ...any) { l.log(DEBUG, fmt.Sprintf(format, args...), false) } @@ -161,11 +161,11 @@ func (l *Logger) Debugj(j JSON) { l.logJSON(DEBUG, j) } -func (l *Logger) Info(i ...interface{}) { +func (l *Logger) Info(i ...any) { l.log(INFO, fmt.Sprint(i...), false) } -func (l *Logger) Infof(format string, args ...interface{}) { +func (l *Logger) Infof(format string, args ...any) { l.log(INFO, fmt.Sprintf(format, args...), false) } @@ -173,11 +173,11 @@ func (l *Logger) Infoj(j JSON) { l.logJSON(INFO, j) } -func (l *Logger) Warn(i ...interface{}) { +func (l *Logger) Warn(i ...any) { l.log(WARN, fmt.Sprint(i...), false) } -func (l *Logger) Warnf(format string, args ...interface{}) { +func (l *Logger) Warnf(format string, args ...any) { l.log(WARN, fmt.Sprintf(format, args...), false) } @@ -185,11 +185,11 @@ func (l *Logger) Warnj(j JSON) { l.logJSON(WARN, j) } -func (l *Logger) Error(i ...interface{}) { +func (l *Logger) Error(i ...any) { l.log(ERROR, fmt.Sprint(i...), false) } -func (l *Logger) Errorf(format string, args ...interface{}) { +func (l *Logger) Errorf(format string, args ...any) { l.log(ERROR, fmt.Sprintf(format, args...), false) } @@ -197,12 +197,12 @@ func (l *Logger) Errorj(j JSON) { l.logJSON(ERROR, j) } -func (l *Logger) Fatal(i ...interface{}) { +func (l *Logger) Fatal(i ...any) { l.log(fatalLevel, fmt.Sprint(i...), false) os.Exit(1) } -func (l *Logger) Fatalf(format string, args ...interface{}) { +func (l *Logger) Fatalf(format string, args ...any) { l.log(fatalLevel, fmt.Sprintf(format, args...), false) os.Exit(1) } @@ -212,13 +212,13 @@ func (l *Logger) Fatalj(j JSON) { os.Exit(1) } -func (l *Logger) Panic(i ...interface{}) { +func (l *Logger) Panic(i ...any) { msg := fmt.Sprint(i...) l.log(panicLevel, msg, false) panic(msg) } -func (l *Logger) Panicf(format string, args ...interface{}) { +func (l *Logger) Panicf(format string, args ...any) { msg := fmt.Sprintf(format, args...) l.log(panicLevel, msg, false) panic(msg) @@ -265,11 +265,11 @@ func SetHeader(h string) { global.SetHeader(h) } -func Print(i ...interface{}) { +func Print(i ...any) { global.Print(i...) } -func Printf(format string, args ...interface{}) { +func Printf(format string, args ...any) { global.Printf(format, args...) } @@ -277,11 +277,11 @@ func Printj(j JSON) { global.Printj(j) } -func Debug(i ...interface{}) { +func Debug(i ...any) { global.Debug(i...) } -func Debugf(format string, args ...interface{}) { +func Debugf(format string, args ...any) { global.Debugf(format, args...) } @@ -289,11 +289,11 @@ func Debugj(j JSON) { global.Debugj(j) } -func Info(i ...interface{}) { +func Info(i ...any) { global.Info(i...) } -func Infof(format string, args ...interface{}) { +func Infof(format string, args ...any) { global.Infof(format, args...) } @@ -301,11 +301,11 @@ func Infoj(j JSON) { global.Infoj(j) } -func Warn(i ...interface{}) { +func Warn(i ...any) { global.Warn(i...) } -func Warnf(format string, args ...interface{}) { +func Warnf(format string, args ...any) { global.Warnf(format, args...) } @@ -313,11 +313,11 @@ func Warnj(j JSON) { global.Warnj(j) } -func Error(i ...interface{}) { +func Error(i ...any) { global.Error(i...) } -func Errorf(format string, args ...interface{}) { +func Errorf(format string, args ...any) { global.Errorf(format, args...) } @@ -325,11 +325,11 @@ func Errorj(j JSON) { global.Errorj(j) } -func Fatal(i ...interface{}) { +func Fatal(i ...any) { global.Fatal(i...) } -func Fatalf(format string, args ...interface{}) { +func Fatalf(format string, args ...any) { global.Fatalf(format, args...) } @@ -337,11 +337,11 @@ func Fatalj(j JSON) { global.Fatalj(j) } -func Panic(i ...interface{}) { +func Panic(i ...any) { global.Panic(i...) } -func Panicf(format string, args ...interface{}) { +func Panicf(format string, args ...any) { global.Panicf(format, args...) } diff --git a/random/random.go b/random/random.go index 282241b..1f0592c 100644 --- a/random/random.go +++ b/random/random.go @@ -1,91 +1 @@ -package random - -import ( - "bufio" - "crypto/rand" - "io" - "strings" - "sync" -) - -type ( - Random struct { - readerPool sync.Pool - } -) - -// Charsets -const ( - Uppercase = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" - Lowercase = "abcdefghijklmnopqrstuvwxyz" - Alphabetic = Uppercase + Lowercase - Numeric = "0123456789" - Alphanumeric = Alphabetic + Numeric - Symbols = "`" + `~!@#$%^&*()-_+={}[]|\;:"<>,./?` - Hex = Numeric + "abcdef" -) - -var ( - global = New() -) - -func New() *Random { - // https://tip.golang.org/doc/go1.19#:~:text=Read%20no%20longer%20buffers%20random%20data%20obtained%20from%20the%20operating%20system%20between%20calls - // sync.Pool must not be copied after first use; construct it directly - // on the struct to avoid copying from a local var. - return &Random{ - readerPool: sync.Pool{ - New: func() interface{} { - return bufio.NewReader(rand.Reader) - }, - }, - } -} - -func (r *Random) String(length uint8, charsets ...string) string { - charset := strings.Join(charsets, "") - if charset == "" { - charset = Alphanumeric - } - - charsetLen := len(charset) - if charsetLen > 255 { - charsetLen = 255 - } - maxByte := 255 - (256 % charsetLen) - - reader := r.readerPool.Get().(*bufio.Reader) - defer r.readerPool.Put(reader) - - b := make([]byte, length) - rs := make([]byte, length+(length/4)) // perf: avoid read from rand.Reader many times - var i uint8 = 0 - - // security note: - // we can't just simply do b[i]=charset[rb%byte(charsetLen)], - // for example, when charsetLen is 52, and rb is [0, 255], 256 = 52 * 4 + 48. - // this will make the first 48 characters more possibly to be generated then others. - // so we have to skip bytes when rb > maxByte - - for { - _, err := io.ReadFull(reader, rs) - if err != nil { - panic("unexpected error happened when reading from bufio.NewReader(crypto/rand.Reader)") - } - for _, rb := range rs { - if rb > byte(maxByte) { - // Skip this number to avoid bias. - continue - } - b[i] = charset[rb%byte(charsetLen)] - i++ - if i == length { - return string(b) - } - } - } -} - -func String(length uint8, charsets ...string) string { - return global.String(length, charsets...) -} +4207