拓展类型 自定义工具类型

This commit is contained in:
2024-10-10 18:22:50 +08:00
parent 3b69ba93ad
commit 1a0a9466d8
14 changed files with 374 additions and 31 deletions
+6 -3
View File
@@ -14,7 +14,9 @@ import (
func ErlangCardView(w fyne.Window, data *models.ErlangCards) fyne.CanvasObject {
if len(data.VarName) == 0 {
return widget.NewCard(data.Title, data.Text, widget.NewButton("复制", func() {
err := clipboard.WriteAll(data.Template)
cleanedText := strings.ReplaceAll(data.Template, "\r", "")
cleanedText = strings.ReplaceAll(cleanedText, "\n", "")
err := clipboard.WriteAll(cleanedText)
if err != nil {
dialog.ShowError(err, w)
return
@@ -121,6 +123,7 @@ func replaceVars(input string, replacements []string) string {
result = strings.Replace(result, "$var", replacements[varIndex], 1)
varIndex++
}
return result
cleanedText := strings.ReplaceAll(result, "\r", "")
cleanedText = strings.ReplaceAll(cleanedText, "\n", "")
return cleanedText
}
+63
View File
@@ -0,0 +1,63 @@
package showView
import (
"encoding/json"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/dialog"
"fyne.io/fyne/v2/widget"
"os"
"os/exec"
"path/filepath"
"work_cation/cfg"
"work_cation/models"
)
func ExecFileCardView(w fyne.Window, data *models.BaseCard) fyne.CanvasObject {
cardButton := widget.NewButton("启动", func() { runExecFile(data, w) })
openDirButton := widget.NewButton("打开目录", func() { openDir(data, w) })
card := widget.NewCard(data.Title, data.Text, container.NewVBox(cardButton, openDirButton))
return card
}
func runExecFile(data *models.BaseCard, w fyne.Window) {
infoPath := filepath.Join(cfg.T.CardDir, data.UUID, cfg.T.CardInfo)
bytes, err := os.ReadFile(infoPath)
if err != nil {
dialog.ShowInformation("测试失败", err.Error(), w)
return
}
var info models.ExecFiles
err = json.Unmarshal(bytes, &info)
if err != nil {
dialog.ShowInformation("测试失败", err.Error(), w)
return
}
ex := exec.Command(info.Cmd)
ex.Dir = filepath.Join(cfg.T.CardDir, data.UUID, info.Pwd)
if err := ex.Start(); err != nil {
dialog.ShowInformation("测试失败", err.Error(), w)
return
}
go func() {
if err := ex.Wait(); err != nil {
dialog.ShowInformation("测试失败", err.Error(), w)
}
}()
}
func openDir(data *models.BaseCard, w fyne.Window) {
infoPath := filepath.Join(cfg.T.CardDir, data.UUID)
// 打开目录
//utils.File.WinOpenFolder(infoPath)
cmd := exec.Command("explorer", infoPath)
if err := cmd.Start(); err != nil {
dialog.ShowInformation("打开失败", err.Error(), w)
}
go func() {
if err := cmd.Wait(); err != nil && err.Error() != "exit status 1" {
dialog.ShowInformation("测试失败", err.Error(), w)
}
}()
}