Skip to content

Commit

Permalink
Change config command. Make it easy to use
Browse files Browse the repository at this point in the history
  • Loading branch information
xalanq committed Aug 14, 2019
1 parent 384a769 commit ecbaa42
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 34 deletions.
9 changes: 3 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 [(<contest-id> <problem-id>)] [<filename>]
cf list [<contest-id>]
cf parse [<contest-id>] [<problem-id>]
Expand All @@ -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 "<contest-id>/<problem-id>", cf will find the
code which can be submitted. Then submit to <contest-id> <problem-id>.
cf submit a.cpp
Expand Down
9 changes: 3 additions & 6 deletions README_zh_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 [(<contest-id> <problem-id>)] [<filename>]
cf list [<contest-id>]
cf parse [<contest-id>] [<problem-id>]
Expand All @@ -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 如果当前路径是 "<contest-id>/<problem-id>" 那 cf 会找到匹配某个模板的代码,
然后提交到 <contest-id> 这场比赛的 <problem-id> 题目。
cf submit a.cpp
Expand Down
9 changes: 3 additions & 6 deletions cf.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 [(<contest-id> <problem-id>)] [<filename>]
cf list [<contest-id>]
cf parse [<contest-id>] [<problem-id>]
Expand All @@ -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 "<contest-id>/<problem-id>", cf will find the
code which can be submitted. Then submit to <contest-id> <problem-id>.
cf submit a.cpp
Expand Down
21 changes: 16 additions & 5 deletions cmd/config.go
Original file line number Diff line number Diff line change
@@ -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
Expand Down
3 changes: 3 additions & 0 deletions config/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.")

Expand Down
67 changes: 56 additions & 11 deletions config/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()
Expand All @@ -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{}
Expand All @@ -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{
Expand All @@ -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
Expand All @@ -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
Expand Down

0 comments on commit ecbaa42

Please sign in to comment.