-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathform.go
More file actions
85 lines (76 loc) · 1.86 KB
/
Copy pathform.go
File metadata and controls
85 lines (76 loc) · 1.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package main
import (
"github.com/charmbracelet/bubbles/v2/help"
"github.com/charmbracelet/bubbles/v2/key"
"github.com/charmbracelet/bubbles/v2/textarea"
"github.com/charmbracelet/bubbles/v2/textinput"
tea "github.com/charmbracelet/bubbletea/v2"
"github.com/charmbracelet/lipgloss/v2"
)
type ReturnBoardMsg struct {
abort bool
form Form
}
type Form struct {
help help.Model
title textinput.Model
description textarea.Model
col column
index int
}
func newDefaultForm() Form {
return NewForm("task name", "")
}
func NewForm(title, description string) Form {
form := Form{
help: help.New(),
title: textinput.New(),
description: textarea.New(),
}
form.title.SetWidth(defaultWidth)
form.description.SetWidth(defaultWidth)
form.title.Placeholder = title
form.description.Placeholder = description
form.title.Focus()
return form
}
func (f Form) CreateTask() Task {
return Task{f.col.status, f.title.Value(), f.description.Value()}
}
func (f Form) Update(msg tea.Msg) (Form, tea.Cmd) {
var cmd tea.Cmd
switch msg := msg.(type) {
case column:
f.col = msg
f.col.list.Index()
case tea.KeyMsg:
switch {
case key.Matches(msg, keys.Quit):
return f, tea.Quit
case key.Matches(msg, keys.Back):
return f, func() tea.Msg { return ReturnBoardMsg{true, f} }
case key.Matches(msg, keys.Enter):
if f.title.Focused() {
f.title.Blur()
f.description.Focus()
return f, textarea.Blink
}
// Return the completed form as a message.
return f, func() tea.Msg { return ReturnBoardMsg{false, f} }
}
}
if f.title.Focused() {
f.title, cmd = f.title.Update(msg)
return f, cmd
}
f.description, cmd = f.description.Update(msg)
return f, cmd
}
func (f Form) View() string {
return lipgloss.JoinVertical(
lipgloss.Left,
"Create a new task",
f.title.View(),
f.description.View(),
f.help.View(keys))
}