增加交互式配置支持
parent
604e5bb4ad
commit
92c92e67e1
169
flag.go
169
flag.go
|
@ -3,6 +3,18 @@ package main
|
|||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"github.com/charmbracelet/bubbles/key"
|
||||
inf "github.com/fzdwx/infinite"
|
||||
"github.com/fzdwx/infinite/color"
|
||||
"github.com/fzdwx/infinite/components"
|
||||
"github.com/fzdwx/infinite/components/input/text"
|
||||
"github.com/fzdwx/infinite/components/selection/confirm"
|
||||
"github.com/fzdwx/infinite/components/selection/singleselect"
|
||||
"github.com/fzdwx/infinite/style"
|
||||
"github.com/fzdwx/infinite/theme"
|
||||
"go.bug.st/serial"
|
||||
"log"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
|
@ -45,6 +57,16 @@ var (
|
|||
flags = []Flag{portName, baudRate, dataBits, stopBits, outputCode, inputCode, endStr, enableLog, logFilePath, forWard, address, frameSize, parityBit}
|
||||
)
|
||||
|
||||
var (
|
||||
bauds = []string{"自定义", "300", "600", "1200", "2400", "4800", "9600",
|
||||
"14400", "19200", "38400", "56000", "57600", "115200", "128000",
|
||||
"256000", "460800", "512000", "750000", "921600", "1500000"}
|
||||
datas = []string{"5", "6", "7", "8"}
|
||||
stops = []string{"1", "1.5", "2"}
|
||||
paritys = []string{"无校验", "奇校验", "偶校验", "1校验", "0校验"}
|
||||
forwards = []string{"No", "TCP-C", "UDP-C"}
|
||||
)
|
||||
|
||||
type ValType int
|
||||
|
||||
const (
|
||||
|
@ -99,3 +121,150 @@ func flagInit(f *Flag) {
|
|||
flag.IntVar(f.v.int, f.lStr, f.dv.int, f.help)
|
||||
}
|
||||
}
|
||||
|
||||
func getCliFlag() {
|
||||
ports, err := serial.GetPortsList()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
inputs := components.NewInput()
|
||||
inputs.Prompt = "Filtering: "
|
||||
inputs.PromptStyle = style.New().Bold().Italic().Fg(color.LightBlue)
|
||||
|
||||
selectKeymap := singleselect.DefaultSingleKeyMap()
|
||||
selectKeymap.Confirm = key.NewBinding(
|
||||
key.WithKeys("enter"),
|
||||
key.WithHelp("enter", "finish select"),
|
||||
)
|
||||
selectKeymap.Choice = key.NewBinding(
|
||||
key.WithKeys("enter"),
|
||||
key.WithHelp("enter", "finish select"),
|
||||
)
|
||||
selectKeymap.NextPage = key.NewBinding(
|
||||
key.WithKeys("right"),
|
||||
key.WithHelp("->", "next page"),
|
||||
)
|
||||
selectKeymap.PrevPage = key.NewBinding(
|
||||
key.WithKeys("left"),
|
||||
key.WithHelp("<-", "prev page"),
|
||||
)
|
||||
|
||||
s, _ := inf.NewSingleSelect(
|
||||
ports,
|
||||
singleselect.WithKeyBinding(selectKeymap),
|
||||
singleselect.WithPageSize(3),
|
||||
singleselect.WithFilterInput(inputs),
|
||||
).Display("选择串口")
|
||||
config.portName = ports[s]
|
||||
|
||||
s, _ = inf.NewSingleSelect(
|
||||
bauds,
|
||||
singleselect.WithKeyBinding(selectKeymap),
|
||||
singleselect.WithPageSize(3),
|
||||
).Display("选择波特率")
|
||||
if s != 0 {
|
||||
config.baudRate, _ = strconv.Atoi(bauds[s])
|
||||
} else {
|
||||
b, _ := inf.NewText(
|
||||
text.WithPrompt("BaudRate:"),
|
||||
text.WithPromptStyle(theme.DefaultTheme.PromptStyle),
|
||||
text.WithDefaultValue("115200"),
|
||||
).Display()
|
||||
config.baudRate, _ = strconv.Atoi(b)
|
||||
}
|
||||
v, _ := inf.NewConfirmWithSelection(
|
||||
confirm.WithPrompt("启用Hex"),
|
||||
).Display()
|
||||
if v {
|
||||
config.inputCode = "hex"
|
||||
}
|
||||
v, _ = inf.NewConfirmWithSelection(
|
||||
confirm.WithDefaultYes(),
|
||||
confirm.WithPrompt("启用高级配置"),
|
||||
).Display()
|
||||
if v {
|
||||
s, _ = inf.NewSingleSelect(
|
||||
datas,
|
||||
singleselect.WithKeyBinding(selectKeymap),
|
||||
singleselect.WithPageSize(3),
|
||||
singleselect.WithFilterInput(inputs),
|
||||
).Display("选择数据位")
|
||||
config.dataBits, _ = strconv.Atoi(datas[s])
|
||||
|
||||
s, _ = inf.NewSingleSelect(
|
||||
stops,
|
||||
singleselect.WithKeyBinding(selectKeymap),
|
||||
singleselect.WithPageSize(3),
|
||||
singleselect.WithFilterInput(inputs),
|
||||
).Display("选择停止位")
|
||||
config.stopBits = s
|
||||
|
||||
s, _ = inf.NewSingleSelect(
|
||||
paritys,
|
||||
singleselect.WithKeyBinding(selectKeymap),
|
||||
singleselect.WithPageSize(4),
|
||||
singleselect.WithFilterInput(inputs),
|
||||
).Display("选择校验位")
|
||||
config.parityBit = s
|
||||
|
||||
t, _ := inf.NewText(
|
||||
text.WithPrompt("换行符:"),
|
||||
text.WithPromptStyle(theme.DefaultTheme.PromptStyle),
|
||||
text.WithDefaultValue(endStr.dv.string),
|
||||
).Display()
|
||||
config.endStr = t
|
||||
|
||||
v, _ = inf.NewConfirmWithSelection(
|
||||
confirm.WithDefaultYes(),
|
||||
confirm.WithPrompt("启用编码转换"),
|
||||
).Display()
|
||||
|
||||
if v {
|
||||
t, _ = inf.NewText(
|
||||
text.WithPrompt("输入编码:"),
|
||||
text.WithPromptStyle(theme.DefaultTheme.PromptStyle),
|
||||
text.WithDefaultValue(inputCode.dv.string),
|
||||
).Display()
|
||||
config.inputCode = t
|
||||
|
||||
t, _ = inf.NewText(
|
||||
text.WithPrompt("输出编码:"),
|
||||
text.WithPromptStyle(theme.DefaultTheme.PromptStyle),
|
||||
text.WithDefaultValue(outputCode.dv.string),
|
||||
).Display()
|
||||
config.outputCode = t
|
||||
}
|
||||
|
||||
s, _ = inf.NewSingleSelect(
|
||||
forwards,
|
||||
singleselect.WithKeyBinding(selectKeymap),
|
||||
singleselect.WithPageSize(3),
|
||||
singleselect.WithFilterInput(inputs),
|
||||
).Display("选择转发模式")
|
||||
if s != 0 {
|
||||
config.forWard = s
|
||||
t, _ = inf.NewText(
|
||||
text.WithPrompt("地址:"),
|
||||
text.WithPromptStyle(theme.DefaultTheme.PromptStyle),
|
||||
text.WithDefaultValue(address.dv.string),
|
||||
).Display()
|
||||
config.address = t
|
||||
}
|
||||
|
||||
e, _ := inf.NewConfirmWithSelection(
|
||||
confirm.WithDefaultYes(),
|
||||
confirm.WithPrompt("启用日志"),
|
||||
).Display()
|
||||
config.enableLog = e
|
||||
if e {
|
||||
t, _ = inf.NewText(
|
||||
text.WithPrompt("Path:"),
|
||||
text.WithPromptStyle(theme.DefaultTheme.PromptStyle),
|
||||
text.WithDefaultValue(logFilePath.dv.string),
|
||||
).Display()
|
||||
config.logFilePath = t
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
27
go.mod
27
go.mod
|
@ -3,12 +3,35 @@ module COM
|
|||
go 1.22
|
||||
|
||||
require (
|
||||
github.com/fzdwx/infinite v0.12.1
|
||||
github.com/zimolab/charsetconv v0.1.2
|
||||
go.bug.st/serial v1.6.2
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/atotto/clipboard v0.1.4 // indirect
|
||||
github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect
|
||||
github.com/charmbracelet/bubbles v0.16.1 // indirect
|
||||
github.com/charmbracelet/bubbletea v0.24.2 // indirect
|
||||
github.com/charmbracelet/lipgloss v0.7.1 // indirect
|
||||
github.com/containerd/console v1.0.4-0.20230313162750-1ae8d489ac81 // indirect
|
||||
github.com/creack/goselect v0.1.2 // indirect
|
||||
golang.org/x/sys v0.4.0 // indirect
|
||||
golang.org/x/text v0.6.0 // indirect
|
||||
github.com/duke-git/lancet/v2 v2.2.1 // indirect
|
||||
github.com/fzdwx/iter v0.0.0-20230511075109-0afee9319312 // indirect
|
||||
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
|
||||
github.com/mattn/go-isatty v0.0.19 // indirect
|
||||
github.com/mattn/go-localereader v0.0.1 // indirect
|
||||
github.com/mattn/go-runewidth v0.0.14 // indirect
|
||||
github.com/muesli/ansi v0.0.0-20230316100256-276c6243b2f6 // indirect
|
||||
github.com/muesli/cancelreader v0.2.2 // indirect
|
||||
github.com/muesli/reflow v0.3.0 // indirect
|
||||
github.com/muesli/termenv v0.15.1 // indirect
|
||||
github.com/rivo/uniseg v0.4.4 // indirect
|
||||
github.com/rotisserie/eris v0.5.4 // indirect
|
||||
github.com/sahilm/fuzzy v0.1.0 // indirect
|
||||
golang.org/x/exp v0.0.0-20230522175609-2e198f4a06a1 // 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.9.0 // indirect
|
||||
)
|
||||
|
|
13
main.go
13
main.go
|
@ -50,6 +50,15 @@ func init() {
|
|||
for _, f := range flags {
|
||||
flagInit(&f)
|
||||
}
|
||||
flag.Func("h", "获取帮助", func(s string) error {
|
||||
ports, err := checkPortAvailability(s)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
printUsage(ports)
|
||||
os.Exit(0)
|
||||
}
|
||||
return err
|
||||
})
|
||||
cmdinit()
|
||||
}
|
||||
|
||||
|
@ -106,6 +115,10 @@ func output() {
|
|||
}
|
||||
func main() {
|
||||
flag.Parse()
|
||||
|
||||
if config.portName == "" {
|
||||
getCliFlag()
|
||||
}
|
||||
ports, err := checkPortAvailability(config.portName)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
|
|
Loading…
Reference in New Issue