pelican/internal/ui/ui.go
Dan Anglin 3e5cd598d0
refactor: project restructure
- Introduced internal packages for different components of the kanban
  board.
- Simplified and refactored the database tests based on linting feedback.
- Add tests for the board package.
2021-09-18 01:03:09 +01:00

136 lines
2.7 KiB
Go

// Right now this will be a very simple, scuffed interface.
package ui
import (
"fmt"
"forge.dananglin.me.uk/code/dananglin/pelican/internal/board"
"github.com/eiannone/keyboard"
bolt "go.etcd.io/bbolt"
)
func App() error {
var db *bolt.DB
var err error
keysEvents, err := keyboard.GetKeys(10)
if err != nil {
return fmt.Errorf("unable to create the keysEvent channel, %w", err)
}
defer func() {
_ = keyboard.Close()
}()
fmt.Println(usage())
app:
for {
event := <-keysEvents
if event.Err != nil {
return fmt.Errorf("keys event error: %w", event.Err)
}
switch event.Rune {
case 'q':
break app
case 'r':
if err = refresh(db); err != nil {
fmt.Printf("Error: Unable to refresh board, %s\n", err)
}
case 'a':
if err = newCard(db); err != nil {
fmt.Printf("Error: Unable to add a card, %s\n", err)
}
case 'v':
if err = viewCard(db, 1); err != nil {
fmt.Printf("Error: Unable to view card, %s\n", err)
}
case 'o':
// TODO: How do we close the db?
db, err = openProject("")
if err != nil {
fmt.Printf("Error: Unable to open the project, %s\n", err)
}
default:
fmt.Println("Error: Unknown key event.")
}
}
db.Close()
return nil
}
func refresh(db *bolt.DB) error {
statusList, err := board.ReadStatusList(db)
if err != nil {
return fmt.Errorf("unable to get the status list, %w", err)
}
fmt.Printf("--------------------\n")
for _, s := range statusList {
fmt.Printf("Status ID: %d\nStatus Name: \"%s\"\nCard IDs: %v\n\n", s.ID, s.Name, s.CardIds)
}
fmt.Printf("--------------------\n\n\n")
return nil
}
func newCard(db *bolt.DB) error {
title := "A card title"
content := "As a user, this is a ticket for me.\nAs a user, I want to close it."
if err := board.CreateCard(db, title, content); err != nil {
return fmt.Errorf("unable to create card, %w", err)
}
fmt.Println("Sample card created successfully.")
return nil
}
func viewCard(db *bolt.DB, id int) error {
card, err := board.ReadCard(db, id)
if err != nil {
return fmt.Errorf("unable to read card, %w", err)
}
fmt.Printf("====================\n")
fmt.Printf("[%d] %s\n", card.ID, card.Title)
fmt.Printf("--------------------\n")
fmt.Println(card.Content)
fmt.Printf("====================\n")
return nil
}
func openProject(path string) (*bolt.DB, error) {
db, err := board.LoadBoard(path)
if err != nil {
return nil, fmt.Errorf("unable to load board, %w", err)
}
if err = refresh(db); err != nil {
return nil, err
}
return db, nil
}
func usage() string {
usage := `
Press 'o' to open the project
Press 'r' to refresh the board
Press 'a' to add a sample card
Press 'v' to view the first sample card
Press 'q' to quit
`
return usage
}