Add automatic default selection to challenge/implementation selection

This is triggered in the event there is only 1 challenge/implementation
This commit is contained in:
akp 2021-10-22 13:00:09 +01:00
parent 5fb11171d3
commit 8e6135fc50
No known key found for this signature in database
GPG key ID: AA5726202C8879B7
2 changed files with 92 additions and 55 deletions

View file

@ -2,7 +2,6 @@ package main
import (
"fmt"
"github.com/AlecAivazis/survey/v2"
"github.com/codemicro/adventOfCode/runtime/challenge"
"github.com/codemicro/adventOfCode/runtime/runners"
"io/ioutil"
@ -14,74 +13,22 @@ const (
challengeInfoFile = "info.json"
)
func userSelect(question string, choices []string) (int, error) {
var o string
prompt := &survey.Select{
Message: question,
Options: choices,
}
err := survey.AskOne(prompt, &o)
if err != nil {
return 0, err
}
for i, x := range choices {
if x == o {
return i, nil
}
}
return -1, nil
}
func run() error {
// List and select challenges
challenges, err := challenge.ListingFromDir(challengeDir)
selectedChallenge, err := selectChallenge(challengeDir)
if err != nil {
return err
}
var selectedChallengeIndex int
{
var opts []string
for _, c := range challenges {
opts = append(opts, c.String())
}
chosen, err := userSelect("Which challenge do you want to run?", opts)
if err != nil {
return err
}
selectedChallengeIndex = chosen
}
selectedChallenge := challenges[selectedChallengeIndex]
// List and select implementations
implementations, err := selectedChallenge.GetImplementations()
selectedImplementation, err := selectImplementation(selectedChallenge)
if err != nil {
return err
}
var selectedImplementationIndex int
{
var opts []string
for _, i := range implementations {
opts = append(opts, runners.RunnerNames[i])
}
chosen, err := userSelect("Which implementation do you want to use?", opts)
if err != nil {
return err
}
selectedImplementationIndex = chosen
}
selectedImplementation := implementations[selectedImplementationIndex]
// Load info.json file
challengeInfo, err := challenge.LoadChallengeInfo(filepath.Join(selectedChallenge.Dir, challengeInfoFile))
if err != nil {

90
runtime/selections.go Normal file
View file

@ -0,0 +1,90 @@
package main
import (
"errors"
"fmt"
"github.com/AlecAivazis/survey/v2"
"github.com/codemicro/adventOfCode/runtime/challenge"
"github.com/codemicro/adventOfCode/runtime/runners"
)
func userSelect(question string, choices []string) (int, error) {
var o string
prompt := &survey.Select{
Message: question,
Options: choices,
}
err := survey.AskOne(prompt, &o)
if err != nil {
return 0, err
}
for i, x := range choices {
if x == o {
return i, nil
}
}
return -1, nil
}
func selectChallenge(dir string) (*challenge.Challenge, error) {
challenges, err := challenge.ListingFromDir(dir)
if err != nil {
return nil, err
}
var selectedChallengeIndex int
switch len(challenges) {
case 0:
return nil, errors.New("no challenges to run")
case 1:
selectedChallengeIndex = 0
c := challenges[0]
fmt.Printf("Automatically selecting day %d (%s)\n", c.Number, c.Name)
default:
var opts []string
for _, c := range challenges {
opts = append(opts, c.String())
}
selectedChallengeIndex, err = userSelect("Which challenge do you want to run?", opts)
if err != nil {
return nil, err
}
}
return challenges[selectedChallengeIndex], nil
}
func selectImplementation(ch *challenge.Challenge) (string, error) {
implementations, err := ch.GetImplementations()
if err != nil {
return "", err
}
var selectedImplementationIndex int
switch len(implementations) {
case 0:
return "", errors.New("no implementations to use")
case 1:
selectedImplementationIndex = 0
fmt.Printf("Automatically selecting implementation %s", runners.RunnerNames[implementations[0]])
default:
var opts []string
for _, i := range implementations {
opts = append(opts, runners.RunnerNames[i])
}
selectedImplementationIndex, err = userSelect("Which implementation do you want to use?", opts)
if err != nil {
return "", err
}
}
return implementations[selectedImplementationIndex], nil
}