/
template.go
188 lines (170 loc) · 5.06 KB
/
template.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
package config
import (
"fmt"
"os"
"path/filepath"
"sort"
"strings"
"github.com/fatih/color"
ansi "github.com/k0kubun/go-ansi"
homedir "github.com/mitchellh/go-homedir"
"github.com/xalanq/cf-tool/client"
"github.com/xalanq/cf-tool/util"
)
// AddTemplate add template
func (c *Config) AddTemplate() (err error) {
color.Cyan("Add a template")
color.Cyan("Language list:")
type kv struct {
K, V string
}
langs := []kv{}
for k, v := range client.Langs {
langs = append(langs, kv{k, v})
}
sort.Slice(langs, func(i, j int) bool { return langs[i].V < langs[j].V })
for _, t := range langs {
fmt.Printf("%5v: %v\n", t.K, t.V)
}
color.Cyan(`Select a language (e.g. "42"): `)
lang := ""
for {
lang = util.ScanlineTrim()
if val, ok := client.Langs[lang]; ok {
color.Green(val)
break
}
color.Red("Invalid index. Please input again")
}
note := `Template:
You can insert some placeholders into your template code. When generate a code from the
template, cf will replace all placeholders by following rules:
$%U%$ Handle (e.g. xalanq)
$%Y%$ Year (e.g. 2019)
$%M%$ Month (e.g. 04)
$%D%$ Day (e.g. 09)
$%h%$ Hour (e.g. 08)
$%m%$ Minute (e.g. 05)
$%s%$ Second (e.g. 00)`
ansi.Println(note)
color.Cyan(`Template absolute path(e.g. "~/template/io.cpp"): `)
path := ""
for {
path = util.ScanlineTrim()
path, err = homedir.Expand(path)
if err == nil {
if _, err := os.Stat(path); err == nil {
break
}
}
color.Red("%v is invalid. Please input again: ", path)
}
color.Cyan(`The suffix of template above will be added by default.`)
color.Cyan(`Other suffix? (e.g. "cxx cc"), empty is ok: `)
tmpSuffix := strings.Fields(util.ScanlineTrim())
tmpSuffix = append(tmpSuffix, strings.Replace(filepath.Ext(path), ".", "", 1))
suffixMap := map[string]bool{}
suffix := []string{}
for _, s := range tmpSuffix {
if _, ok := suffixMap[s]; !ok {
suffixMap[s] = true
suffix = append(suffix, s)
}
}
color.Cyan(`Template's alias (e.g. "cpp" "py"): `)
alias := ""
for {
alias = util.ScanlineTrim()
if len(alias) > 0 {
break
}
color.Red("Alias can not be empty. Please input again: ")
}
color.Green("Script in template:")
note = `Template will run 3 scripts in sequence when you run "cf test":
- before_script (execute once)
- script (execute the number of samples times)
- after_script (execute once)
You could set "before_script" or "after_script" to empty string, meaning not executing.
You have to run your program in "script" with standard input/output (no need to redirect).
You can insert some placeholders in your scripts. When execute a script,
cf will replace all placeholders by following rules:
$%path%$ Path to source file (Excluding $%full%$, e.g. "/home/xalanq/")
$%full%$ Full name of source file (e.g. "a.cpp")
$%file%$ Name of source file (Excluding suffix, e.g. "a")
$%rand%$ Random string with 8 character (including "a-z" "0-9")`
ansi.Println(note)
color.Cyan(`Before script (e.g. "g++ $%full%$ -o $%file%$.exe -std=c++11"), empty is ok: `)
beforeScript := util.ScanlineTrim()
color.Cyan(`Script (e.g. "./$%file%$.exe" "python3 $%full%$"): `)
script := ""
for {
script = util.ScanlineTrim()
if len(script) > 0 {
break
}
color.Red("Script can not be empty. Please input again: ")
}
color.Cyan(`After script (e.g. "rm $%file%$.exe" or "cmd.exe /C del $%file%$.exe" in windows), empty is ok: `)
afterScript := util.ScanlineTrim()
c.Template = append(c.Template, CodeTemplate{
alias, lang, path, suffix,
beforeScript, script, afterScript,
})
if util.YesOrNo("Make it default (y/n)? ") {
c.Default = len(c.Template) - 1
}
return c.save()
}
// RemoveTemplate remove template
func (c *Config) RemoveTemplate() (err error) {
color.Cyan("Remove a template")
if len(c.Template) == 0 {
color.Red("There is no template. Please add one")
return nil
}
for i, template := range c.Template {
star := " "
if i == c.Default {
star = color.New(color.FgGreen).Sprint("*")
}
ansi.Printf(`%v%2v: "%v" "%v"`, star, i, template.Alias, template.Path)
ansi.Println()
}
idx := util.ChooseIndex(len(c.Template))
c.Template = append(c.Template[:idx], c.Template[idx+1:]...)
if idx == c.Default {
c.Default = 0
} else if idx < c.Default {
c.Default--
}
return c.save()
}
// SetDefaultTemplate set default template index
func (c *Config) SetDefaultTemplate() error {
color.Cyan("Set default template")
if len(c.Template) == 0 {
color.Red("There is no template. Please add one")
return nil
}
for i, template := range c.Template {
star := " "
if i == c.Default {
star = color.New(color.FgGreen).Sprint("*")
}
ansi.Printf(`%v%2v: "%v" "%v"`, star, i, template.Alias, template.Path)
ansi.Println()
}
c.Default = util.ChooseIndex(len(c.Template))
return c.save()
}
// TemplateByAlias return all template which alias equals to alias
func (c *Config) TemplateByAlias(alias string) []CodeTemplate {
ret := []CodeTemplate{}
for _, template := range c.Template {
if template.Alias == alias {
ret = append(ret, template)
}
}
return ret
}