From c841dfeae45f05392dc259408f68ab74e74c0024 Mon Sep 17 00:00:00 2001 From: jixishi Date: Mon, 3 Jun 2024 20:54:23 +0800 Subject: [PATCH] =?UTF-8?q?UP=20=E5=8A=9F=E8=83=BD=E5=BB=BA=E8=AE=AE?= =?UTF-8?q?=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 +- config.go | 5 +++- flag.go | 72 +++++++++++++++++++++++++++++++++---------------------- go.mod | 7 +++--- main.go | 5 ++-- 5 files changed, 54 insertions(+), 37 deletions(-) diff --git a/README.md b/README.md index a1a43a1..4e2c3c1 100644 --- a/README.md +++ b/README.md @@ -48,5 +48,5 @@ ![img11.png](image/img11.png) 9. 时间戳 `./COM -p COM8 -t` ![img9.png](image/img9.png) -10. 格式修改 `./COM -p COM11 -t -T='<2006-01-02 15:04:05>'` +10. 格式修改 `./COM -p COM11 -t='<2006-01-02 15:04:05>'` ![img10.png](image/img10.png) \ No newline at end of file diff --git a/config.go b/config.go index 385c1bf..ddd07b7 100644 --- a/config.go +++ b/config.go @@ -1,9 +1,11 @@ package main import ( + "fmt" "log" "net" "os" + "time" ) type Config struct { @@ -55,7 +57,8 @@ func setForWardClient() (conn net.Conn) { func checkLogOpen() { if config.enableLog { - f, err := os.OpenFile(config.logFilePath, os.O_RDWR|os.O_APPEND|os.O_CREATE, 0666) + path := fmt.Sprintf(config.logFilePath, config.portName, time.Now().Format("2006_01_02T150405")) + f, err := os.OpenFile(path, os.O_RDWR|os.O_APPEND|os.O_CREATE, 0666) if err != nil { log.Fatal(err) } diff --git a/flag.go b/flag.go index 9f9bbaf..19d2f40 100644 --- a/flag.go +++ b/flag.go @@ -1,7 +1,6 @@ package main import ( - "flag" "fmt" "github.com/charmbracelet/bubbles/key" inf "github.com/fzdwx/infinite" @@ -12,6 +11,7 @@ import ( "github.com/fzdwx/infinite/components/selection/singleselect" "github.com/fzdwx/infinite/style" "github.com/fzdwx/infinite/theme" + "github.com/spf13/pflag" "go.bug.st/serial" "log" "strconv" @@ -24,6 +24,7 @@ type ptrVal struct { *bool *float64 *float32 + ext *string } type Val struct { string @@ -31,6 +32,7 @@ type Val struct { bool float64 float32 + extdef string } type Flag struct { v ptrVal @@ -41,22 +43,20 @@ type Flag struct { } var ( - portName = Flag{ptrVal{string: &config.portName}, "p", "port", Val{string: ""}, "要连接的串口\t(/dev/ttyUSB0、COMx)"} - baudRate = Flag{ptrVal{int: &config.baudRate}, "b", "baud", Val{int: 115200}, "波特率"} - dataBits = Flag{ptrVal{int: &config.dataBits}, "d", "data", Val{int: 8}, "数据位"} - stopBits = Flag{ptrVal{int: &config.stopBits}, "s", "stop", Val{int: 0}, "停止位停止位(0: 1停止 1:1.5停止 2:2停止)"} - outputCode = Flag{ptrVal{string: &config.outputCode}, "o", "out", Val{string: "UTF-8"}, "输出编码"} - inputCode = Flag{ptrVal{string: &config.inputCode}, "i", "in", Val{string: "UTF-8"}, "输入编码"} - endStr = Flag{ptrVal{string: &config.endStr}, "e", "end", Val{string: "\n"}, "终端换行符"} - enableLog = Flag{ptrVal{bool: &config.enableLog}, "l", "log", Val{bool: false}, "是否启用日志保存"} - logFilePath = Flag{ptrVal{string: &config.logFilePath}, "P", "Path", Val{string: "./Log.txt"}, "日志保存路径"} - forWard = Flag{ptrVal{int: &config.forWard}, "f", "forward", Val{int: 0}, "转发模式(0: 无 1:TCP-C 2:UDP-C)"} - address = Flag{ptrVal{string: &config.address}, "a", "address", Val{string: "127.0.0.1:12345"}, "转发服务地址"} - frameSize = Flag{ptrVal{int: &config.frameSize}, "F", "Frame", Val{int: 16}, "帧大小"} - timesTamp = Flag{ptrVal{bool: &config.timesTamp}, "t", "tamp", Val{bool: false}, "是否启用接收时间戳"} - timesFmt = Flag{ptrVal{string: &config.timesFmt}, "T", "TimeFmt", Val{string: "[06-01-02 15:04:05]"}, "时间戳格式化字段"} - parityBit = Flag{ptrVal{int: &config.parityBit}, "v", "verify", Val{int: 0}, "奇偶校验(0:无校验、1:奇校验、2:偶校验、3:1校验、4:0校验)"} - flags = []Flag{portName, baudRate, dataBits, stopBits, outputCode, inputCode, endStr, enableLog, logFilePath, forWard, address, frameSize, timesTamp, timesFmt, parityBit} + portName = Flag{ptrVal{string: &config.portName}, "p", "port", Val{string: ""}, "要连接的串口\t(/dev/ttyUSB0、COMx)"} + baudRate = Flag{ptrVal{int: &config.baudRate}, "b", "baud", Val{int: 115200}, "波特率"} + dataBits = Flag{ptrVal{int: &config.dataBits}, "d", "data", Val{int: 8}, "数据位"} + stopBits = Flag{ptrVal{int: &config.stopBits}, "s", "stop", Val{int: 0}, "停止位停止位(0: 1停止 1:1.5停止 2:2停止)"} + outputCode = Flag{ptrVal{string: &config.outputCode}, "o", "out", Val{string: "UTF-8"}, "输出编码"} + inputCode = Flag{ptrVal{string: &config.inputCode}, "i", "in", Val{string: "UTF-8"}, "输入编码"} + endStr = Flag{ptrVal{string: &config.endStr}, "e", "end", Val{string: "\n"}, "终端换行符"} + logExt = Flag{v: ptrVal{ext: &config.logFilePath}, sStr: "l", lStr: "log", dv: Val{extdef: "./%s-$s.txt", string: ""}, help: "日志保存路径"} + timeExt = Flag{v: ptrVal{ext: &config.timesFmt}, sStr: "t", lStr: "time", dv: Val{extdef: "[06-01-02 15:04:05.000]", string: ""}, help: "时间戳格式化字段"} + forWard = Flag{ptrVal{int: &config.forWard}, "f", "forward", Val{int: 0}, "转发模式(0: 无 1:TCP-C 2:UDP-C)"} + address = Flag{ptrVal{string: &config.address}, "a", "address", Val{string: "127.0.0.1:12345"}, "转发服务地址"} + frameSize = Flag{ptrVal{int: &config.frameSize}, "F", "Frame", Val{int: 16}, "帧大小"} + parityBit = Flag{ptrVal{int: &config.parityBit}, "v", "verify", Val{int: 0}, "奇偶校验(0:无校验、1:奇校验、2:偶校验、3:1校验、4:0校验)"} + flags = []Flag{portName, baudRate, dataBits, stopBits, outputCode, inputCode, endStr, forWard, address, frameSize, parityBit, logExt, timeExt} ) var ( @@ -76,6 +76,7 @@ const ( stringVal intVal boolVal + extVal ) func printUsage(ports []string) { @@ -95,6 +96,9 @@ func flagFindValue(v ptrVal) ValType { if v.int != nil { return intVal } + if v.ext != nil { + return extVal + } return notVal } func flagprint(f Flag) { @@ -105,25 +109,35 @@ func flagprint(f Flag) { fmt.Printf("\t-%v -%v %T \n\t %v\t默认值:%v\n", f.sStr, f.lStr, f.dv.int, f.help, f.dv.int) case boolVal: fmt.Printf("\t-%v -%v %T \n\t %v\t默认值:%v\n", f.sStr, f.lStr, f.dv.bool, f.help, f.dv.bool) + case extVal: + fmt.Printf("\t-%v -%v %T \n\t %v\t默认值:%v\n", f.sStr, f.lStr, f.dv.extdef, f.help, f.dv.extdef) default: panic("unhandled default case") } } func flagInit(f *Flag) { if f.v.string != nil { - flag.StringVar(f.v.string, f.sStr, f.dv.string, "") - flag.StringVar(f.v.string, f.lStr, f.dv.string, f.help) + pflag.StringVarP(f.v.string, f.lStr, f.sStr, f.dv.string, f.help) } if f.v.bool != nil { - flag.BoolVar(f.v.bool, f.sStr, f.dv.bool, "") - flag.BoolVar(f.v.bool, f.lStr, f.dv.bool, f.help) + pflag.BoolVarP(f.v.bool, f.lStr, f.sStr, f.dv.bool, f.help) } if f.v.int != nil { - flag.IntVar(f.v.int, f.sStr, f.dv.int, "") - flag.IntVar(f.v.int, f.lStr, f.dv.int, f.help) + pflag.IntVarP(f.v.int, f.lStr, f.sStr, f.dv.int, f.help) + } + if f.v.ext != nil { + pflag.StringVarP(f.v.ext, f.lStr, f.sStr, f.dv.string, f.help) + pflag.Lookup(f.lStr).NoOptDefVal = f.dv.extdef + } +} +func flagExt() { + if config.logFilePath != "" { + config.enableLog = true + } + if config.timesFmt != "" { + config.timesTamp = true } } - func getCliFlag() { ports, err := serial.GetPortsList() if err != nil { @@ -190,14 +204,14 @@ func getCliFlag() { v, _ = inf.NewConfirmWithSelection( confirm.WithPrompt("启用时间戳"), ).Display() + config.timesTamp = v if v { - config.inputCode = "hex" b, _ := inf.NewText( - text.WithPrompt("Frames:"), + text.WithPrompt("格式化字段:"), text.WithPromptStyle(theme.DefaultTheme.PromptStyle), - text.WithDefaultValue("16"), + text.WithDefaultValue(logExt.dv.extdef), ).Display() - config.frameSize, _ = strconv.Atoi(b) + config.timesFmt = b } v, _ = inf.NewConfirmWithSelection( confirm.WithPrompt("启用高级配置"), @@ -280,7 +294,7 @@ func getCliFlag() { t, _ = inf.NewText( text.WithPrompt("Path:"), text.WithPromptStyle(theme.DefaultTheme.PromptStyle), - text.WithDefaultValue(logFilePath.dv.string), + text.WithDefaultValue("./%s-$s.txt"), ).Display() config.logFilePath = t } diff --git a/go.mod b/go.mod index 1984dde..4f82d1a 100644 --- a/go.mod +++ b/go.mod @@ -4,12 +4,12 @@ go 1.22 require ( github.com/charmbracelet/bubbles v0.18.0 - github.com/charmbracelet/bubbletea v0.25.0 github.com/fzdwx/infinite v0.12.1 - github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646 + github.com/spf13/pflag v1.0.5 github.com/trzsz/trzsz-go v1.1.7 github.com/zimolab/charsetconv v0.1.2 go.bug.st/serial v1.6.2 + golang.org/x/term v0.19.0 ) require ( @@ -18,6 +18,7 @@ require ( github.com/alexflint/go-scalar v1.2.0 // indirect github.com/atotto/clipboard v0.1.4 // indirect github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect + github.com/charmbracelet/bubbletea v0.25.0 // indirect github.com/charmbracelet/lipgloss v0.9.1 // indirect github.com/chzyer/readline v1.5.1 // indirect github.com/containerd/console v1.0.4-0.20230313162750-1ae8d489ac81 // indirect @@ -25,7 +26,6 @@ require ( github.com/creack/pty v1.1.21 // indirect github.com/dchest/jsmin v0.0.0-20220218165748-59f39799265f // indirect github.com/duke-git/lancet/v2 v2.2.1 // indirect - github.com/dustin/go-humanize v1.0.1 // indirect github.com/fzdwx/iter v0.0.0-20230511075109-0afee9319312 // indirect github.com/josephspurrier/goversioninfo v1.4.0 // indirect github.com/klauspost/compress v1.17.4 // indirect @@ -48,6 +48,5 @@ require ( golang.org/x/image v0.14.0 // indirect golang.org/x/sync v0.2.0 // indirect golang.org/x/sys v0.19.0 // indirect - golang.org/x/term v0.19.0 // indirect golang.org/x/text v0.14.0 // indirect ) diff --git a/main.go b/main.go index 7dfe378..b1d4c0b 100644 --- a/main.go +++ b/main.go @@ -2,8 +2,8 @@ package main import ( "bufio" - "flag" "fmt" + "github.com/spf13/pflag" "github.com/trzsz/trzsz-go/trzsz" "github.com/zimolab/charsetconv" "go.bug.st/serial" @@ -126,7 +126,8 @@ func output() { } } func main() { - flag.Parse() + pflag.Parse() + flagExt() if config.portName == "" { getCliFlag() }