-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathwgo.go
135 lines (115 loc) · 2.23 KB
/
wgo.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
125
126
127
128
129
130
131
132
133
134
135
package main
import (
"github.com/c-bata/go-prompt"
"github.com/wxnacy/wgo/commands"
"flag"
"fmt"
"os"
"strings"
"time"
)
// 输入提示
func completer(d prompt.Document) []prompt.Suggest {
line := d.GetWordBeforeCursor()
var s = make([]prompt.Suggest, 0)
// filterString := d.GetWordBeforeCursor()
var prompts = make([]Prompt, 0)
if strings.Contains(line, ".") {
_, ok := commands.HasCommand("gocode")
if ok {
prompts = Complete(line)
// for _, d := range prompts {
// Logger().Debug(d)
// }
}
prefix := line[0 : strings.Index(line, ".")+1]
for _, p := range prompts {
s = append(s, prompt.Suggest{
Text: prefix + p.Name, Description: p.Class + " " + p.Type,
})
}
} else {
prompts = GetPromptBySpace()
for _, p := range prompts {
s = append(s, prompt.Suggest{
Text: p.Name, Description: p.Class,
})
}
}
// Logger().Debugf("s %v", s)
return prompt.FilterContains(s, d.GetWordBeforeCursor(), true)
}
// 执行
func executor(t string) {
b := time.Now()
cmd := strings.Split(t, " ")[0]
switch cmd {
case "exit":
{
os.Exit(0)
}
default:
{
Coder().Input(t)
Coder().Run()
}
}
Logger().Debugf("once command time used: %v", time.Since(b))
return
}
var LivePrefixState struct {
LivePrefix string
IsEnable bool
}
func changeLivePrefix() (string, bool) {
return LivePrefixState.LivePrefix, LivePrefixState.IsEnable
}
var VER = `%s
Wgo version %s
Copyright (C) 2019 wxnacy
`
var args []string
func initArgs() {
flag.Parse()
args = flag.Args()
// fmt.Println(args)
// fmt.Println(os.Args)
}
func commandArgs() {
if len(args) > 0 {
arg := args[0]
switch arg {
case "version":
{
fmt.Println(VERSION)
os.Exit(0)
}
case "logs":
{
// t, _ := tail.TailFile(tempDir() + "wgo.log", tail.Config{Follow: true})
// for {
// for line := range t.Lines {
// fmt.Println(line.Text)
// }
// }
}
}
}
}
func main() {
initArgs()
commandArgs()
initLogger()
goVer, _ := commands.Command("go", "version")
fmt.Println(fmt.Sprintf(VER, goVer, VERSION))
if len(args) == 0 {
p := prompt.New(
executor,
completer,
prompt.OptionPrefix(">>> "),
prompt.OptionLivePrefix(changeLivePrefix),
)
p.Run()
}
// destroyTempDir()
}