Initial commit

This commit is contained in:
Martin
2024-05-20 21:17:47 +02:00
commit d960f1f113
25 changed files with 1897 additions and 0 deletions

27
common/common.go Normal file
View File

@ -0,0 +1,27 @@
package common
import (
"context"
"tasksquire/taskwarrior"
tea "github.com/charmbracelet/bubbletea"
)
type Common struct {
Ctx context.Context
PageStack *Stack[tea.Model]
TW taskwarrior.TaskWarrior
Keymap *Keymap
Styles *Styles
}
func NewCommon(ctx context.Context, tw taskwarrior.TaskWarrior) *Common {
return &Common{
Ctx: ctx,
PageStack: NewStack[tea.Model](),
TW: tw,
Keymap: NewKeymap(),
Styles: NewStyles(tw.GetConfig()),
}
}

62
common/keymap.go Normal file
View File

@ -0,0 +1,62 @@
package common
import (
"github.com/charmbracelet/bubbles/key"
)
// Keymap is a collection of key bindings.
type Keymap struct {
Quit key.Binding
Back key.Binding
Add key.Binding
Edit key.Binding
SetReport key.Binding
SetContext key.Binding
SetProject key.Binding
Select key.Binding
}
// NewKeymap creates a new Keymap.
func NewKeymap() *Keymap {
return &Keymap{
Quit: key.NewBinding(
key.WithKeys("q", "ctrl+c"),
key.WithHelp("q, ctrl+c", "Quit"),
),
Back: key.NewBinding(
key.WithKeys("esc"),
key.WithHelp("esc", "Back"),
),
Add: key.NewBinding(
key.WithKeys("a"),
key.WithHelp("a", "Add new task"),
),
Edit: key.NewBinding(
key.WithKeys("e"),
key.WithHelp("e", "Edit task"),
),
SetReport: key.NewBinding(
key.WithKeys("r"),
key.WithHelp("r", "Set report"),
),
SetContext: key.NewBinding(
key.WithKeys("c"),
key.WithHelp("c", "Set context"),
),
SetProject: key.NewBinding(
key.WithKeys("p"),
key.WithHelp("p", "Set project"),
),
Select: key.NewBinding(
key.WithKeys("enter"),
key.WithHelp("enter", "Select"),
),
}
}

40
common/stack.go Normal file
View File

@ -0,0 +1,40 @@
package common
import (
"errors"
"sync"
)
type Stack[T any] struct {
items []T
mutex sync.Mutex
}
func NewStack[T any]() *Stack[T] {
return &Stack[T]{
items: make([]T, 0),
mutex: sync.Mutex{},
}
}
func (s *Stack[T]) Push(item T) {
s.mutex.Lock()
defer s.mutex.Unlock()
s.items = append(s.items, item)
}
func (s *Stack[T]) Pop() (T, error) {
s.mutex.Lock()
defer s.mutex.Unlock()
if len(s.items) == 0 {
var empty T
return empty, errors.New("stack is empty")
}
item := s.items[len(s.items)-1]
s.items = s.items[:len(s.items)-1]
return item, nil
}

25
common/styles.go Normal file
View File

@ -0,0 +1,25 @@
package common
import (
"github.com/charmbracelet/lipgloss"
"tasksquire/taskwarrior"
)
type Styles struct {
Main lipgloss.Style
ActiveTask lipgloss.Style
InactiveTask lipgloss.Style
NormalTask lipgloss.Style
}
func NewStyles(config *taskwarrior.TWConfig) *Styles {
return &Styles{
Main: lipgloss.NewStyle().Foreground(lipgloss.Color("241")),
ActiveTask: lipgloss.NewStyle().Foreground(lipgloss.Color("255")).Background(lipgloss.Color("236")),
InactiveTask: lipgloss.NewStyle().Foreground(lipgloss.Color("241")).Background(lipgloss.Color("236")),
NormalTask: lipgloss.NewStyle().Foreground(lipgloss.Color("241")),
}
}