Skip to content
This repository has been archived by the owner on Dec 14, 2024. It is now read-only.

Commit

Permalink
Add <problem-id> parameter in watch command. See #27
Browse files Browse the repository at this point in the history
  • Loading branch information
xalanq committed Aug 14, 2019
1 parent 5f104b5 commit 0cd73c6
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 10 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ Usage:
cf parse [<contest-id>] [<problem-id>]
cf gen [<alias>]
cf test [<filename>]
cf watch [all] [<contest-id>]
cf watch [all] [<contest-id>] [<problem-id>]
cf open [<contest-id>] [<problem-id>]
cf stand [<contest-id>]
cf sid [<submission-id>] [<contest-id>]
Expand Down
2 changes: 1 addition & 1 deletion README_zh_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ $ go build -ldflags "-s -w" cf.go
cf parse [<contest-id>] [<problem-id>]
cf gen [<alias>]
cf test [<filename>]
cf watch [all] [<contest-id>]
cf watch [all] [<contest-id>] [<problem-id>]
cf open [<contest-id>] [<problem-id>]
cf stand [<contest-id>]
cf sid [<submission-id>] [<contest-id>]
Expand Down
2 changes: 1 addition & 1 deletion cf.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ Usage:
cf parse [<contest-id>] [<problem-id>]
cf gen [<alias>]
cf test [<filename>]
cf watch [all] [<contest-id>]
cf watch [all] [<contest-id>] [<problem-id>]
cf open [<contest-id>] [<problem-id>]
cf stand [<contest-id>]
cf sid [<submission-id>] [<contest-id>]
Expand Down
2 changes: 1 addition & 1 deletion client/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,6 @@ func (c *Client) PullContest(contestID, problemID, rootPath string, ac bool) (er

color.Cyan("These submissions' codes have been saved.")
maxline := 0
display(used, true, &maxline, false)
display(used, "", true, &maxline, false)
return nil
}
2 changes: 1 addition & 1 deletion client/submit.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func (c *Client) SubmitContest(contestID, problemID, langID, source string) (err
}
color.Green("Submitted")

submissions, err := c.WatchSubmission(contestID, 1, true)
submissions, err := c.WatchSubmission(contestID, "", 1, true)
if err != nil {
return
}
Expand Down
18 changes: 15 additions & 3 deletions client/watch.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,15 @@ func (s *Submission) ParseTime() string {
return fmt.Sprintf("%v ms", s.time)
}

// ParsePorblemIndex get problem's index
func (s *Submission) ParseProblemIndex() string {
p := strings.Index(s.name, " ")
if p == -1 {
return ""
}
return strings.ToLower(s.name[:p])
}

func refreshLine(n int, maxWidth int) {
for i := 0; i < n; i++ {
ansi.Printf("%v\n", strings.Repeat(" ", maxWidth))
Expand All @@ -97,7 +106,7 @@ func (s *Submission) display(first bool, maxWidth *int) {
ansi.Printf(" memory: %v\n", s.ParseMemory())
}

func display(submissions []Submission, first bool, maxWidth *int, line bool) {
func display(submissions []Submission, problemID string, first bool, maxWidth *int, line bool) {
if line {
submissions[0].display(first, maxWidth)
return
Expand All @@ -111,6 +120,9 @@ func display(submissions []Submission, first bool, maxWidth *int, line bool) {
table.SetCenterSeparator("|")
table.SetAutoWrapText(false)
for _, sub := range submissions {
if problemID != "" && sub.ParseProblemIndex() != problemID {
continue
}
table.Append([]string{
sub.ParseID(),
sub.when,
Expand Down Expand Up @@ -268,7 +280,7 @@ func (c *Client) getSubmissions(myURL string, n int) (submissions []Submission,
}

// WatchSubmission n is the number of submissions
func (c *Client) WatchSubmission(contestID string, n int, line bool) (submissions []Submission, err error) {
func (c *Client) WatchSubmission(contestID, problemID string, n int, line bool) (submissions []Submission, err error) {
URL := ToGym(fmt.Sprintf("https://codeforces.com/contest/%v/my", contestID), contestID)
maxWidth := 0
first := true
Expand All @@ -278,7 +290,7 @@ func (c *Client) WatchSubmission(contestID string, n int, line bool) (submission
if err != nil {
return
}
display(submissions, first, &maxWidth, line)
display(submissions, problemID, first, &maxWidth, line)
first = false
endCount := 0
for _, submission := range submissions {
Expand Down
8 changes: 6 additions & 2 deletions cmd/watch.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package cmd

import (
"strings"

"github.com/xalanq/cf-tool/client"
"github.com/xalanq/cf-tool/config"
)
Expand All @@ -11,16 +13,18 @@ func Watch(args map[string]interface{}) error {
if err != nil {
return err
}
problemID, _ := args["<problem-id>"].(string)
problemID = strings.ToLower(problemID)
cfg := config.New(config.ConfigPath)
cln := client.New(config.SessionPath)
n := 10
if args["all"].(bool) {
n = -1
}
_, err = cln.WatchSubmission(contestID, n, false)
_, err = cln.WatchSubmission(contestID, problemID, n, false)
if err != nil {
if err = loginAgain(cfg, cln, err); err == nil {
_, err = cln.WatchSubmission(contestID, n, false)
_, err = cln.WatchSubmission(contestID, problemID, n, false)
}
}
if err != nil {
Expand Down

0 comments on commit 0cd73c6

Please sign in to comment.