|
| 1 | +package user |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "strings" |
| 6 | + |
| 7 | + "github.com/exercism/cli/api" |
| 8 | +) |
| 9 | + |
| 10 | +// Status is the status of a track (active/inactive). |
| 11 | +type Status bool |
| 12 | + |
| 13 | +const ( |
| 14 | + // TrackActive represents an active track. |
| 15 | + // Problems from active tracks will be delivered with the `fetch` command. |
| 16 | + TrackActive Status = true |
| 17 | + // TrackInactive represents an inactive track. |
| 18 | + // It is possible to fetch problems from an inactive track, and |
| 19 | + // submit them to the website, but these will not automatically be |
| 20 | + // delivered in the global `fetch` command. |
| 21 | + TrackInactive Status = false |
| 22 | +) |
| 23 | + |
| 24 | +// Curriculum is a collection of language tracks. |
| 25 | +type Curriculum struct { |
| 26 | + Tracks []*api.Track |
| 27 | + wLang int |
| 28 | + wID int |
| 29 | +} |
| 30 | + |
| 31 | +// NewCurriculum returns a collection of language tracks. |
| 32 | +func NewCurriculum(tracks []*api.Track) *Curriculum { |
| 33 | + return &Curriculum{Tracks: tracks} |
| 34 | +} |
| 35 | + |
| 36 | +// Report creates a table of the tracks that have the requested status. |
| 37 | +func (cur *Curriculum) Report(status Status) { |
| 38 | + for _, track := range cur.Tracks { |
| 39 | + if Status(track.Active) == status { |
| 40 | + fmt.Println( |
| 41 | + " ", |
| 42 | + track.Language, |
| 43 | + strings.Repeat(" ", cur.lenLang()-len(track.Language)+1), |
| 44 | + track.ID, |
| 45 | + strings.Repeat(" ", cur.lenID()-len(track.ID)+1), |
| 46 | + track.Len(), |
| 47 | + "problems", |
| 48 | + ) |
| 49 | + } |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +func (cur *Curriculum) lenLang() int { |
| 54 | + if cur.wLang > 0 { |
| 55 | + return cur.wLang |
| 56 | + } |
| 57 | + |
| 58 | + for _, track := range cur.Tracks { |
| 59 | + if len(track.Language) > cur.wLang { |
| 60 | + cur.wLang = len(track.Language) |
| 61 | + } |
| 62 | + } |
| 63 | + return cur.wLang |
| 64 | +} |
| 65 | + |
| 66 | +func (cur *Curriculum) lenID() int { |
| 67 | + if cur.wID > 0 { |
| 68 | + return cur.wID |
| 69 | + } |
| 70 | + |
| 71 | + for _, track := range cur.Tracks { |
| 72 | + if len(track.ID) > cur.wID { |
| 73 | + cur.wID = len(track.ID) |
| 74 | + } |
| 75 | + } |
| 76 | + return cur.wID |
| 77 | +} |
0 commit comments