-
-
Notifications
You must be signed in to change notification settings - Fork 362
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
166 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package api | ||
|
||
import "fmt" | ||
|
||
// Track is a collection of problems in a given language. | ||
type Track struct { | ||
ID string `json:"id"` | ||
Language string `json:"language"` | ||
Active bool `json:"active"` | ||
Problems []string `json:"problems"` | ||
} | ||
|
||
// Len lists the number of problems a track has. | ||
func (t *Track) Len() int { | ||
return len(t.Problems) | ||
} | ||
|
||
func (t *Track) String() string { | ||
return fmt.Sprintf("%s (%s)", t.Language, t.ID) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
|
||
"github.com/codegangsta/cli" | ||
"github.com/exercism/cli/api" | ||
"github.com/exercism/cli/config" | ||
"github.com/exercism/cli/user" | ||
) | ||
|
||
// Tracks lists available tracks. | ||
func Tracks(ctx *cli.Context) { | ||
c, err := config.Read(ctx.GlobalString("config")) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
tracks, err := api.Tracks(fmt.Sprintf("%s/tracks", c.XAPI)) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
curr := user.NewCurriculum(tracks) | ||
fmt.Println("\nActive language tracks:") | ||
curr.Report(user.TrackActive) | ||
fmt.Println("\nInactive language tracks:") | ||
curr.Report(user.TrackInactive) | ||
|
||
// TODO: implement `list` command to list problems in a track | ||
msg := ` | ||
Related commands: | ||
exercism fetch (see 'exercism help fetch') | ||
` | ||
fmt.Println(msg) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package user | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/exercism/cli/api" | ||
) | ||
|
||
// Status is the status of a track (active/inactive). | ||
type Status bool | ||
|
||
const ( | ||
// TrackActive represents an active track. | ||
// Problems from active tracks will be delivered with the `fetch` command. | ||
TrackActive Status = true | ||
// TrackInactive represents an inactive track. | ||
// It is possible to fetch problems from an inactive track, and | ||
// submit them to the website, but these will not automatically be | ||
// delivered in the global `fetch` command. | ||
TrackInactive Status = false | ||
) | ||
|
||
// Curriculum is a collection of language tracks. | ||
type Curriculum struct { | ||
Tracks []*api.Track | ||
wLang int | ||
wID int | ||
} | ||
|
||
// NewCurriculum returns a collection of language tracks. | ||
func NewCurriculum(tracks []*api.Track) *Curriculum { | ||
return &Curriculum{Tracks: tracks} | ||
} | ||
|
||
// Report creates a table of the tracks that have the requested status. | ||
func (cur *Curriculum) Report(status Status) { | ||
for _, track := range cur.Tracks { | ||
if Status(track.Active) == status { | ||
fmt.Println( | ||
" ", | ||
track.Language, | ||
strings.Repeat(" ", cur.lenLang()-len(track.Language)+1), | ||
track.ID, | ||
strings.Repeat(" ", cur.lenID()-len(track.ID)+1), | ||
track.Len(), | ||
"problems", | ||
) | ||
} | ||
} | ||
} | ||
|
||
func (cur *Curriculum) lenLang() int { | ||
if cur.wLang > 0 { | ||
return cur.wLang | ||
} | ||
|
||
for _, track := range cur.Tracks { | ||
if len(track.Language) > cur.wLang { | ||
cur.wLang = len(track.Language) | ||
} | ||
} | ||
return cur.wLang | ||
} | ||
|
||
func (cur *Curriculum) lenID() int { | ||
if cur.wID > 0 { | ||
return cur.wID | ||
} | ||
|
||
for _, track := range cur.Tracks { | ||
if len(track.ID) > cur.wID { | ||
cur.wID = len(track.ID) | ||
} | ||
} | ||
return cur.wID | ||
} |