diff --git a/README.md b/README.md index b17cd771..bb36f0ee 100644 --- a/README.md +++ b/README.md @@ -84,12 +84,12 @@ List problems' stats of the contest. Open the standings' page of the contest. ```plain -You should run "cf config login" and "cf config add" at first. +You should run "cf config" to configure your username, password and the code template at first. If you want to compete, the best command is "cf race 1111" where "1111" is the contest id. Usage: - cf config (login | add | del | default) + cf config cf submit [( )] [] cf list [] cf parse [] [] @@ -105,10 +105,7 @@ Usage: cf upgrade Examples: - cf config login Config your username and password. - cf config add Add a template. - cf config del Remove a template. - cf config default Set default template. + cf config Configure the cf-tool. cf submit If current path is "/", cf will find the code which can be submitted. Then submit to . cf submit a.cpp diff --git a/README_zh_CN.md b/README_zh_CN.md index 47a27598..04cd4f16 100644 --- a/README_zh_CN.md +++ b/README_zh_CN.md @@ -82,12 +82,12 @@ $ go build -ldflags "-s -w" cf.go 用浏览器打开榜单,查看排名。 ```plain -首先你得用 "cf config login"、 "cf config add" 命令来配置一下 +首先你得用 "cf config" 命令来配置一下用户名、密码和代码模板 如果你想用本工具打比赛,那么最好用 "cf race 1111" 命令,其中 "1111" 是比赛的 id 支持的命令: - cf config (login | add | del | default) + cf config cf submit [( )] [] cf list [] cf parse [] [] @@ -103,10 +103,7 @@ $ go build -ldflags "-s -w" cf.go cf upgrade 例子: - cf config login 配置你的用户名和密码。 - cf config add 添加一份模板。 - cf config del 删掉一份模板。 - cf config default 设置默认的模板。 + cf config 配置 cf-tool。 cf submit 如果当前路径是 "/" 那 cf 会找到匹配某个模板的代码, 然后提交到 这场比赛的 题目。 cf submit a.cpp diff --git a/cf.go b/cf.go index 1b3c5702..1c0e4f75 100644 --- a/cf.go +++ b/cf.go @@ -17,12 +17,12 @@ const version = "v0.7.2" func main() { usage := `Codeforces Tool $%version%$ (cf). https://github.com/xalanq/cf-tool -You should run "cf config login" and "cf config add" at first. +You should run "cf config" to configure your username, password and the code template at first. If you want to compete, the best command is "cf race 1111" where "1111" is the contest id. Usage: - cf config (login | add | del | default) + cf config cf submit [( )] [] cf list [] cf parse [] [] @@ -38,10 +38,7 @@ Usage: cf upgrade Examples: - cf config login Config your username and password. - cf config add Add a template. - cf config del Remove a template. - cf config default Set default template. + cf config Configure the cf-tool. cf submit If current path is "/", cf will find the code which can be submitted. Then submit to . cf submit a.cpp diff --git a/cmd/config.go b/cmd/config.go index 6f1f2257..7f900038 100644 --- a/cmd/config.go +++ b/cmd/config.go @@ -1,17 +1,28 @@ package cmd -import "github.com/xalanq/cf-tool/config" +import ( + "github.com/fatih/color" + ansi "github.com/k0kubun/go-ansi" + "github.com/xalanq/cf-tool/config" + "github.com/xalanq/cf-tool/util" +) // Config command func Config(args map[string]interface{}) error { + color.Cyan("Configure the tool") cfg := config.New(config.ConfigPath) - if args["login"].(bool) { + ansi.Println("0) username and password") + ansi.Println("1) add a template") + ansi.Println("2) delete a template") + ansi.Println("3) set default template") + index := util.ChooseIndex(4) + if index == 0 { return cfg.Login(config.SessionPath) - } else if args["add"].(bool) { + } else if index == 1 { return cfg.AddTemplate() - } else if args["del"].(bool) { + } else if index == 2 { return cfg.RemoveTemplate() - } else if args["default"].(bool) { + } else if index == 3 { return cfg.SetDefaultTemplate() } return nil diff --git a/config/login.go b/config/login.go index 733dc776..f2f54247 100644 --- a/config/login.go +++ b/config/login.go @@ -75,6 +75,9 @@ func (c *Config) DecryptPassword() (string, error) { // Login configure username and password func (c *Config) Login(path string) (err error) { + if c.Username != "" { + color.Green("Current user: %v", c.Username) + } color.Cyan("Configure username/email and password") color.Cyan("Note: The password is invisible, just type it correctly.") diff --git a/config/template.go b/config/template.go index 5c45dff5..4deadcbe 100644 --- a/config/template.go +++ b/config/template.go @@ -16,6 +16,7 @@ import ( // AddTemplate add template func (c *Config) AddTemplate() (err error) { + color.Cyan("Add a template") color.Cyan("Language list:") type kv struct { K, V string @@ -28,13 +29,29 @@ func (c *Config) AddTemplate() (err error) { for _, t := range langs { fmt.Printf("%5v: %v\n", t.K, t.V) } - color.Cyan("Select a language (e.g. 42): ") - lang := util.ScanlineTrim() + 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") + } - color.Cyan("Alias (e.g. cpp): ") - alias := util.ScanlineTrim() + ansi.Println(`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: - color.Cyan(`Template absolute path(e.g. ~/template/io.cpp): `) + $%U%$ Username + $%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)`) + color.Cyan(`Template absolute path(e.g. "~/template/io.cpp"): `) path := "" for { path = util.ScanlineTrim() @@ -47,7 +64,8 @@ func (c *Config) AddTemplate() (err error) { color.Red("%v is invalid. Please input again: ", path) } - color.Cyan("(The suffix of template above will be added by default) Other suffix? (e.g. cxx cc): ") + 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{} @@ -59,20 +77,46 @@ func (c *Config) AddTemplate() (err error) { } } - color.Cyan("Before script (e.g. g++ $%full%$ -o $%file%$.exe -std=c++11), empty is ok: ") + 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:") + ansi.Println(`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")`) + + 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): ") + 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.Red("Script can not be empty. Please input again: ") } - color.Cyan("After script (e.g. rm $%file%$.exe): ") + color.Cyan(`After script (e.g. "rm $%file%$.exe"), empty is ok: `) afterScript := util.ScanlineTrim() c.Template = append(c.Template, CodeTemplate{ @@ -83,12 +127,12 @@ func (c *Config) AddTemplate() (err error) { 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 @@ -113,6 +157,7 @@ func (c *Config) RemoveTemplate() (err error) { // 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