-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
124 lines (110 loc) · 3.07 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package main
import (
"bufio"
"fmt"
"io"
"os"
"strconv"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/app"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/dialog"
"fyne.io/fyne/v2/storage"
"fyne.io/fyne/v2/widget"
)
func main() {
a := app.New()
w := a.NewWindow("File Spliter")
splitValueEntry := widget.NewEntry()
splitValueEntry.SetPlaceHolder("整数")
unitSelect := widget.NewSelect([]string{UNIT_KB, UNIT_MB}, func(value string) {})
splitType := widget.NewSelect([]string{SPLIT_BY_LINE, SPLIT_BY_SIZE}, func(value string) {
if value == SPLIT_BY_LINE {
splitValueEntry.SetText(SPLIT_LINE_DEFAULT)
unitSelect.Hide()
} else {
splitValueEntry.SetText(SPLIT_SIZE_DEFAULT)
unitSelect.SetSelectedIndex(1)
unitSelect.Show()
}
})
splitType.SetSelectedIndex(0)
firstLineCheck := widget.NewCheck("首行列名", func(checked bool) {})
fileLabel := widget.NewLabel("")
fileInfoLabel := widget.NewLabel("")
var filePath string
openButton := widget.NewButton("打开", func() {
fileDialog := dialog.NewFileOpen(func(reader fyne.URIReadCloser, err error) {
if err != nil {
dialog.ShowError(err, w)
return
}
if reader != nil {
filePath = reader.URI().Path()
fileLabel.SetText(filePath)
fileLines, fileSize, _ := getFileInfo(filePath)
fileInfoLabel.SetText(fmt.Sprintf("行数:%d, 大小: %s", fileLines, fileSize))
}
}, w)
fileDialog.SetFilter(storage.NewExtensionFileFilter([]string{".csv", ".txt"}))
fileDialog.Show()
})
form := &widget.Form{
Items: []*widget.FormItem{
{Text: "文件", Widget: openButton},
{Text: "", Widget: fileLabel},
{Text: "文件信息", Widget: fileInfoLabel},
{Text: "分割方式", Widget: splitType},
{Text: "行数/大小", Widget: splitValueEntry},
{Text: "单位", Widget: unitSelect},
{Text: "", Widget: firstLineCheck},
},
SubmitText: "执行",
OnSubmit: func() {
if splitValueEntry.Text == "" {
dialog.ShowError(fmt.Errorf("请输入行数或文件大小"), w)
return
}
num, err := strconv.Atoi(splitValueEntry.Text)
if err != nil {
dialog.ShowError(fmt.Errorf("不是整数: %s", splitValueEntry.Text), w)
return
}
if filePath == "" {
dialog.ShowError(fmt.Errorf("没有选择文件"), w)
return
}
splitFile(filePath, splitType.Selected, num, unitSelect.Selected, firstLineCheck.Checked, w)
dialog.ShowInformation("INFO", "分割完成", w)
},
}
w.SetContent(container.NewVBox(form))
w.Resize(fyne.NewSize(600, 400))
w.ShowAndRun()
}
// getFileInfo 获取文件信息
func getFileInfo(filename string) (fileLine int, fileSize string, err error) {
// 获取文件信息
fileInfo, err := os.Stat(filename)
if err != nil {
return
}
fileSize = bytesToString(fileInfo.Size())
file, err := os.Open(filename)
if err != nil {
return
}
defer file.Close()
reader := bufio.NewReaderSize(file, 4096) // 设置缓冲区大小
for {
_, err := reader.ReadBytes('\n') // 读取到下一个换行符
if err != nil {
if err == io.EOF {
break // 文件结束
}
return fileLine, fileSize, err
}
fileLine++
}
return
}