修复 部分情况下无法显示彩色文字的问题

This commit is contained in:
xiu2
2025-07-22 19:51:05 +08:00
parent d76a8bc749
commit 4f9ca03008
6 changed files with 42 additions and 29 deletions

15
main.go
View File

@@ -109,7 +109,7 @@ https://github.com/XIU2/CloudflareSpeedTest
flag.Parse()
if task.MinSpeed > 0 && time.Duration(maxDelay)*time.Millisecond == utils.InputMaxDelay {
fmt.Println("\033[33m[提示] 在使用 [-sl] 参数时,建议搭配 [-tl] 参数,以避免因凑不够 [-dn] 数量而一直测速...\033[0m")
utils.Yellow.Println("[提示] 在使用 [-sl] 参数时,建议搭配 [-tl] 参数,以避免因凑不够 [-dn] 数量而一直测速...")
}
utils.InputMaxDelay = time.Duration(maxDelay) * time.Millisecond
utils.InputMinDelay = time.Duration(minDelay) * time.Millisecond
@@ -122,9 +122,9 @@ https://github.com/XIU2/CloudflareSpeedTest
fmt.Println("检查版本更新中...")
checkUpdate()
if versionNew != "" {
fmt.Printf("*** 发现新版本 [%s]!请前往 [https://github.com/XIU2/CloudflareSpeedTest] 更新! ***", versionNew)
utils.Yellow.Printf("*** 发现新版本 [%s]!请前往 [https://github.com/XIU2/CloudflareSpeedTest] 更新! ***", versionNew)
} else {
fmt.Println("当前为最新版本 [" + version + "]")
utils.Green.Println("当前为最新版本 [" + version + "]")
}
os.Exit(0)
}
@@ -141,15 +141,12 @@ func main() {
speedData := task.TestDownloadSpeed(pingData)
utils.ExportCsv(speedData) // 输出文件
speedData.Print() // 打印结果
if versionNew != "" {
fmt.Printf("\n*** 发现新版本 [%s]!请前往 [https://github.com/XIU2/CloudflareSpeedTest] 更新! ***\n", versionNew)
}
endPrint()
endPrint() // 根据情况选择退出方式(针对 Windows
}
// 根据情况选择退出方式(针对 Windows
func endPrint() {
if utils.NoPrintResult() {
if utils.NoPrintResult() { // 如果不需要打印测速结果,则直接退出
return
}
if runtime.GOOS == "windows" { // 如果是 Windows 系统,则需要按下 回车键 或 Ctrl+C 退出(避免通过双击运行时,测速完毕后直接关闭)

View File

@@ -54,7 +54,7 @@ func TestDownloadSpeed(ipSet utils.PingDelaySet) (speedSet utils.DownloadSpeedSe
return utils.DownloadSpeedSet(ipSet)
}
if len(ipSet) <= 0 { // IP 数组长度(IP数量) 大于 0 时才会继续下载测速
fmt.Println("\n\033[33m[信息] 延迟测速结果 IP 数量为 0跳过下载测速。\033[0m")
utils.Yellow.Println("[信息] 延迟测速结果 IP 数量为 0跳过下载测速。")
return
}
testNum := TestCount // 等待下载测速的队列数量 先默认等于 下载测速数量(-dn
@@ -65,7 +65,7 @@ func TestDownloadSpeed(ipSet utils.PingDelaySet) (speedSet utils.DownloadSpeedSe
TestCount = testNum
}
fmt.Printf("\033[34m开始下载测速(下限:%.2f MB/s, 数量:%d, 队列:%d\033[0m\n", MinSpeed, TestCount, testNum)
utils.Cyan.Printf("开始下载测速(下限:%.2f MB/s, 数量:%d, 队列:%d\n", MinSpeed, TestCount, testNum)
// 控制 下载测速进度条 与 延迟测速进度条 长度一致(强迫症)
bar_a := len(strconv.Itoa(len(ipSet)))
bar_b := " "
@@ -92,7 +92,7 @@ func TestDownloadSpeed(ipSet utils.PingDelaySet) (speedSet utils.DownloadSpeedSe
if MinSpeed == 0.00 { // 如果没有指定下载速度下限,则直接返回所有测速数据
speedSet = utils.DownloadSpeedSet(ipSet)
} else if utils.Debug && len(speedSet) == 0 { // 如果指定了下载速度下限,且是调试模式下,且没有找到任何一个满足条件的 IP 时,返回所有测速数据,供用户查看当前的测速结果,以便适当调低预期测速条件
fmt.Println("\033[33m[调试] 没有满足 下载速度下限 条件的 IP忽略条件返回所有测速数据方便下次测速时调整条件\033[0m")
utils.Yellow.Println("[调试] 没有满足 下载速度下限 条件的 IP忽略条件返回所有测速数据方便下次测速时调整条件。")
speedSet = utils.DownloadSpeedSet(ipSet)
}
// 按速度排序
@@ -122,15 +122,15 @@ func printDownloadDebugInfo(ip *net.IPAddr, err error, statusCode int, url, last
}
if url != finalURL { // 如果 URL 和最终地址不一致,说明有重定向,是该重定向后的地址引起的错误
if statusCode > 0 { // 如果状态码大于 0说明是后续 HTTP 状态码引起的错误
fmt.Printf("\033[31m[调试] IP: %s, 下载测速终止HTTP 状态码: %d, 下载测速地址: %s, 出错的重定向后地址: %s\033[0m\n", ip.String(), statusCode, url, finalURL)
utils.Red.Printf("[调试] IP: %s, 下载测速终止HTTP 状态码: %d, 下载测速地址: %s, 出错的重定向后地址: %s\n", ip.String(), statusCode, url, finalURL)
} else {
fmt.Printf("\033[31m[调试] IP: %s, 下载测速失败,错误信息: %v, 下载测速地址: %s, 出错的重定向后地址: %s\033[0m\n", ip.String(), err, url, finalURL)
utils.Red.Printf("[调试] IP: %s, 下载测速失败,错误信息: %v, 下载测速地址: %s, 出错的重定向后地址: %s\n", ip.String(), err, url, finalURL)
}
} else { // 如果 URL 和最终地址一致,说明没有重定向
if statusCode > 0 { // 如果状态码大于 0说明是后续 HTTP 状态码引起的错误
fmt.Printf("\033[31m[调试] IP: %s, 下载测速终止HTTP 状态码: %d, 下载测速地址: %s\033[0m\n", ip.String(), statusCode, url)
utils.Red.Printf("[调试] IP: %s, 下载测速终止HTTP 状态码: %d, 下载测速地址: %s\n", ip.String(), statusCode, url)
} else {
fmt.Printf("\033[31m[调试] IP: %s, 下载测速失败,错误信息: %v, 下载测速地址: %s\033[0m\n", ip.String(), err, url)
utils.Red.Printf("[调试] IP: %s, 下载测速失败,错误信息: %v, 下载测速地址: %s\n", ip.String(), err, url)
}
}
}
@@ -145,7 +145,7 @@ func downloadHandler(ip *net.IPAddr) (float64, string) {
lastRedirectURL = req.URL.String() // 记录每次重定向的目标,以便在访问错误时输出
if len(via) > 10 { // 限制最多重定向 10 次
if utils.Debug { // 调试模式下,输出更多信息
fmt.Printf("\033[31m[调试] IP: %s, 下载测速地址重定向次数过多,终止测速,下载测速地址: %s\033[0m\n", ip.String(), req.URL.String())
utils.Red.Printf("[调试] IP: %s, 下载测速地址重定向次数过多,终止测速,下载测速地址: %s\n", ip.String(), req.URL.String())
}
return http.ErrUseLastResponse
}
@@ -158,7 +158,7 @@ func downloadHandler(ip *net.IPAddr) (float64, string) {
req, err := http.NewRequest("GET", URL, nil)
if err != nil {
if utils.Debug { // 调试模式下,输出更多信息
fmt.Printf("\033[31m[调试] IP: %s, 下载测速请求创建失败,错误信息: %v, 下载测速地址: %s\033[0m\n", ip.String(), err, URL)
utils.Red.Printf("[调试] IP: %s, 下载测速请求创建失败,错误信息: %v, 下载测速地址: %s\n", ip.String(), err, URL)
}
return 0.0, ""
}

View File

@@ -2,7 +2,7 @@ package task
import (
//"crypto/tls"
"fmt"
"io"
"log"
"net"
@@ -44,7 +44,7 @@ func (p *Ping) httping(ip *net.IPAddr) (int, time.Duration, string) {
request, err := http.NewRequest(http.MethodHead, URL, nil)
if err != nil {
if utils.Debug { // 调试模式下,输出更多信息
fmt.Printf("\033[31m[调试] IP: %s, 延迟测速请求创建失败,错误信息: %v, 测速地址: %s\033[0m\n", ip.String(), err, URL)
utils.Red.Printf("[调试] IP: %s, 延迟测速请求创建失败,错误信息: %v, 测速地址: %s\n", ip.String(), err, URL)
}
return 0, 0, ""
}
@@ -52,7 +52,7 @@ func (p *Ping) httping(ip *net.IPAddr) (int, time.Duration, string) {
response, err := hc.Do(request)
if err != nil {
if utils.Debug { // 调试模式下,输出更多信息
fmt.Printf("\033[31m[调试] IP: %s, 延迟测速失败,错误信息: %v, 测速地址: %s\033[0m\n", ip.String(), err, URL)
utils.Red.Printf("[调试] IP: %s, 延迟测速失败,错误信息: %v, 测速地址: %s\n", ip.String(), err, URL)
}
return 0, 0, ""
}
@@ -63,14 +63,14 @@ func (p *Ping) httping(ip *net.IPAddr) (int, time.Duration, string) {
if HttpingStatusCode == 0 || HttpingStatusCode < 100 && HttpingStatusCode > 599 {
if response.StatusCode != 200 && response.StatusCode != 301 && response.StatusCode != 302 {
if utils.Debug { // 调试模式下,输出更多信息
fmt.Printf("\033[31m[调试] IP: %s, 延迟测速终止HTTP 状态码: %d, 测速地址: %s\033[0m\n", ip.String(), response.StatusCode, URL)
utils.Red.Printf("[调试] IP: %s, 延迟测速终止HTTP 状态码: %d, 测速地址: %s\n", ip.String(), response.StatusCode, URL)
}
return 0, 0, ""
}
} else {
if response.StatusCode != HttpingStatusCode {
if utils.Debug { // 调试模式下,输出更多信息
fmt.Printf("\033[31m[调试] IP: %s, 延迟测速终止HTTP 状态码: %d, 指定的 HTTP 状态码 %d, 测速地址: %s\033[0m\n", ip.String(), response.StatusCode, HttpingStatusCode, URL)
utils.Red.Printf("[调试] IP: %s, 延迟测速终止HTTP 状态码: %d, 指定的 HTTP 状态码 %d, 测速地址: %s\n", ip.String(), response.StatusCode, HttpingStatusCode, URL)
}
return 0, 0, ""
}
@@ -87,7 +87,7 @@ func (p *Ping) httping(ip *net.IPAddr) (int, time.Duration, string) {
colo = p.filterColo(colo)
if colo == "" { // 没有匹配到地区码或不符合指定地区则直接结束该 IP 测试
if utils.Debug { // 调试模式下,输出更多信息
fmt.Printf("\033[31m[调试] IP: %s, 地区码不匹配: %s\033[0m\n", ip.String(), colo)
utils.Red.Printf("[调试] IP: %s, 地区码不匹配: %s\n", ip.String(), colo)
}
return 0, 0, ""
}

View File

@@ -64,9 +64,9 @@ func (p *Ping) Run() utils.PingDelaySet {
return p.csv
}
if Httping {
fmt.Printf("\033[34m开始延迟测速模式HTTP, 端口:%d, 范围:%v ~ %v ms, 丢包:%.2f)\033[0m\n", TCPPort, utils.InputMinDelay.Milliseconds(), utils.InputMaxDelay.Milliseconds(), utils.InputMaxLossRate)
utils.Cyan.Printf("开始延迟测速模式HTTP, 端口:%d, 范围:%v ~ %v ms, 丢包:%.2f)\n", TCPPort, utils.InputMinDelay.Milliseconds(), utils.InputMaxDelay.Milliseconds(), utils.InputMaxLossRate)
} else {
fmt.Printf("\033[34m开始延迟测速模式TCP, 端口:%d, 范围:%v ~ %v ms, 丢包:%.2f)\033[0m\n", TCPPort, utils.InputMinDelay.Milliseconds(), utils.InputMaxDelay.Milliseconds(), utils.InputMaxLossRate)
utils.Cyan.Printf("开始延迟测速模式TCP, 端口:%d, 范围:%v ~ %v ms, 丢包:%.2f)\n", TCPPort, utils.InputMinDelay.Milliseconds(), utils.InputMaxDelay.Milliseconds(), utils.InputMaxLossRate)
}
for _, ip := range p.ips {
p.wg.Add(1)

16
utils/color.go Normal file
View File

@@ -0,0 +1,16 @@
package utils
import (
"github.com/fatih/color"
)
// 由专业的库来处理多平台的颜色输出效果
var (
Red = color.New(color.FgRed) // 红色 31
Green = color.New(color.FgGreen) // 绿色 32
Yellow = color.New(color.FgYellow) // 黄色 33
Blue = color.New(color.FgBlue, color.Bold) // 蓝色 34
Magenta = color.New(color.FgMagenta) // 紫红色 35
Cyan = color.New(color.FgHiCyan, color.Bold) // 青色 36
White = color.New(color.FgWhite) // 白色 37
)

View File

@@ -176,16 +176,16 @@ func (s DownloadSpeedSet) Print() {
if len(dateString) < PrintNum { // 如果IP数组长度(IP数量) 小于 打印次数则次数改为IP数量
PrintNum = len(dateString)
}
headFormat := "\033[34m%-16s%-5s%-5s%-5s%-6s%-12s%-5s\033[0m\n"
headFormat := "%-16s%-5s%-5s%-5s%-6s%-12s%-5s\n"
dataFormat := "%-18s%-8s%-8s%-8s%-10s%-16s%-8s\n"
for i := 0; i < PrintNum; i++ { // 如果要输出的 IP 中包含 IPv6那么就需要调整一下间隔
if len(dateString[i][0]) > 15 {
headFormat = "\033[34m%-40s%-5s%-5s%-5s%-6s%-12s%-5s\033[0m\n"
headFormat = "%-40s%-5s%-5s%-5s%-6s%-12s%-5s\n"
dataFormat = "%-42s%-8s%-8s%-8s%-10s%-16s%-8s\n"
break
}
}
fmt.Printf(headFormat, "IP 地址", "已发送", "已接收", "丢包率", "平均延迟", "下载速度(MB/s)", "地区码")
Cyan.Printf(headFormat, "IP 地址", "已发送", "已接收", "丢包率", "平均延迟", "下载速度(MB/s)", "地区码")
for i := 0; i < PrintNum; i++ {
fmt.Printf(dataFormat, dateString[i][0], dateString[i][1], dateString[i][2], dateString[i][3], dateString[i][4], dateString[i][5], dateString[i][6])
}