Skip to content

Commit

Permalink
Improve demo command output
Browse files Browse the repository at this point in the history
  • Loading branch information
kytrinyx committed Sep 5, 2014
1 parent 1a4cb6e commit 8b174e2
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 2 deletions.
2 changes: 1 addition & 1 deletion api/problem.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ type Problem struct {
}

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

func (p *Problem) ExistsIn(dir string) bool {
Expand Down
6 changes: 5 additions & 1 deletion handlers/demo.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ func Demo(ctx *cli.Context) {
fmt.Println(err)
return
}
fmt.Printf("%s (%s) - %s/%s\n", problem.Name, problem.Language, c.Dir, problem.ID)
}

NewHomework(problems, c).Report()

fmt.Println()
fmt.Println("Next step: choose a language, read the README, and make the test suite pass.")
}
48 changes: 48 additions & 0 deletions handlers/homework.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package handlers

import (
"fmt"

"github.com/exercism/cli/api"
"github.com/exercism/cli/config"
)

type Item struct {
Title string
Path string
}

type Homework struct {
Items []*Item
template string
}

func (hw Homework) MaxTitleWidth() int {
var max int
for _, item := range hw.Items {
if len(item.Title) > max {
max = len(item.Title)
}
}
return max
}

func NewHomework(problems []*api.Problem, c *config.Config) Homework {
hw := Homework{}
for _, problem := range problems {
item := &Item{
Title: problem.String(),
Path: fmt.Sprintf("%s/%s", c.Dir, problem.ID),
}
hw.Items = append(hw.Items, item)
}

hw.template = fmt.Sprintf("%%%ds %%s\n", hw.MaxTitleWidth())
return hw
}

func (hw Homework) Report() {
for _, item := range hw.Items {
fmt.Printf(hw.template, item.Title, item.Path)
}
}

0 comments on commit 8b174e2

Please sign in to comment.