-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Zero
committed
May 30, 2023
1 parent
d9cfedb
commit d29651a
Showing
10 changed files
with
257 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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("哈哈哈") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.