-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
65 lines (58 loc) · 1.52 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
package main
import (
_ "embed"
"fmt"
"log"
"strings"
"github.com/eiannone/keyboard"
"github.com/senorL/TransCLI/conf"
"github.com/senorL/TransCLI/history"
"github.com/senorL/TransCLI/prediction"
"github.com/senorL/TransCLI/translate"
)
//go:embed asset/dict.txt
var dict string
func main() {
conf.GetConf()
prediction.LoadDict(dict)
var builder strings.Builder
err := keyboard.Open()
if err != nil {
log.Fatal(err)
}
defer keyboard.Close()
fmt.Printf("\r\033[1;32mTrans-CLI>\033[0m %s", builder.String())
for {
char, key, err := keyboard.GetKey()
if err != nil {
log.Fatal(err)
}
switch key {
case keyboard.KeyEnter:
fmt.Printf("\n\033[1;32mTrans-CLI>\033[0m ") // Trans-CLI>
translate.TranForBaidu(builder.String()) // 翻译
history.AddHistory(builder.String()) // 历史记录
builder.Reset() // Trans-CLI>
case keyboard.KeyArrowDown, keyboard.KeyArrowUp:
if key == keyboard.KeyArrowUp {
history.GetUpHistory(&builder)
}
if key == keyboard.KeyArrowDown {
history.GetDownHistory(&builder)
}
case keyboard.KeyBackspace, keyboard.KeyBackspace2:
str := builder.String()
if len(str) > 0 {
builder.Reset()
builder.WriteString(str[:len(str)-1])
}
case keyboard.KeyTab:
prediction.KeyTab(&builder)
case keyboard.KeySpace:
builder.WriteRune(' ')
default:
builder.WriteRune(char)
}
fmt.Printf("\r\033[1;32mTrans-CLI>\033[0m %s\033[0;90m%s\033[0m\033[K", builder.String(), prediction.Predict(&builder))
}
}