pelican/internal/ui/ui.go

227 lines
4.5 KiB
Go
Raw Normal View History

package ui
import (
"fmt"
"strconv"
2023-02-14 07:54:10 +00:00
"codeflow.dananglin.me.uk/apollo/canal/internal/board"
2023-04-22 19:18:47 +01:00
"github.com/gdamore/tcell/v2"
2023-04-23 00:17:34 +01:00
"github.com/rivo/tview"
)
const (
2023-04-22 19:18:47 +01:00
shiftLeft int = iota
shiftRight
)
const (
2023-04-22 19:18:47 +01:00
mainPageName string = "main"
quitPageName string = "quit"
addPageName string = "add"
movePageName string = "move"
)
2023-04-22 17:50:27 +01:00
type UI struct {
*tview.Application
columns []column
flex *tview.Flex
pages *tview.Pages
focusedColumn int
board board.Board
2023-04-22 19:18:47 +01:00
quit *tview.Modal
2023-04-23 00:17:34 +01:00
add *modalInput
move *tview.Flex
2023-04-22 19:18:47 +01:00
}
// NewUI returns a new UI value.
func NewUI() UI {
ui := UI{
2023-04-22 19:18:47 +01:00
Application: tview.NewApplication(),
pages: tview.NewPages(),
flex: tview.NewFlex(),
quit: tview.NewModal(),
2023-04-23 00:17:34 +01:00
add: NewModalInput(),
2023-04-22 19:18:47 +01:00
focusedColumn: 0,
columns: nil,
move: nil,
board: board.Board{},
2023-04-22 19:18:47 +01:00
}
ui.init()
2023-04-22 19:18:47 +01:00
return ui
2023-04-22 19:18:47 +01:00
}
2023-04-23 00:35:08 +01:00
// closeBoard closes the board.
2023-04-23 00:17:34 +01:00
func (u *UI) closeBoard() {
_ = u.board.Close()
}
2023-04-23 00:35:08 +01:00
// init initialises the UI.
2023-04-23 00:17:34 +01:00
func (u *UI) init() {
2023-04-22 19:18:47 +01:00
u.pages.AddPage(mainPageName, u.flex, true, true)
u.initQuitModal()
u.pages.AddPage(quitPageName, u.quit, false, false)
2023-04-23 00:17:34 +01:00
u.initAddInputModal()
u.pages.AddPage(addPageName, u.add, false, false)
2023-04-22 19:18:47 +01:00
u.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
2023-04-23 04:27:16 +01:00
switch event.Rune() {
case 'o':
if u.flex.HasFocus() && len(u.columns) == 0 {
_ = u.openBoard("")
2023-04-23 04:27:16 +01:00
}
2023-04-22 19:18:47 +01:00
}
return event
})
u.SetRoot(u.pages, true)
}
2023-04-23 00:35:08 +01:00
// initAddInputModal initialises the add input modal.
2023-04-23 00:17:34 +01:00
func (u *UI) initAddInputModal() {
doneFunc := func(text string, success bool) {
if success {
_ = u.newCard(text, "")
}
2023-04-23 00:17:34 +01:00
u.pages.HidePage(addPageName)
u.setColumnFocus()
}
u.add.SetDoneFunc(doneFunc)
}
2023-04-22 19:18:47 +01:00
// initQuitModal initialises the quit modal.
func (u *UI) initQuitModal() {
quitDoneFunc := func(_ int, buttonLabel string) {
switch buttonLabel {
case "Quit":
u.shutdown()
default:
2023-04-23 00:17:34 +01:00
u.pages.SwitchToPage(mainPageName)
2023-04-22 19:18:47 +01:00
u.setColumnFocus()
}
}
u.quit.SetText("Do you want to quit the application?").
AddButtons([]string{"Quit", "Cancel"}).
SetDoneFunc(quitDoneFunc)
}
2023-04-23 00:35:08 +01:00
// newCard creates and saves a new card to the database.
2023-04-23 00:17:34 +01:00
func (u *UI) newCard(title, content string) error {
args := board.CardArgs{
NewTitle: title,
NewContent: content,
}
if _, err := u.board.CreateCard(args); err != nil {
2023-04-23 00:17:34 +01:00
return fmt.Errorf("unable to create card, %w", err)
}
_ = u.refresh()
2023-04-23 00:17:34 +01:00
return nil
}
2023-04-23 00:35:08 +01:00
// openBoard opens the kanban board.
2023-04-22 17:50:27 +01:00
func (u *UI) openBoard(path string) error {
b, err := board.Open(path)
if err != nil {
return fmt.Errorf("unable to load board, %w", err)
}
2023-04-22 17:50:27 +01:00
u.board = b
2023-04-22 17:50:27 +01:00
if err = u.refresh(); err != nil {
return fmt.Errorf("error refreshing the board, %w", err)
}
return nil
}
2023-04-23 00:35:08 +01:00
// refresh refreshes the UI.
2023-04-22 17:50:27 +01:00
func (u *UI) refresh() error {
statusList, err := u.board.StatusList()
if err != nil {
return fmt.Errorf("unable to get the status list, %w", err)
}
u.updateColumns(statusList)
u.updateMovePage(statusList)
u.setColumnFocus()
return nil
}
2023-04-23 00:17:34 +01:00
// shutdown shuts down the application.
func (u *UI) shutdown() {
u.closeBoard()
u.Stop()
}
func (u *UI) updateMovePage(statusList []board.Status) {
if u.pages.HasPage(movePageName) {
u.pages.RemovePage(movePageName)
}
move := tview.NewFlex()
statusSelection := tview.NewList()
statusSelection.SetBorder(true)
statusSelection.ShowSecondaryText(false)
statusSelection.SetHighlightFullLine(true)
statusSelection.SetSelectedFocusOnly(true)
statusSelection.SetWrapAround(false)
doneFunc := func() {
u.pages.HidePage(movePageName)
u.setColumnFocus()
}
statusSelection.SetDoneFunc(doneFunc)
selectedFunc := func(_ int, _, secondary string, _ rune) {
currentStatusID := u.columns[u.focusedColumn].statusID
nextStatusID, err := strconv.Atoi(secondary)
if err != nil {
nextStatusID = 0
}
currentItem := u.columns[u.focusedColumn].cards.GetCurrentItem()
_, cardIDText := u.columns[u.focusedColumn].cards.GetItemText(currentItem)
cardID, _ := strconv.Atoi(cardIDText)
args := board.MoveToStatusArgs{
CardID: cardID,
CurrentStatusID: currentStatusID,
NextStatusID: nextStatusID,
}
_ = u.board.MoveToStatus(args)
u.pages.HidePage(movePageName)
_ = u.refresh()
}
statusSelection.SetSelectedFunc(selectedFunc)
for _, status := range statusList {
id := strconv.Itoa(status.ID)
statusSelection.AddItem(fmt.Sprintf("\u25C9 %s", status.Name), id, 0, nil)
}
move.AddItem(statusSelection, 0, 1, true)
u.move = move
u.pages.AddPage(movePageName, move, false, false)
}