feat: move a card between statuses #3
4 changed files with 170 additions and 106 deletions
|
@ -12,16 +12,16 @@ type column struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (u *UI) newColumn(statusID int, statusName string) column {
|
func (u *UI) newColumn(statusID int, statusName string) column {
|
||||||
l := tview.NewList()
|
cards := tview.NewList()
|
||||||
|
|
||||||
l.SetBorder(true)
|
cards.SetBorder(true)
|
||||||
l.ShowSecondaryText(false)
|
cards.ShowSecondaryText(false)
|
||||||
l.SetTitle(" " + statusName + " ")
|
cards.SetTitle(" " + statusName + " ")
|
||||||
l.SetHighlightFullLine(true)
|
cards.SetHighlightFullLine(true)
|
||||||
l.SetSelectedFocusOnly(true)
|
cards.SetSelectedFocusOnly(true)
|
||||||
l.SetWrapAround(false)
|
cards.SetWrapAround(false)
|
||||||
|
|
||||||
l.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
|
cards.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
|
||||||
if event.Rune() == 'h' || event.Key() == tcell.KeyLeft {
|
if event.Rune() == 'h' || event.Key() == tcell.KeyLeft {
|
||||||
u.shiftColumnFocus(shiftLeft)
|
u.shiftColumnFocus(shiftLeft)
|
||||||
} else if event.Rune() == 'l' || event.Key() == tcell.KeyRight {
|
} else if event.Rune() == 'l' || event.Key() == tcell.KeyRight {
|
||||||
|
@ -34,8 +34,31 @@ func (u *UI) newColumn(statusID int, statusName string) column {
|
||||||
c := column{
|
c := column{
|
||||||
statusID: statusID,
|
statusID: statusID,
|
||||||
statusName: statusName,
|
statusName: statusName,
|
||||||
cards: l,
|
cards: cards,
|
||||||
}
|
}
|
||||||
|
|
||||||
return c
|
return c
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (u *UI) setColumnFocus() {
|
||||||
|
u.SetFocus(u.columns[u.focusedColumn].cards)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (u *UI) shiftColumnFocus(s int) {
|
||||||
|
switch s {
|
||||||
|
case shiftRight:
|
||||||
|
if u.focusedColumn == len(u.columns)-1 {
|
||||||
|
u.focusedColumn = 0
|
||||||
|
} else {
|
||||||
|
u.focusedColumn++
|
||||||
|
}
|
||||||
|
case shiftLeft:
|
||||||
|
if u.focusedColumn == 0 {
|
||||||
|
u.focusedColumn = len(u.columns) - 1
|
||||||
|
} else {
|
||||||
|
u.focusedColumn--
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
u.setColumnFocus()
|
||||||
|
}
|
||||||
|
|
|
@ -1,35 +0,0 @@
|
||||||
package ui
|
|
||||||
|
|
||||||
import (
|
|
||||||
"github.com/rivo/tview"
|
|
||||||
)
|
|
||||||
|
|
||||||
// newAddForm creates a new Form primitive for creating a new card.
|
|
||||||
func newAddForm(u *UI) *tview.Form {
|
|
||||||
add := tview.NewForm()
|
|
||||||
|
|
||||||
titleField := "Title"
|
|
||||||
|
|
||||||
add.AddInputField(titleField, "", 0, nil, nil)
|
|
||||||
|
|
||||||
add.AddButton("Save", func() {
|
|
||||||
title := add.GetFormItemByLabel(titleField).(*tview.InputField).GetText()
|
|
||||||
// TODO: error value needs handling
|
|
||||||
_ = u.newCard(title, "")
|
|
||||||
add.GetFormItemByLabel(titleField).(*tview.InputField).SetText("")
|
|
||||||
u.pages.SwitchToPage(mainPageName)
|
|
||||||
u.setColumnFocus()
|
|
||||||
})
|
|
||||||
|
|
||||||
add.AddButton("Cancel", func() {
|
|
||||||
u.pages.SwitchToPage(mainPageName)
|
|
||||||
add.GetFormItemByLabel(titleField).(*tview.InputField).SetText("")
|
|
||||||
u.setColumnFocus()
|
|
||||||
})
|
|
||||||
|
|
||||||
add.SetBorder(true)
|
|
||||||
|
|
||||||
add.SetTitle(" New Card ")
|
|
||||||
|
|
||||||
return add
|
|
||||||
}
|
|
86
internal/ui/modalinput.go
Normal file
86
internal/ui/modalinput.go
Normal file
|
@ -0,0 +1,86 @@
|
||||||
|
package ui
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/gdamore/tcell/v2"
|
||||||
|
"github.com/rivo/tview"
|
||||||
|
)
|
||||||
|
|
||||||
|
type modalInput struct {
|
||||||
|
*tview.Form
|
||||||
|
frame *tview.Frame
|
||||||
|
text string
|
||||||
|
done func(string, bool)
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewModalInput() *modalInput {
|
||||||
|
form := tview.NewForm()
|
||||||
|
m := modalInput{
|
||||||
|
Form: form,
|
||||||
|
frame: tview.NewFrame(form),
|
||||||
|
text: "",
|
||||||
|
done: nil,
|
||||||
|
}
|
||||||
|
|
||||||
|
m.SetButtonsAlign(tview.AlignCenter).
|
||||||
|
SetButtonBackgroundColor(tview.Styles.PrimitiveBackgroundColor).
|
||||||
|
SetButtonTextColor(tview.Styles.PrimaryTextColor).
|
||||||
|
SetBackgroundColor(tview.Styles.ContrastBackgroundColor).
|
||||||
|
SetBorderPadding(0, 0, 0, 0)
|
||||||
|
|
||||||
|
m.AddInputField("", "", 50, nil, func(text string) {
|
||||||
|
m.text = text
|
||||||
|
})
|
||||||
|
|
||||||
|
m.AddButton("OK", func() {
|
||||||
|
if m.done != nil {
|
||||||
|
m.done(m.text, true)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
m.AddButton("Cancel", func() {
|
||||||
|
if m.done != nil {
|
||||||
|
m.done(m.text, false)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
m.frame.SetBorders(0, 0, 1, 0, 0, 0).
|
||||||
|
SetBorder(true).
|
||||||
|
SetBackgroundColor(tview.Styles.ContrastBackgroundColor).
|
||||||
|
SetBorderPadding(1, 1, 1, 1)
|
||||||
|
|
||||||
|
return &m
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *modalInput) SetValue(text string) {
|
||||||
|
m.Clear(false)
|
||||||
|
m.AddInputField("", text, 50, nil, func(text string) {
|
||||||
|
m.text = text
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *modalInput) SetDoneFunc(handler func(string, bool)) *modalInput {
|
||||||
|
m.done = handler
|
||||||
|
return m
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *modalInput) Draw(screen tcell.Screen) {
|
||||||
|
buttonsWidth := 50
|
||||||
|
screenWidth, screenHeight := screen.Size()
|
||||||
|
width := screenWidth / 3
|
||||||
|
|
||||||
|
if width < buttonsWidth {
|
||||||
|
width = buttonsWidth
|
||||||
|
}
|
||||||
|
|
||||||
|
height := 7
|
||||||
|
width += 4
|
||||||
|
|
||||||
|
// Set the modal's position and size.
|
||||||
|
x := (screenWidth - width) / 2
|
||||||
|
y := (screenHeight - height) / 2
|
||||||
|
m.SetRect(x, y, width, height)
|
||||||
|
|
||||||
|
// Draw the frame.
|
||||||
|
m.frame.SetRect(x, y, width, height)
|
||||||
|
m.frame.Draw(screen)
|
||||||
|
}
|
|
@ -1,11 +1,13 @@
|
||||||
package ui
|
package ui
|
||||||
|
|
||||||
|
// TODO: Update card status (card ID, oldStatus, newStatus)
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"codeflow.dananglin.me.uk/apollo/canal/internal/board"
|
"codeflow.dananglin.me.uk/apollo/canal/internal/board"
|
||||||
"github.com/rivo/tview"
|
|
||||||
"github.com/gdamore/tcell/v2"
|
"github.com/gdamore/tcell/v2"
|
||||||
|
"github.com/rivo/tview"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
@ -29,6 +31,7 @@ type UI struct {
|
||||||
focusedColumn int
|
focusedColumn int
|
||||||
board board.Board
|
board board.Board
|
||||||
quit *tview.Modal
|
quit *tview.Modal
|
||||||
|
add *modalInput
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewUI returns a new UI value.
|
// NewUI returns a new UI value.
|
||||||
|
@ -38,27 +41,39 @@ func NewUI() UI {
|
||||||
pages: tview.NewPages(),
|
pages: tview.NewPages(),
|
||||||
flex: tview.NewFlex(),
|
flex: tview.NewFlex(),
|
||||||
quit: tview.NewModal(),
|
quit: tview.NewModal(),
|
||||||
|
add: NewModalInput(),
|
||||||
focusedColumn: 0,
|
focusedColumn: 0,
|
||||||
}
|
}
|
||||||
|
|
||||||
u.initialise()
|
u.init()
|
||||||
|
|
||||||
return u
|
return u
|
||||||
}
|
}
|
||||||
|
|
||||||
// initialise the UI.
|
// closeBoard closes the BoltDB database.
|
||||||
func (u *UI) initialise() {
|
func (u *UI) closeBoard() {
|
||||||
|
_ = u.board.Close()
|
||||||
|
}
|
||||||
|
|
||||||
|
// init the UI.
|
||||||
|
func (u *UI) init() {
|
||||||
u.pages.AddPage(mainPageName, u.flex, true, true)
|
u.pages.AddPage(mainPageName, u.flex, true, true)
|
||||||
|
|
||||||
u.initQuitModal()
|
u.initQuitModal()
|
||||||
u.pages.AddPage(quitPageName, u.quit, false, false)
|
u.pages.AddPage(quitPageName, u.quit, false, false)
|
||||||
|
|
||||||
|
u.initAddInputModal()
|
||||||
|
u.pages.AddPage(addPageName, u.add, false, false)
|
||||||
|
|
||||||
u.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
|
u.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
|
||||||
if event.Rune() == 'q' {
|
if event.Rune() == 'q' {
|
||||||
u.pages.ShowPage(quitPageName)
|
u.pages.ShowPage(quitPageName)
|
||||||
u.SetFocus(u.quit)
|
u.SetFocus(u.quit)
|
||||||
} else if event.Rune() == 'o' {
|
} else if event.Rune() == 'o' {
|
||||||
u.openBoard("")
|
u.openBoard("")
|
||||||
|
} else if event.Rune() == 'a' {
|
||||||
|
u.pages.ShowPage(addPageName)
|
||||||
|
u.SetFocus(u.add)
|
||||||
}
|
}
|
||||||
|
|
||||||
return event
|
return event
|
||||||
|
@ -67,6 +82,18 @@ func (u *UI) initialise() {
|
||||||
u.SetRoot(u.pages, true)
|
u.SetRoot(u.pages, true)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (u *UI) initAddInputModal() {
|
||||||
|
doneFunc := func(text string, success bool) {
|
||||||
|
if success {
|
||||||
|
_ = u.newCard(text, "")
|
||||||
|
}
|
||||||
|
u.pages.HidePage(addPageName)
|
||||||
|
u.setColumnFocus()
|
||||||
|
}
|
||||||
|
|
||||||
|
u.add.SetDoneFunc(doneFunc)
|
||||||
|
}
|
||||||
|
|
||||||
// initQuitModal initialises the quit modal.
|
// initQuitModal initialises the quit modal.
|
||||||
func (u *UI) initQuitModal() {
|
func (u *UI) initQuitModal() {
|
||||||
quitDoneFunc := func(_ int, buttonLabel string) {
|
quitDoneFunc := func(_ int, buttonLabel string) {
|
||||||
|
@ -74,7 +101,7 @@ func (u *UI) initQuitModal() {
|
||||||
case "Quit":
|
case "Quit":
|
||||||
u.shutdown()
|
u.shutdown()
|
||||||
default:
|
default:
|
||||||
u.pages.SwitchToPage("main")
|
u.pages.SwitchToPage(mainPageName)
|
||||||
u.setColumnFocus()
|
u.setColumnFocus()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -84,15 +111,20 @@ func (u *UI) initQuitModal() {
|
||||||
SetDoneFunc(quitDoneFunc)
|
SetDoneFunc(quitDoneFunc)
|
||||||
}
|
}
|
||||||
|
|
||||||
// shutdown shuts down the application.
|
// newCard creates a new card and saves it to the database.
|
||||||
func (u *UI) shutdown() {
|
func (u *UI) newCard(title, content string) error {
|
||||||
u.closeBoard()
|
args := board.CardArgs{
|
||||||
u.Stop()
|
NewTitle: title,
|
||||||
}
|
NewContent: content,
|
||||||
|
}
|
||||||
|
|
||||||
// closeBoard closes the BoltDB database.
|
if err := u.board.CreateCard(args); err != nil {
|
||||||
func (u *UI) closeBoard() {
|
return fmt.Errorf("unable to create card, %w", err)
|
||||||
_ = u.board.Close()
|
}
|
||||||
|
|
||||||
|
u.refresh()
|
||||||
|
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// openBoard opens the kanban project.
|
// openBoard opens the kanban project.
|
||||||
|
@ -111,7 +143,7 @@ func (u *UI) openBoard(path string) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// refresh refreshes the UI.
|
// refresh the UI.
|
||||||
func (u *UI) refresh() error {
|
func (u *UI) refresh() error {
|
||||||
statusList, err := u.board.StatusList()
|
statusList, err := u.board.StatusList()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -125,6 +157,12 @@ func (u *UI) refresh() error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// shutdown shuts down the application.
|
||||||
|
func (u *UI) shutdown() {
|
||||||
|
u.closeBoard()
|
||||||
|
u.Stop()
|
||||||
|
}
|
||||||
|
|
||||||
func (u *UI) updateBoard(statusList []board.Status) error {
|
func (u *UI) updateBoard(statusList []board.Status) error {
|
||||||
u.flex.Clear()
|
u.flex.Clear()
|
||||||
|
|
||||||
|
@ -151,51 +189,3 @@ func (u *UI) updateBoard(statusList []board.Status) error {
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (u *UI) shiftColumnFocus(s int) {
|
|
||||||
switch s {
|
|
||||||
case shiftRight:
|
|
||||||
if u.focusedColumn == len(u.columns)-1 {
|
|
||||||
u.focusedColumn = 0
|
|
||||||
} else {
|
|
||||||
u.focusedColumn++
|
|
||||||
}
|
|
||||||
case shiftLeft:
|
|
||||||
if u.focusedColumn == 0 {
|
|
||||||
u.focusedColumn = len(u.columns) - 1
|
|
||||||
} else {
|
|
||||||
u.focusedColumn--
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
u.setColumnFocus()
|
|
||||||
}
|
|
||||||
|
|
||||||
func (u *UI) setColumnFocus() {
|
|
||||||
u.SetFocus(u.columns[u.focusedColumn].cards)
|
|
||||||
}
|
|
||||||
|
|
||||||
// newCard creates a new card and saves it to the database.
|
|
||||||
func (u *UI) newCard(title, content string) error {
|
|
||||||
args := board.CardArgs{
|
|
||||||
NewTitle: title,
|
|
||||||
NewContent: content,
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := u.board.CreateCard(args); err != nil {
|
|
||||||
return fmt.Errorf("unable to create card, %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
u.refresh()
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO: Move 'Add' to the centre of the app
|
|
||||||
// TODO: Customize list primitive or create a new one
|
|
||||||
// TODO: If customizing existing list primitive, wrap list around a column type. Add statusID to it.
|
|
||||||
// TODO: Update card status (card ID, oldStatus, newStatus)
|
|
||||||
|
|
||||||
//func viewCard() error {
|
|
||||||
//return nil
|
|
||||||
//}
|
|
||||||
|
|
Loading…
Reference in a new issue