Repository navigation
Expand file tree
/
Copy pathhomework.go
More file actions
136 lines (117 loc) · 3.04 KB
/
Copy pathhomework.go
File metadata and controls
136 lines (117 loc) · 3.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package user
import (
"fmt"
"github.com/exercism/cli/api"
"github.com/exercism/cli/config"
)
// HWFilter is used to categorize homework items.
type HWFilter int
// SummaryOption is an alias of HWFilter that allows
// selective display of summary items
type SummaryOption HWFilter
const (
// HWAll represents all items in the collection.
HWAll = iota
// HWUpdated represents problems that were already on the
// user's filesystem, where one or more new files have been added.
HWUpdated
// HWNew represents problems that did not yet exist on the
// user's filesystem.
HWNew
// HWNotSubmitted represents problems that have not been submitted
// for review.
HWNotSubmitted
)
// Homework is a collection of problems that were fetched from the APIs.
type Homework struct {
Items []*Item
template string
}
// NewHomework decorates a problem set with some additional data based on the
// user's system.
func NewHomework(problems []*api.Problem, c *config.Config) *Homework {
hw := Homework{}
for _, problem := range problems {
item := &Item{
Problem: problem,
dir: c.Dir,
}
hw.Items = append(hw.Items, item)
}
hw.template = fmt.Sprintf("%%%ds %%s\n", hw.maxTitleWidth())
return &hw
}
// Save saves all problems in the problem set.
func (hw *Homework) Save() error {
for _, item := range hw.Items {
if err := item.Save(); err != nil {
return err
}
}
return nil
}
// ItemsMatching returns a subset of the set of problems.
func (hw *Homework) ItemsMatching(filter HWFilter) []*Item {
items := []*Item{}
for _, item := range hw.Items {
if item.Matches(filter) {
items = append(items, item)
}
}
return items
}
// Report outputs a list of the problems in the set.
// It prints the track name, the problem name, and the full
// path to the problem on the user's filesystem.
func (hw *Homework) Report(filter HWFilter) {
items := hw.ItemsMatching(filter)
hw.heading(filter, len(items))
for _, item := range items {
fmt.Printf(hw.template, item.String(), item.Path())
}
}
func (hw *Homework) heading(filter HWFilter, count int) {
if count == 0 {
return
}
fmt.Println()
if filter == HWAll {
return
}
unit := "problems"
if count == 1 {
unit = "problem"
}
var status string
switch filter {
case HWUpdated:
status = "Updated:"
case HWNew:
status = "New:"
case HWNotSubmitted:
status = "Not Submitted:"
}
summary := fmt.Sprintf("%d %s", count, unit)
fmt.Printf(hw.template, status, summary)
}
func (hw *Homework) maxTitleWidth() int {
var max int
for _, item := range hw.Items {
if len(item.String()) > max {
max = len(item.String())
}
}
return max
}
// Summarize prints a full report of new and updated items in the set.
func (hw *Homework) Summarize(summaryFilter SummaryOption) {
hw.Report(HWUpdated)
hw.Report(HWNew)
if summaryFilter != HWNotSubmitted {
hw.Report(HWNotSubmitted)
}
fresh := len(hw.ItemsMatching(HWNew))
updated := len(hw.ItemsMatching(HWUpdated))
unchanged := len(hw.Items) - updated - fresh
fmt.Printf("\nunchanged: %d, updated: %d, new: %d\n\n", unchanged, updated, fresh)
}