Skip to content

Commit 8b174e2

Browse files
committed
Improve demo command output
1 parent 1a4cb6e commit 8b174e2

3 files changed

Lines changed: 54 additions & 2 deletions

File tree

api/problem.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ type Problem struct {
1818
}
1919

2020
func (p *Problem) String() string {
21-
return fmt.Sprintf("%s - %s in %s", p.ID, p.Name, p.Language)
21+
return fmt.Sprintf("%s (%s)", p.Name, p.Language)
2222
}
2323

2424
func (p *Problem) ExistsIn(dir string) bool {

handlers/demo.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ func Demo(ctx *cli.Context) {
2828
fmt.Println(err)
2929
return
3030
}
31-
fmt.Printf("%s (%s) - %s/%s\n", problem.Name, problem.Language, c.Dir, problem.ID)
3231
}
32+
33+
NewHomework(problems, c).Report()
34+
35+
fmt.Println()
36+
fmt.Println("Next step: choose a language, read the README, and make the test suite pass.")
3337
}

handlers/homework.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package handlers
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/exercism/cli/api"
7+
"github.com/exercism/cli/config"
8+
)
9+
10+
type Item struct {
11+
Title string
12+
Path string
13+
}
14+
15+
type Homework struct {
16+
Items []*Item
17+
template string
18+
}
19+
20+
func (hw Homework) MaxTitleWidth() int {
21+
var max int
22+
for _, item := range hw.Items {
23+
if len(item.Title) > max {
24+
max = len(item.Title)
25+
}
26+
}
27+
return max
28+
}
29+
30+
func NewHomework(problems []*api.Problem, c *config.Config) Homework {
31+
hw := Homework{}
32+
for _, problem := range problems {
33+
item := &Item{
34+
Title: problem.String(),
35+
Path: fmt.Sprintf("%s/%s", c.Dir, problem.ID),
36+
}
37+
hw.Items = append(hw.Items, item)
38+
}
39+
40+
hw.template = fmt.Sprintf("%%%ds %%s\n", hw.MaxTitleWidth())
41+
return hw
42+
}
43+
44+
func (hw Homework) Report() {
45+
for _, item := range hw.Items {
46+
fmt.Printf(hw.template, item.Title, item.Path)
47+
}
48+
}

0 commit comments

Comments
 (0)