-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
60 lines (52 loc) · 1005 Bytes
/
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
package main
import (
"bufio"
"fmt"
"os"
"runtime/debug"
"strings"
)
func tryCatch(task func(), errCallback func(R any)) {
defer func() {
if r := recover(); r != nil {
errCallback(r)
debug.PrintStack()
}
}()
task()
}
func mainTask() {
prefix, _ := os.Getwd()
sourceFile, err := os.Open(os.Args[1])
if err != nil {
panic("无法打开源文件")
}
defer sourceFile.Close()
targetFile, err := os.Create(prefix + "/cn.srt")
if err != nil {
panic("无法创建新文件")
}
defer targetFile.Close()
scanner := bufio.NewScanner(sourceFile)
var block []string
for scanner.Scan() {
line := scanner.Text()
if strings.TrimSpace(line) != "" {
block = append(block, line)
} else {
for i := 0; i < len(block)-1; i++ {
targetFile.WriteString(block[i] + "\n")
}
targetFile.WriteString("\n")
block = nil
}
if err := scanner.Err(); err != nil {
panic("扫描出错")
}
}
}
func main() {
tryCatch(mainTask, func(R any) {
fmt.Println(R)
})
}