Compare commits
6 Commits
1cc6d923fc
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
1ec5072ef4 | ||
|
|
3acff6618c | ||
|
|
ae05b237b4
|
||
|
|
891ea79d53
|
||
|
|
fcb23827e4
|
||
|
|
117fa5085b
|
108
bubbletea.go
108
bubbletea.go
@@ -13,6 +13,10 @@ func (m model) Init() tea.Cmd {
|
|||||||
|
|
||||||
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
||||||
var cmd tea.Cmd
|
var cmd tea.Cmd
|
||||||
|
|
||||||
|
if m.addTask {
|
||||||
|
m.textinput, cmd = m.textinput.Update(msg)
|
||||||
|
}
|
||||||
|
|
||||||
switch msg := msg.(type) {
|
switch msg := msg.(type) {
|
||||||
|
|
||||||
@@ -50,54 +54,92 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
|||||||
|
|
||||||
// new task
|
// new task
|
||||||
case "n":
|
case "n":
|
||||||
m.addTask = true
|
if !m.addTask {
|
||||||
|
m.addTask = true
|
||||||
|
m.textinput.Focus()
|
||||||
|
m.textinput.Reset()
|
||||||
|
}
|
||||||
|
|
||||||
case "esc":
|
case "esc":
|
||||||
m.addTask = false
|
if m.addTask {
|
||||||
|
m.addTask = false
|
||||||
|
m.textinput.Reset()
|
||||||
|
}
|
||||||
|
|
||||||
|
case "enter":
|
||||||
|
if m.addTask {
|
||||||
|
AddNewTask(&m)
|
||||||
|
m.addTask = false
|
||||||
|
m.textinput.Reset()
|
||||||
|
}
|
||||||
|
|
||||||
// The "enter" key and the spacebar (a literal space) toggle
|
// The "enter" key and the spacebar (a literal space) toggle
|
||||||
// the selected state for the item that the cursor is pointing at.
|
// the selected state for the item that the cursor is pointing at.
|
||||||
case "enter", " ":
|
case " ":
|
||||||
_, ok := m.selected[m.cursor]
|
|
||||||
if ok {
|
if !m.addTask {
|
||||||
delete(m.selected, m.cursor)
|
if m.todos[m.cursor].done {
|
||||||
} else {
|
m.todos[m.cursor].done = false
|
||||||
m.selected[m.cursor] = struct{}{}
|
} else {
|
||||||
|
m.todos[m.cursor].done = true
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
m.textinput, cmd = m.textinput.Update(msg)
|
|
||||||
|
|
||||||
// Return the updated model to the Bubble Tea runtime for processing.
|
// Return the updated model to the Bubble Tea runtime for processing.
|
||||||
// Note that we're not returning a command.
|
// Note that we're not returning a command.
|
||||||
return m, cmd
|
return m, cmd
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func AddNewTask(m *model) {
|
||||||
|
t := todo{
|
||||||
|
name: m.textinput.Value(),
|
||||||
|
done: false,
|
||||||
|
startdate: 0,
|
||||||
|
deadline: 0,
|
||||||
|
priority: 1,
|
||||||
|
isInbox: true,
|
||||||
|
}
|
||||||
|
m.todos = append(m.todos, t)
|
||||||
|
}
|
||||||
|
|
||||||
func (m model) View() string {
|
func (m model) View() string {
|
||||||
// The header
|
// The header
|
||||||
s := ""
|
s := ""
|
||||||
currentList := []todo{}
|
currentList := []todo{}
|
||||||
|
|
||||||
// switch m.tab {
|
|
||||||
// case 0:
|
|
||||||
// s += "Inbox"
|
|
||||||
// inboxFilter := func(t todo) bool { return t.isInbox }
|
|
||||||
// currentList = filter(m.todos, inboxFilter)
|
|
||||||
// case 1:
|
|
||||||
// s += "Today"
|
|
||||||
// case 2:
|
|
||||||
// s += "Tomorrow"
|
|
||||||
// case 3:
|
|
||||||
// s += "Scheduled"
|
|
||||||
// case 4:
|
|
||||||
// s += "Anytime"
|
|
||||||
// }
|
|
||||||
|
|
||||||
s += "GOTD\n\n"
|
s += "GOTD\n\n"
|
||||||
|
|
||||||
|
switch m.tab {
|
||||||
|
case 0:
|
||||||
|
// s += "Inbox"
|
||||||
|
taskFilter := func(t todo) bool { return t.isInbox }
|
||||||
|
currentList = filter(m.todos, taskFilter)
|
||||||
|
case 1:
|
||||||
|
// s += "Today"
|
||||||
|
taskFilter := func(t todo) bool { return t.startdate == int(midnightToUnix()) || t.deadline == int(midnightToUnix()) }
|
||||||
|
currentList = filter(m.todos, taskFilter)
|
||||||
|
case 2:
|
||||||
|
// s += "Tomorrow"
|
||||||
|
// 86400 seconds in 24h, add it on to today's midnight for tomorrow's midnight
|
||||||
|
taskFilter := func(t todo) bool { return t.startdate == int(midnightToUnix()) + 86400 || t.deadline == int(midnightToUnix()) + 86400}
|
||||||
|
currentList = filter(m.todos, taskFilter)
|
||||||
|
case 3:
|
||||||
|
// s += "Scheduled"
|
||||||
|
taskFilter := func(t todo) bool { return t.startdate != -1 }
|
||||||
|
currentList = filter(m.todos, taskFilter)
|
||||||
|
case 4:
|
||||||
|
// s += "Anytime"
|
||||||
|
taskFilter := func(t todo) bool { return t.startdate == -1 }
|
||||||
|
currentList = filter(m.todos, taskFilter)
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
// Iterate over our choices
|
// Iterate over our choices
|
||||||
for i, choice := range currentList {
|
for i, value := range currentList {
|
||||||
|
|
||||||
// Is the cursor pointing at this choice?
|
// Is the cursor pointing at this choice?
|
||||||
cursor := " " // no cursor
|
cursor := " " // no cursor
|
||||||
@@ -107,19 +149,23 @@ func (m model) View() string {
|
|||||||
|
|
||||||
// Is this choice selected?
|
// Is this choice selected?
|
||||||
checked := " " // not selected
|
checked := " " // not selected
|
||||||
if _, ok := m.selected[i]; ok {
|
// if _, ok := m.selected[i]; ok {
|
||||||
checked = "x" // selected!
|
// checked = "x" // selected!
|
||||||
|
// }
|
||||||
|
if value.done {
|
||||||
|
checked = "x"
|
||||||
}
|
}
|
||||||
|
|
||||||
// Render the row
|
// Render the row
|
||||||
s += fmt.Sprintf("%s [%s] %s\n", cursor, checked, choice)
|
s += fmt.Sprintf("%s [%s] %s\n", cursor, checked, value.name)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
s += "\n\n"
|
||||||
|
|
||||||
// render tab bar
|
// render tab bar
|
||||||
for i, v := range []string{"Inbox", "Today", "Tomorrow", "Scheduled", "Anytime"} {
|
for i, v := range []string{"Inbox", "Today", "Tomorrow", "Scheduled", "Anytime"} {
|
||||||
if i == m.tab {
|
if i == m.tab {
|
||||||
s += lipgloss.
|
s += lipgloss.NewStyle().
|
||||||
NewStyle().
|
|
||||||
Bold(true).
|
Bold(true).
|
||||||
Underline(true).
|
Underline(true).
|
||||||
PaddingRight(3).
|
PaddingRight(3).
|
||||||
@@ -136,8 +182,6 @@ func (m model) View() string {
|
|||||||
|
|
||||||
if m.addTask {
|
if m.addTask {
|
||||||
s += ": "
|
s += ": "
|
||||||
m.textinput.Focus()
|
|
||||||
m.textinput.Cursor.BlinkCmd()
|
|
||||||
s += m.textinput.View()
|
s += m.textinput.View()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
24
helper.go
24
helper.go
@@ -1,5 +1,12 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"os"
|
||||||
|
"time"
|
||||||
|
"os/exec"
|
||||||
|
"runtime"
|
||||||
|
)
|
||||||
|
|
||||||
func filter[T any](ss []T, test func(T) bool) (ret []T) {
|
func filter[T any](ss []T, test func(T) bool) (ret []T) {
|
||||||
for _, s := range ss {
|
for _, s := range ss {
|
||||||
if test(s) {
|
if test(s) {
|
||||||
@@ -7,4 +14,21 @@ func filter[T any](ss []T, test func(T) bool) (ret []T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func midnightToUnix() int64 {
|
||||||
|
now := time.Now()
|
||||||
|
midnight := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, now.Location())
|
||||||
|
return midnight.Unix()
|
||||||
|
}
|
||||||
|
|
||||||
|
func clearTerminal() {
|
||||||
|
var cmd *exec.Cmd
|
||||||
|
if runtime.GOOS == "windows" {
|
||||||
|
cmd = exec.Command("cmd", "/c", "cls")
|
||||||
|
} else {
|
||||||
|
cmd = exec.Command("clear")
|
||||||
|
}
|
||||||
|
cmd.Stdout = os.Stdout
|
||||||
|
cmd.Run()
|
||||||
}
|
}
|
||||||
2
main.go
2
main.go
@@ -11,6 +11,8 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
clearTerminal()
|
||||||
|
|
||||||
p := tea.NewProgram(initialModel())
|
p := tea.NewProgram(initialModel())
|
||||||
if _, err := p.Run(); err != nil {
|
if _, err := p.Run(); err != nil {
|
||||||
fmt.Printf("Alas, there's been an error: %v", err)
|
fmt.Printf("Alas, there's been an error: %v", err)
|
||||||
|
|||||||
4
model.go
4
model.go
@@ -15,7 +15,7 @@ type todo struct {
|
|||||||
|
|
||||||
type model struct {
|
type model struct {
|
||||||
todos []todo // ALL items on the to-do list
|
todos []todo // ALL items on the to-do list
|
||||||
list []todo // items currently visible on the list right now
|
// list []todo // items currently visible on the list right now
|
||||||
cursor int // which to-do list item our cursor is pointing at
|
cursor int // which to-do list item our cursor is pointing at
|
||||||
selected map[int]struct{} // which to-do items are selected
|
selected map[int]struct{} // which to-do items are selected
|
||||||
tab int // which tab is selected
|
tab int // which tab is selected
|
||||||
@@ -36,7 +36,7 @@ func initialModel() model {
|
|||||||
// Start empty
|
// Start empty
|
||||||
todos: []todo{},
|
todos: []todo{},
|
||||||
|
|
||||||
list: []todo{},
|
// list: []todo{},
|
||||||
|
|
||||||
// start on today tab
|
// start on today tab
|
||||||
// 0: inbox, 1: today, 2: tomorrow, 3: scheduled, 4: anytime
|
// 0: inbox, 1: today, 2: tomorrow, 3: scheduled, 4: anytime
|
||||||
|
|||||||
Reference in New Issue
Block a user