Skip to content

Commit

Permalink
Refactor(): Split years and puzzles selection into separate menu laye…
Browse files Browse the repository at this point in the history
…rs (#12)

* 2019/day05: Add puzzle spec and stub for solution

* input/2019: Add day05 input

* puzzles/solutions: Register day05 puzzle

* update gitignore

* Update dependencies; Fix markdown linters

* chore(): upgrade go version to 1.14

* chore(): gomnd linter config

* chore(): Linters fix

* upgrade vendor

* chore(): Update vendor

* chore(): Upgrade go version and dependencies

* chore(): Update tools and add go version to Readme

* fix(): Fix after merge

* refactor(): Split menu to separate layers years selection and puzzles.

* fix(): Remove day05 solution.
  • Loading branch information
obalunenko authored Nov 6, 2020
1 parent b5ebb8e commit a5b83f3
Show file tree
Hide file tree
Showing 34 changed files with 523 additions and 215 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

# Output of the go coverage tool, specifically when used with LiteIDE
*.out*
.vscode/

.idea

Expand Down
17 changes: 9 additions & 8 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ linters-settings:
simple: true
range-loops: true # Report preallocation suggestions on range loops, true by default
for-loops: false # Report preallocation suggestions on for loops, false by default
gomnd:
settings:
mnd:
# the list of enabled checks, see https://github.com/tommy-muehle/go-mnd/#checks for description.
checks: case,condition,operation,return
gocritic:
enabled-checks:
- docStub
Expand Down Expand Up @@ -78,24 +83,20 @@ linters-settings:
underef:
skipRecvDeref: true


run:


skip-dirs:
- vendor/

output:
format: colored-line-number

issues:
# Excluding configuration per-path, per-linter, per-text and per-source
# Excluding configuration per-path, per-linter, per-text and per-source
exclude-rules:
# Exclude some linters from running on tests files.
- path: _test\.go
linters:
# Exclude some linters from running on tests files.
- path: _test\.go
linters:
- gocyclo
- funlen
- dupl
- gosec

1 change: 0 additions & 1 deletion .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ builds:
- linux
goarch:
- amd64
- 386
- arm
env:
- CGO_ENABLED=0
Expand Down
6 changes: 4 additions & 2 deletions .tools/README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
This package is used to track CI tool dependencies.
# Tools

* [Why does installing a tool via go get fail with error cannot find main module](https://github.com/golang/go/wiki/Modules#why-does-installing-a-tool-via-go-get-fail-with-error-cannot-find-main-module)
This package used to track CI tool dependencies.

- [Why does installing a tool via go get fail with error cannot find main module](https://github.com/golang/go/wiki/Modules#why-does-installing-a-tool-via-go-get-fail-with-error-cannot-find-main-module)
2 changes: 1 addition & 1 deletion .tools/go.mod
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module linters
module tools

go 1.15

Expand Down
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ jobs:
script:
- echo "Build stage"
- make compile
after_success:
after_success: true
deploy:
skip_cleanup: true
provider: script
Expand All @@ -44,4 +44,4 @@ jobs:
tags: true
condition: "$TRAVIS_OS_NAME = linux"
addons:
ssh_known_hosts: github.com
ssh_known_hosts: github.com
38 changes: 14 additions & 24 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,59 +1,44 @@
NAME=aoc-cli
BIN_DIR=./bin

# COLORS
GREEN := $(shell tput -Txterm setaf 2)
YELLOW := $(shell tput -Txterm setaf 3)
WHITE := $(shell tput -Txterm setaf 7)
RESET := $(shell tput -Txterm sgr0)


TARGET_MAX_CHAR_NUM=20


define colored
@echo '${GREEN}$1${RESET}'
endef

## Show help
help:
${call colored, help is running...}
@echo ''
@echo 'Usage:'
@echo ' ${YELLOW}make${RESET} ${GREEN}<target>${RESET}'
@echo ' make <target>'
@echo ''
@echo 'Targets:'
@awk '/^[a-zA-Z\-\_0-9]+:/ { \
helpMessage = match(lastLine, /^## (.*)/); \
if (helpMessage) { \
helpCommand = substr($$1, 0, index($$1, ":")-1); \
helpMessage = substr(lastLine, RSTART + 3, RLENGTH); \
printf " ${YELLOW}%-$(TARGET_MAX_CHAR_NUM)s${RESET} ${GREEN}%s${RESET}\n", helpCommand, helpMessage; \
printf " %-$(TARGET_MAX_CHAR_NUM)s %s\n", helpCommand, helpMessage; \
} \
} \
{ lastLine = $$0 }' $(MAKEFILE_LIST)
.PHONY: help

## app compile
compile:
${call colored, compile is running...}
./scripts/compile.sh
.PHONY: compile

## Cross os compile
cross-compile:
${call colored, compile is running...}
./scripts/cross-compile.sh
.PHONY: cross-compile

## lint project
lint:
${call colored, lint is running...}
./scripts/run-linters.sh
.PHONY: lint

## Lint in CI
lint-ci:
${call colored, lint_ci is running...}
./scripts/run-linters-ci.sh
.PHONY: lint-ci

Expand All @@ -64,37 +49,31 @@ pretty-markdown:

## Test all packages
test:
${call colored, test is running...}
./scripts/run-tests.sh
.PHONY: test

## Test coverage
test-cover:
${call colored, test-cover is running...}
./scripts/coverage.sh
.PHONY: test-cover

## Increase version number
new-version: lint test compile
${call colored, new version is running...}
./scripts/version.sh
.PHONY: new-version

## Release
release:
${call colored, release is running...}
./scripts/release.sh
.PHONY: release

## Fix imports sorting
imports:
${call colored, sort and group imports...}
./scripts/fix-imports.sh
.PHONY: imports

## fetch all dependencies for scripts
dependencies:
${call colored, dependensies is running...}
./scripts/get-dependencies.sh
.PHONY: dependencies

Expand All @@ -121,3 +100,14 @@ generate:
.PHONY: generate

.DEFAULT_GOAL := test

## vendor-check if dependencies were not changed.
vendor-check:
./scripts/check-vendor.sh
.PHONY: vendor-check

vet:
./scripts/vet.sh
.PHONY: vet

.DEFAULT_GOAL := test
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
# advent-of-code

![GitHub go.mod Go version](https://img.shields.io/github/go-mod/go-version/oleg-balunenko/advent-of-code)
[![Build Status](https://travis-ci.com/oleg-balunenko/advent-of-code.svg?branch=master)](https://travis-ci.com/oleg-balunenko/advent-of-code)
[![Go Report Card](https://goreportcard.com/badge/github.com/oleg-balunenko/advent-of-code)](https://goreportcard.com/report/github.com/oleg-balunenko/advent-of-code)
[![codecov](https://codecov.io/gh/oleg-balunenko/advent-of-code/branch/master/graph/badge.svg)](https://codecov.io/gh/oleg-balunenko/advent-of-code)
[![GoDoc](https://godoc.org/github.com/oleg-balunenko/advent-of-code?status.svg)](https://godoc.org/github.com/oleg-balunenko/advent-of-code)

# advent-of-code
My solutions of puzzles for [Advent Of Code](https://adventofcode.com/)

This repository contains solutions for puzzles and cli tool to run solutions to get answers for input on site.
Expand Down
84 changes: 74 additions & 10 deletions cmd/aoc-cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (

const (
exit = "exit"
back = "back"
)

var (
Expand All @@ -42,6 +43,53 @@ func main() {
}
}

func menu() error {
years := puzzles.GetYears()

prompt := promptui.Select{
Label: nil,
Items: append(years, exit),
Size: 0,
CursorPos: 0,
IsVimMode: false,
HideHelp: false,
HideSelected: false,
Templates: nil,
Keys: nil,
Searcher: nil,
StartInSearchMode: false,
Pointer: nil,
Stdin: nil,
Stdout: nil,
}

return handleYearChoices(prompt)
}

func handleYearChoices(opt promptui.Select) error {
for {
_, choice, err := opt.Run()
if err != nil {
return errors.Wrap(err, "prompt failed")
}

if isExit(choice) {
return nil
}

err = menuPuzzle(choice)
if errors.Is(err, errExit) {
return nil
}

if err != nil {
log.Error(err)

continue
}
}
}

func setLogger() {
l, err := log.ParseLevel(*logLevel)
if err != nil {
Expand Down Expand Up @@ -70,14 +118,14 @@ func setLogger() {
log.SetFormatter(formatter)
}

func menu() error {
solvers := puzzles.Solvers()
func menuPuzzle(year string) error {
solvers := puzzles.NamesByYear(year)

pageSize := 20

prompt := promptui.Select{
Label: "Puzzles menu (input 'exit' for exit)",
Items: append(solvers, exit),
Label: "Puzzles menu (exit' for exit; back - to return to year selection)",
Items: append(solvers, back, exit),
Size: pageSize,
CursorPos: 0,
IsVimMode: false,
Expand All @@ -92,28 +140,33 @@ func menu() error {
Stdout: nil,
}

return handleChoices(prompt)
return handlePuzzleChoices(year, prompt)
}

func handleChoices(opt promptui.Select) error {
func handlePuzzleChoices(year string, opt promptui.Select) error {
for {
_, choice, err := opt.Run()
if err != nil {
return errors.Wrap(err, "prompt failed")
}

if isExit(choice) {
return errExit
}

if isBack(choice) {
return nil
}

res, err := run(choice)
res, err := run(year, choice)
if err != nil {
log.Error(err)

continue
}

log.WithFields(log.Fields{
"year": res.Year,
"name": res.Name,
"part1": res.Part1,
"part2": res.Part2,
Expand All @@ -125,14 +178,25 @@ func isExit(input string) bool {
return strings.EqualFold(exit, input)
}

func run(puzzle string) (puzzles.Result, error) {
s, err := puzzles.GetSolver(puzzle)
func isBack(input string) bool {
return strings.EqualFold(back, input)
}

var errExit = errors.New("exit is chosen")

func run(year string, name string) (puzzles.Result, error) {
s, err := puzzles.GetSolver(year, name)
if err != nil {
return puzzles.Result{}, errors.Wrap(err, "failed to get solver")
}

fullName, err := puzzles.MakeName(s.Year(), s.Name())
if err != nil {
return puzzles.Result{}, fmt.Errorf("failed to make full name: %w", err)
}

asset, err := input.Asset(filepath.Clean(
filepath.Join(input.InputDir, fmt.Sprintf("%s.txt", s.Name()))))
filepath.Join(input.InputDir, fmt.Sprintf("%s.txt", fullName))))
if err != nil {
return puzzles.Result{}, errors.Wrap(err, "failed to open input data")
}
Expand Down
4 changes: 4 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,15 @@ module github.com/oleg-balunenko/advent-of-code
go 1.15

require (
github.com/gopherjs/gopherjs v0.0.0-20200217142428-fce0ec30dd00 // indirect
github.com/lunixbochs/vtclean v1.0.0 // indirect
github.com/manifoldco/promptui v0.8.0
github.com/mattn/go-colorable v0.1.8 // indirect
github.com/pkg/errors v0.9.1
github.com/sirupsen/logrus v1.7.0
github.com/smartystreets/assertions v1.2.0 // indirect
github.com/smartystreets/goconvey v1.6.4 // indirect
github.com/stretchr/testify v1.6.1
golang.org/x/sys v0.0.0-20201101102859-da207088b7d1 // indirect
golang.org/x/tools v0.0.0-20201105220310-78b158585360 // indirect
)
Loading

0 comments on commit a5b83f3

Please sign in to comment.