Skip to content

Commit

Permalink
feature: add print、uuid、runtime
Browse files Browse the repository at this point in the history
  • Loading branch information
Zero committed May 30, 2023
1 parent d9cfedb commit d29651a
Show file tree
Hide file tree
Showing 10 changed files with 257 additions and 1 deletion.
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/zlx2019/toys
go 1.20

require (
github.com/bytedance/sonic v1.8.8
github.com/go-resty/resty/v2 v2.7.0
github.com/jinzhu/copier v0.3.5
github.com/json-iterator/go v1.1.12
Expand All @@ -13,10 +14,10 @@ require (
)

require (
github.com/bytedance/sonic v1.8.8 // indirect
github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/fsnotify/fsnotify v1.6.0 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/klauspost/cpuid/v2 v2.0.9 // indirect
github.com/magiconair/properties v1.8.7 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,8 @@ github.com/google/pprof v0.0.0-20201203190320-1bf35d6f28c2/go.mod h1:kpwsk12EmLe
github.com/google/pprof v0.0.0-20201218002935-b9804c9f04c2/go.mod h1:kpwsk12EmLew5upagYY7GY0pfYCcupk39gWOCRROcvE=
github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm40UhjYkI=
github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/googleapis/gax-go/v2 v2.0.4/go.mod h1:0Wqv26UfaUD9n4G6kQubkQ+KchISgw+vpHVxEJEs9eg=
github.com/googleapis/gax-go/v2 v2.0.5/go.mod h1:DWXyrwAJ9X0FpwwEdw+IPEYBICEFu5mhpdKc/us6bOk=
github.com/googleapis/google-cloud-go-testing v0.0.0-20200911160855-bcd43fbb19e8/go.mod h1:dvDLG8qkwmyD9a/MJJN3XJcT3xFxOKAvTZGvuZmac9g=
Expand Down
59 changes: 59 additions & 0 deletions prints/color.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package prints

// 终端文字 ASCII 控制符

// 文字显示样式标识
// Default 0 重置(终端默认样式)
// High 1 高亮
// Underline 4 下划线
// Flash 5 闪烁(不可用)
// Reverse 7 反白(将字体颜色作为背景颜色)
// Hide 8 隐藏
const (
Default = 0
High = 1
Underline = 4
Flash = 5
Reverse = 7
Hide = 8
)

// 终端前景颜色标识
// Black 30 黑色
// Red 31 红色
// Green 32 绿色
// Yellow 33 黄色
// Blue 34 蓝色
// Purple 35 紫色
// Cyan 36 青色
// White 37 白色
const (
Black = iota + 30
Red
Green
Yellow
Blue
Purple
Cyan
White
)

// 终端背景颜色标识
// BackgroundBlack 40 黑色背景
// BackgroundRed 41 红色背景
// BackgroundGreen 42 绿色背景
// BackgroundYellow 43 黄色背景
// BackgroundBlue 44 蓝色背景
// BackgroundPurple 45 紫色背景
// BackgroundCyan 46 青色背景
// BackgroundWhite 47 白色背景
const (
BackgroundBlack = iota + 40
BackgroundRed
BackgroundGreen
BackgroundYellow
BackgroundBlue
BackgroundPurple
BackgroundCyan
BackgroundWhite
)
98 changes: 98 additions & 0 deletions prints/print.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/**
@author: Zero
@date: 2023/5/28 08:58:25
@desc: 终端输出工具库
**/

package prints

import (
"fmt"
)

// DefaultTextStyle 默认的文本输出样式
var DefaultTextStyle = Default

// Println 输出无颜色文本
func Println(message string) {
PrintlnColor(message, 0)
}

// PrintlnBlue 输出蓝色文本
func PrintlnBlue(message string) {
PrintlnColor(message, Blue)
}

// PrintlnYellow 输出黄色文本
func PrintlnYellow(message string) {
PrintlnColor(message, Yellow)
}

// PrintlnGreen 输出绿色文本
func PrintlnGreen(message string) {
PrintlnColor(message, Green)
}

// PrintlnCyan 输出青色文本
func PrintlnCyan(message string) {
PrintlnColor(message, Cyan)
}

// PrintlnRed 输出红色文本
func PrintlnRed(message string) {
PrintlnColor(message, Red)
}

// PrintlnBlack 输出黑色文本
func PrintlnBlack(message string) {
PrintlnColor(message, Black)
}

// PrintlnPurple 输出紫色文本
func PrintlnPurple(message string) {
PrintlnColor(message, Purple)
}

// PrintlnWhite 输出白色文本
func PrintlnWhite(message string) {
PrintlnColor(message, White)
}

// PrintlnColor 输出带有颜色符号的消息
func PrintlnColor(message string, textColor int) {
fmt.Println(GetColor(message, textColor))
}

// SprintfColor 获取带有颜色符号的消息
func SprintfColor(message string, textColor int) string {
return GetColor(message, textColor)
}

// SetDefaultTextStyle 设置默认的字体显示样式
func SetDefaultTextStyle(textStyle int) {
DefaultTextStyle = textStyle
}

// GetColor 拼接输出内容和样式符号
func GetColor(message string, textColo int) string {
return fmt.Sprintf("%s%s", getASCIIColor(DefaultTextStyle, 0, textColo), message)
}

// getASCIIColorDefault 获取重置默认样式符号
func getASCIIColorDefault() string {
return fmt.Sprintf("%c[0m", 0x1B)
}

// getASCIIColor 根据指定的样式,获取ASCII文字符
// 格式: 0x1B[文字样式;背景颜色;前景颜色m
// 示例: 0x1B[1;40;31m
// 解释: 0x1B[高亮;黑色背景m
func getASCIIColor(textStyle, backgroundColor, textColo int) string {
if textStyle == 0 {
return fmt.Sprintf("%c[%d;%dm", 0x1B, backgroundColor, textColo)
} else if backgroundColor == 0 {
return fmt.Sprintf("%c[%d;%dm", 0x1B, textStyle, textColo)
} else {
return fmt.Sprintf("%c[%d;%d;%dm", 0x1B, textStyle, backgroundColor, textColo)
}
}
16 changes: 16 additions & 0 deletions prints/print_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package prints

import "testing"

func TestGetASCIIColor(t *testing.T) {
SetDefaultTextStyle(High)
Println("哈哈哈")
PrintlnRed("哈哈哈")
PrintlnBlack("哈哈哈")
PrintlnBlue("哈哈哈")
PrintlnCyan("哈哈哈")
PrintlnGreen("哈哈哈")
PrintlnPurple("哈哈哈")
PrintlnYellow("哈哈哈")
PrintlnWhite("哈哈哈")
}
6 changes: 6 additions & 0 deletions randoms/random.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ package randoms

import (
"bytes"
"github.com/google/uuid"
"math/rand"
"time"
)
Expand All @@ -26,6 +27,11 @@ func init() {
//rand.NewSource(time.Now().UnixNano())
}

// RandomUUID 生成一个uuid
func RandomUUID() string {
return uuid.New().String()
}

// RandomRune 从一个rune切片中,随机挑选n个rune,生成一个string
func RandomRune(length int, charset []rune) string {
if length <= 0 {
Expand Down
23 changes: 23 additions & 0 deletions randoms/random_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
@author: Zero
@date: 2023/5/29 21:58:30
@desc:
**/

package randoms

import (
"fmt"
"testing"
)

func TestRandomUUID(t *testing.T) {
fmt.Println(RandomUUID())
fmt.Println(RandomUUID())
fmt.Println(RandomUUID())
fmt.Println(RandomString(15))
fmt.Println(RandomString(15))
fmt.Println(RandomString(15))
fmt.Println(RandomString(15))
}
33 changes: 33 additions & 0 deletions system/runtime.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
@author: Zero
@date: 2023/5/30 11:48:48
@desc: Go程序相关函数库
**/

package system

import (
"os"
"runtime"
"strconv"
"strings"
)

// GetCurrentProcessID 获取当前Go程序的进程ID
func GetCurrentProcessID() string {
return strconv.Itoa(os.Getpid())
}

// GetCurrentGoroutineID 获取当前协程ID
func GetCurrentGoroutineID() string {
buf := make([]byte, 128)
// 读取当前栈信息
buf = buf[:runtime.Stack(buf, false)]
stackInfo := string(buf)
// 去除其他信息,获取 `goroutine 协程ID`
idStr := strings.Split(stackInfo, "[running]")[0]
// 再去除`goroutine`
id := strings.Split(idStr, "goroutine")[1]
return strings.TrimSpace(id)
}
18 changes: 18 additions & 0 deletions system/runtime_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/**
@author: Zero
@date: 2023/5/30 11:54:02
@desc:
**/

package system

import (
"fmt"
"testing"
)

func TestGetCurrentGoroutineID(t *testing.T) {
fmt.Println(GetCurrentProcessID())
fmt.Println(GetCurrentGoroutineID())
}
File renamed without changes.

0 comments on commit d29651a

Please sign in to comment.