Compare commits

5 Commits

Author SHA1 Message Date
5cbfc58aa3 Add time editing 2026-02-01 21:46:11 +01:00
72a5c57faa Add time page 2026-02-01 21:30:19 +01:00
b47763034b Refactor picker 2026-02-01 14:00:33 +01:00
5de3b646fc Integrate timewarrior 2026-02-01 14:00:33 +01:00
a846d2f562 Add README 2026-01-10 18:55:16 +01:00
18 changed files with 2096 additions and 202 deletions

1
.gitignore vendored
View File

@ -1,3 +1,4 @@
.DS_Store .DS_Store
app.log app.log
test/taskchampion.sqlite3 test/taskchampion.sqlite3
tasksquire

71
GEMINI.md Normal file
View File

@ -0,0 +1,71 @@
# Tasksquire
## Project Overview
Tasksquire is a Terminal User Interface (TUI) for [Taskwarrior](https://taskwarrior.org/), built using Go and the [Charm](https://charm.sh/) ecosystem (Bubble Tea, Lip Gloss, Huh). It provides a visual and interactive way to manage your tasks, contexts, and reports directly from the terminal.
The application functions as a wrapper around the `task` command-line tool, parsing its output (JSON, config) and executing commands to read and modify task data.
## Architecture
The project follows the standard [Bubble Tea](https://github.com/charmbracelet/bubbletea) Model-View-Update (MVU) architecture.
### Key Directories & Files
* **`main.go`**: The entry point of the application. It initializes the `TaskSquire` wrapper, sets up logging, and starts the Bubble Tea program with the `MainPage`.
* **`taskwarrior/`**: Contains the logic for interacting with the Taskwarrior CLI.
* `taskwarrior.go`: The core wrapper (`TaskSquire` struct) that executes `task` commands (`export`, `add`, `modify`, etc.) and parses results.
* `models.go`: Defines the Go structs matching Taskwarrior's data model (Tasks, Reports, Config).
* **`pages/`**: Contains the different views of the application.
* `main.go`: The top-level component (`MainPage`) that manages routing/switching between different pages.
* `report.go`: Displays lists of tasks (Taskwarrior reports).
* `taskEditor.go`: UI for creating or editing tasks.
* **`common/`**: Shared utilities, global state, and data structures used across the application.
* **`components/`**: Reusable UI components (e.g., inputs, tables).
* **`timewarrior/`**: Contains logic for integration with Timewarrior (likely in progress or planned).
## Building and Running
### Prerequisites
* **Go**: Version 1.22 or higher.
* **Taskwarrior**: The `task` binary must be installed and available in your system's `PATH`.
### Commands
To run the application directly:
```bash
go run main.go
```
To build a binary:
```bash
go build -o tasksquire main.go
```
### Nix Support
This project includes a `flake.nix` for users of the Nix package manager. You can enter a development shell with all dependencies (Go, tools) by running:
```bash
nix develop
```
## Configuration
Tasksquire respects your existing Taskwarrior configuration (`.taskrc`). It looks for the configuration file in the following order:
1. `TASKRC` environment variable.
2. `$HOME/.taskrc`
3. `$HOME/.config/task/taskrc`
Logging is written to `app.log` in the current working directory.
## Development Conventions
* **UI Framework**: Uses [Bubble Tea](https://github.com/charmbracelet/bubbletea) for the TUI loop.
* **Styling**: Uses [Lip Gloss](https://github.com/charmbracelet/lipgloss) for terminal styling.
* **Forms**: Uses [Huh](https://github.com/charmbracelet/huh) for form inputs.
* **Logging**: Uses `log/slog` for structured logging.

20
README.md Normal file
View File

@ -0,0 +1,20 @@
# TODO
- [>] Add default tags
- Default tags should be defined in the config and always displayed in the tag picker
- [ ] Add project manager
- [ ] Add projects that are always displayed in the project picker
- Saved in config
- [ ] Remove/archive projects
- [ ] Integrate timewarrior
- [ ] Add default timetracking items when addind projects
- [ ] Create interface for timewarrior input
- [ ] Create daily/weekly reports for HRM input
- Combine by project
- Combine by task names
- Combine by tags
- [ ] Add tag manager
- [ ] Edit default tags
- [ ] Update to bubbletea 2.0
# Done
- [x] Use JJ

View File

@ -6,6 +6,7 @@ import (
"os" "os"
"tasksquire/taskwarrior" "tasksquire/taskwarrior"
"tasksquire/timewarrior"
"golang.org/x/term" "golang.org/x/term"
) )
@ -13,6 +14,7 @@ import (
type Common struct { type Common struct {
Ctx context.Context Ctx context.Context
TW taskwarrior.TaskWarrior TW taskwarrior.TaskWarrior
TimeW timewarrior.TimeWarrior
Keymap *Keymap Keymap *Keymap
Styles *Styles Styles *Styles
Udas []taskwarrior.Uda Udas []taskwarrior.Uda
@ -22,10 +24,11 @@ type Common struct {
height int height int
} }
func NewCommon(ctx context.Context, tw taskwarrior.TaskWarrior) *Common { func NewCommon(ctx context.Context, tw taskwarrior.TaskWarrior, timeW timewarrior.TimeWarrior) *Common {
return &Common{ return &Common{
Ctx: ctx, Ctx: ctx,
TW: tw, TW: tw,
TimeW: timeW,
Keymap: NewKeymap(), Keymap: NewKeymap(),
Styles: NewStyles(tw.GetConfig()), Styles: NewStyles(tw.GetConfig()),
Udas: tw.GetUdas(), Udas: tw.GetUdas(),

209
components/picker/picker.go Normal file
View File

@ -0,0 +1,209 @@
package picker
import (
"tasksquire/common"
"github.com/charmbracelet/bubbles/key"
"github.com/charmbracelet/bubbles/list"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
)
type Item struct {
text string
}
func NewItem(text string) Item { return Item{text: text} }
func (i Item) Title() string { return i.text }
func (i Item) Description() string { return "" }
func (i Item) FilterValue() string { return i.text }
// creationItem is a special item for creating new entries
type creationItem struct {
text string
filter string
}
func (i creationItem) Title() string { return i.text }
func (i creationItem) Description() string { return "" }
func (i creationItem) FilterValue() string { return i.filter }
type Picker struct {
common *common.Common
list list.Model
itemProvider func() []list.Item
onSelect func(list.Item) tea.Cmd
onCreate func(string) tea.Cmd
title string
filterByDefault bool
baseItems []list.Item
}
type PickerOption func(*Picker)
func WithFilterByDefault(enabled bool) PickerOption {
return func(p *Picker) {
p.filterByDefault = enabled
}
}
func WithOnCreate(onCreate func(string) tea.Cmd) PickerOption {
return func(p *Picker) {
p.onCreate = onCreate
}
}
func New(
c *common.Common,
title string,
itemProvider func() []list.Item,
onSelect func(list.Item) tea.Cmd,
opts ...PickerOption,
) *Picker {
delegate := list.NewDefaultDelegate()
delegate.ShowDescription = false
delegate.SetSpacing(0)
l := list.New([]list.Item{}, delegate, 0, 0)
l.SetShowTitle(false)
l.SetShowHelp(false)
l.SetShowStatusBar(false)
l.SetFilteringEnabled(true)
// Custom key for filtering (insert mode)
l.KeyMap.Filter = key.NewBinding(
key.WithKeys("i"),
key.WithHelp("i", "filter"),
)
p := &Picker{
common: c,
list: l,
itemProvider: itemProvider,
onSelect: onSelect,
title: title,
}
if c.TW.GetConfig().Get("uda.tasksquire.picker.filter_by_default") == "yes" {
p.filterByDefault = true
}
for _, opt := range opts {
opt(p)
}
p.Refresh()
return p
}
func (p *Picker) Refresh() tea.Cmd {
p.baseItems = p.itemProvider()
return p.updateListItems()
}
func (p *Picker) updateListItems() tea.Cmd {
items := p.baseItems
filterVal := p.list.FilterValue()
if p.onCreate != nil && filterVal != "" {
newItem := creationItem{
text: "(new) " + filterVal,
filter: filterVal,
}
newItems := make([]list.Item, len(items)+1)
copy(newItems, items)
newItems[len(items)] = newItem
items = newItems
}
return p.list.SetItems(items)
}
func (p *Picker) SetSize(width, height int) {
// We do NOT set common.SetSize here, as we are a sub-component.
// Set list size. The parent is responsible for providing a reasonable size.
// If this component is intended to fill a page, width/height will be large.
// If it's a small embedded box, they will be small.
// We apply a small margin for the title if needed, but for now we just pass through
// minus a header gap if we render a title.
headerHeight := 2 // Title + gap
p.list.SetSize(width, height-headerHeight)
}
func (p *Picker) Init() tea.Cmd {
if p.filterByDefault {
return func() tea.Msg {
return tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune{'i'}}
}
}
return nil
}
func (p *Picker) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
var cmd tea.Cmd
switch msg := msg.(type) {
case tea.KeyMsg:
// If filtering, let the list handle keys (including Enter to stop filtering)
if p.list.FilterState() == list.Filtering {
if key.Matches(msg, p.common.Keymap.Ok) {
items := p.list.VisibleItems()
if len(items) == 1 {
return p, p.handleSelect(items[0])
}
}
break // Pass to list.Update
}
switch {
case key.Matches(msg, p.common.Keymap.Ok):
selectedItem := p.list.SelectedItem()
if selectedItem == nil {
return p, nil
}
return p, p.handleSelect(selectedItem)
}
}
prevFilter := p.list.FilterValue()
p.list, cmd = p.list.Update(msg)
if p.list.FilterValue() != prevFilter {
updateCmd := p.updateListItems()
return p, tea.Batch(cmd, updateCmd)
}
return p, cmd
}
func (p *Picker) handleSelect(item list.Item) tea.Cmd {
if cItem, ok := item.(creationItem); ok {
if p.onCreate != nil {
return p.onCreate(cItem.filter)
}
}
return p.onSelect(item)
}
func (p *Picker) View() string {
title := p.common.Styles.Form.Focused.Title.Render(p.title)
return lipgloss.JoinVertical(lipgloss.Left, title, p.list.View())
}
func (p *Picker) IsFiltering() bool {
return p.list.FilterState() == list.Filtering
}
// SelectItemByFilterValue selects the item with the given filter value
func (p *Picker) SelectItemByFilterValue(filterValue string) {
items := p.list.Items()
for i, item := range items {
if item.FilterValue() == filterValue {
p.list.Select(i)
break
}
}
}

View File

@ -0,0 +1,498 @@
package timetable
import (
"strings"
"github.com/charmbracelet/bubbles/key"
"github.com/charmbracelet/bubbles/viewport"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/mattn/go-runewidth"
"tasksquire/common"
"tasksquire/timewarrior"
)
// Model defines a state for the table widget.
type Model struct {
common *common.Common
KeyMap KeyMap
cols []Column
rows timewarrior.Intervals
rowStyles []lipgloss.Style
cursor int
focus bool
styles common.TableStyle
styleFunc StyleFunc
viewport viewport.Model
start int
end int
}
// Row represents one line in the table.
type Row *timewarrior.Interval
// Column defines the table structure.
type Column struct {
Title string
Name string
Width int
MaxWidth int
ContentWidth int
}
// KeyMap defines keybindings. It satisfies to the help.KeyMap interface, which
// is used to render the menu.
type KeyMap struct {
LineUp key.Binding
LineDown key.Binding
PageUp key.Binding
PageDown key.Binding
HalfPageUp key.Binding
HalfPageDown key.Binding
GotoTop key.Binding
GotoBottom key.Binding
}
// ShortHelp implements the KeyMap interface.
func (km KeyMap) ShortHelp() []key.Binding {
return []key.Binding{km.LineUp, km.LineDown}
}
// FullHelp implements the KeyMap interface.
func (km KeyMap) FullHelp() [][]key.Binding {
return [][]key.Binding{
{km.LineUp, km.LineDown, km.GotoTop, km.GotoBottom},
{km.PageUp, km.PageDown, km.HalfPageUp, km.HalfPageDown},
}
}
// DefaultKeyMap returns a default set of keybindings.
func DefaultKeyMap() KeyMap {
const spacebar = " "
return KeyMap{
LineUp: key.NewBinding(
key.WithKeys("up", "k"),
key.WithHelp("↑/k", "up"),
),
LineDown: key.NewBinding(
key.WithKeys("down", "j"),
key.WithHelp("↓/j", "down"),
),
PageUp: key.NewBinding(
key.WithKeys("b", "pgup"),
key.WithHelp("b/pgup", "page up"),
),
PageDown: key.NewBinding(
key.WithKeys("f", "pgdown", spacebar),
key.WithHelp("f/pgdn", "page down"),
),
HalfPageUp: key.NewBinding(
key.WithKeys("u", "ctrl+u"),
key.WithHelp("u", "½ page up"),
),
HalfPageDown: key.NewBinding(
key.WithKeys("d", "ctrl+d"),
key.WithHelp("d", "½ page down"),
),
GotoTop: key.NewBinding(
key.WithKeys("home", "g"),
key.WithHelp("g/home", "go to start"),
),
GotoBottom: key.NewBinding(
key.WithKeys("end", "G"),
key.WithHelp("G/end", "go to end"),
),
}
}
// SetStyles sets the table styles.
func (m *Model) SetStyles(s common.TableStyle) {
m.styles = s
m.UpdateViewport()
}
// Option is used to set options in New. For example:
//
// table := New(WithColumns([]Column{{Title: "ID", Width: 10}}))
type Option func(*Model)
// New creates a new model for the table widget.
func New(com *common.Common, opts ...Option) Model {
m := Model{
common: com,
cursor: 0,
viewport: viewport.New(0, 20),
KeyMap: DefaultKeyMap(),
}
for _, opt := range opts {
opt(&m)
}
m.cols = m.parseColumns(m.cols)
m.rowStyles = m.parseRowStyles(m.rows)
m.UpdateViewport()
return m
}
func (m *Model) parseRowStyles(rows timewarrior.Intervals) []lipgloss.Style {
styles := make([]lipgloss.Style, len(rows))
if len(rows) == 0 {
return styles
}
for i := range rows {
// Default style
styles[i] = m.common.Styles.Base.Inherit(m.styles.Cell).Margin(m.styles.Cell.GetMargin()).Padding(m.styles.Cell.GetPadding())
// If active, maybe highlight?
if rows[i].IsActive() {
if c, ok := m.common.Styles.Colors["active"]; ok && c != nil {
styles[i] = c.Inherit(m.styles.Cell).Margin(m.styles.Cell.GetMargin()).Padding(m.styles.Cell.GetPadding())
}
}
}
return styles
}
func (m *Model) parseColumns(cols []Column) []Column {
if len(cols) == 0 {
return cols
}
for i, col := range cols {
for _, interval := range m.rows {
col.ContentWidth = max(col.ContentWidth, lipgloss.Width(interval.GetString(col.Name)))
}
cols[i] = col
}
combinedSize := 0
nonZeroWidths := 0
tagIndex := -1
for i, col := range cols {
if col.ContentWidth > 0 {
col.Width = max(col.ContentWidth, lipgloss.Width(col.Title))
nonZeroWidths++
}
if !strings.Contains(col.Name, "tags") {
combinedSize += col.Width
} else {
tagIndex = i
}
cols[i] = col
}
if tagIndex >= 0 {
cols[tagIndex].Width = m.Width() - combinedSize - nonZeroWidths
}
return cols
}
// WithColumns sets the table columns (headers).
func WithColumns(cols []Column) Option {
return func(m *Model) {
m.cols = cols
}
}
// WithRows sets the table rows (data).
func WithIntervals(rows timewarrior.Intervals) Option {
return func(m *Model) {
m.rows = rows
}
}
// WithHeight sets the height of the table.
func WithHeight(h int) Option {
return func(m *Model) {
m.viewport.Height = h - lipgloss.Height(m.headersView())
}
}
// WithWidth sets the width of the table.
func WithWidth(w int) Option {
return func(m *Model) {
m.viewport.Width = w
}
}
// WithFocused sets the focus state of the table.
func WithFocused(f bool) Option {
return func(m *Model) {
m.focus = f
}
}
// WithStyles sets the table styles.
func WithStyles(s common.TableStyle) Option {
return func(m *Model) {
m.styles = s
}
}
// WithStyleFunc sets the table style func which can determine a cell style per column, row, and selected state.
func WithStyleFunc(f StyleFunc) Option {
return func(m *Model) {
m.styleFunc = f
}
}
// WithKeyMap sets the key map.
func WithKeyMap(km KeyMap) Option {
return func(m *Model) {
m.KeyMap = km
}
}
// Update is the Bubble Tea update loop.
func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) {
if !m.focus {
return m, nil
}
switch msg := msg.(type) {
case tea.KeyMsg:
switch {
case key.Matches(msg, m.KeyMap.LineUp):
m.MoveUp(1)
case key.Matches(msg, m.KeyMap.LineDown):
m.MoveDown(1)
case key.Matches(msg, m.KeyMap.PageUp):
m.MoveUp(m.viewport.Height)
case key.Matches(msg, m.KeyMap.PageDown):
m.MoveDown(m.viewport.Height)
case key.Matches(msg, m.KeyMap.HalfPageUp):
m.MoveUp(m.viewport.Height / 2)
case key.Matches(msg, m.KeyMap.HalfPageDown):
m.MoveDown(m.viewport.Height / 2)
case key.Matches(msg, m.KeyMap.LineDown):
m.MoveDown(1)
case key.Matches(msg, m.KeyMap.GotoTop):
m.GotoTop()
case key.Matches(msg, m.KeyMap.GotoBottom):
m.GotoBottom()
}
}
return m, nil
}
// Focused returns the focus state of the table.
func (m Model) Focused() bool {
return m.focus
}
// Focus focuses the table, allowing the user to move around the rows and
// interact.
func (m *Model) Focus() {
m.focus = true
m.UpdateViewport()
}
// Blur blurs the table, preventing selection or movement.
func (m *Model) Blur() {
m.focus = false
m.UpdateViewport()
}
// View renders the component.
func (m Model) View() string {
return m.headersView() + "\n" + m.viewport.View()
}
// UpdateViewport updates the list content based on the previously defined
// columns and rows.
func (m *Model) UpdateViewport() {
renderedRows := make([]string, 0, len(m.rows))
if m.cursor >= 0 {
m.start = clamp(m.cursor-m.viewport.Height, 0, m.cursor)
} else {
m.start = 0
}
m.end = clamp(m.cursor+m.viewport.Height, m.cursor, len(m.rows))
for i := m.start; i < m.end; i++ {
renderedRows = append(renderedRows, m.renderRow(i))
}
m.viewport.SetContent(
lipgloss.JoinVertical(lipgloss.Left, renderedRows...),
)
}
// SelectedRow returns the selected row.
func (m Model) SelectedRow() Row {
if m.cursor < 0 || m.cursor >= len(m.rows) {
return nil
}
return m.rows[m.cursor]
}
// Rows returns the current rows.
func (m Model) Rows() timewarrior.Intervals {
return m.rows
}
// Columns returns the current columns.
func (m Model) Columns() []Column {
return m.cols
}
// SetRows sets a new rows state.
func (m *Model) SetRows(r timewarrior.Intervals) {
m.rows = r
m.UpdateViewport()
}
// SetColumns sets a new columns state.
func (m *Model) SetColumns(c []Column) {
m.cols = c
m.UpdateViewport()
}
// SetWidth sets the width of the viewport of the table.
func (m *Model) SetWidth(w int) {
m.viewport.Width = w
m.UpdateViewport()
}
// SetHeight sets the height of the viewport of the table.
func (m *Model) SetHeight(h int) {
m.viewport.Height = h - lipgloss.Height(m.headersView())
m.UpdateViewport()
}
// Height returns the viewport height of the table.
func (m Model) Height() int {
return m.viewport.Height
}
// Width returns the viewport width of the table.
func (m Model) Width() int {
return m.viewport.Width
}
// Cursor returns the index of the selected row.
func (m Model) Cursor() int {
return m.cursor
}
// SetCursor sets the cursor position in the table.
func (m *Model) SetCursor(n int) {
m.cursor = clamp(n, 0, len(m.rows)-1)
m.UpdateViewport()
}
// MoveUp moves the selection up by any number of rows.
// It can not go above the first row.
func (m *Model) MoveUp(n int) {
m.cursor = clamp(m.cursor-n, 0, len(m.rows)-1)
switch {
case m.start == 0:
m.viewport.SetYOffset(clamp(m.viewport.YOffset, 0, m.cursor))
case m.start < m.viewport.Height:
m.viewport.YOffset = (clamp(clamp(m.viewport.YOffset+n, 0, m.cursor), 0, m.viewport.Height))
case m.viewport.YOffset >= 1:
m.viewport.YOffset = clamp(m.viewport.YOffset+n, 1, m.viewport.Height)
}
m.UpdateViewport()
}
// MoveDown moves the selection down by any number of rows.
// It can not go below the last row.
func (m *Model) MoveDown(n int) {
m.cursor = clamp(m.cursor+n, 0, len(m.rows)-1)
m.UpdateViewport()
switch {
case m.end == len(m.rows) && m.viewport.YOffset > 0:
m.viewport.SetYOffset(clamp(m.viewport.YOffset-n, 1, m.viewport.Height))
case m.cursor > (m.end-m.start)/2 && m.viewport.YOffset > 0:
m.viewport.SetYOffset(clamp(m.viewport.YOffset-n, 1, m.cursor))
case m.viewport.YOffset > 1:
case m.cursor > m.viewport.YOffset+m.viewport.Height-1:
m.viewport.SetYOffset(clamp(m.viewport.YOffset+1, 0, 1))
}
}
// GotoTop moves the selection to the first row.
func (m *Model) GotoTop() {
m.MoveUp(m.cursor)
}
// GotoBottom moves the selection to the last row.
func (m *Model) GotoBottom() {
m.MoveDown(len(m.rows))
}
// StyleFunc is a function that can be used to customize the style of a table cell based on the row and column index.
type StyleFunc func(row, col int, value string) lipgloss.Style
func (m Model) headersView() string {
var s = make([]string, 0, len(m.cols))
for _, col := range m.cols {
if col.Width <= 0 {
continue
}
style := lipgloss.NewStyle().Width(col.Width).MaxWidth(col.Width).Inline(true)
renderedCell := style.Render(runewidth.Truncate(col.Title, col.Width, "…"))
s = append(s, m.styles.Header.Render(renderedCell))
}
return lipgloss.JoinHorizontal(lipgloss.Left, s...)
}
func (m *Model) renderRow(r int) string {
var s = make([]string, 0, len(m.cols))
for i, col := range m.cols {
if m.cols[i].Width <= 0 {
continue
}
var cellStyle lipgloss.Style
cellStyle = m.rowStyles[r]
if r == m.cursor {
cellStyle = cellStyle.Inherit(m.styles.Selected)
}
style := lipgloss.NewStyle().Width(m.cols[i].Width).MaxWidth(m.cols[i].Width).Inline(true)
renderedCell := cellStyle.Render(style.Render(runewidth.Truncate(m.rows[r].GetString(col.Name), m.cols[i].Width, "…")))
s = append(s, renderedCell)
}
row := lipgloss.JoinHorizontal(lipgloss.Left, s...)
if r == m.cursor {
return m.styles.Selected.Render(row)
}
return row
}
func max(a, b int) int {
if a > b {
return a
}
return b
}
func min(a, b int) int {
if a < b {
return a
}
return b
}
func clamp(v, low, high int) int {
return min(max(v, low), high)
}

17
main.go
View File

@ -10,6 +10,7 @@ import (
"tasksquire/common" "tasksquire/common"
"tasksquire/pages" "tasksquire/pages"
"tasksquire/taskwarrior" "tasksquire/taskwarrior"
"tasksquire/timewarrior"
tea "github.com/charmbracelet/bubbletea" tea "github.com/charmbracelet/bubbletea"
) )
@ -26,9 +27,23 @@ func main() {
log.Fatal("Unable to find taskrc file") log.Fatal("Unable to find taskrc file")
} }
var timewConfigPath string
if timewConfigEnv := os.Getenv("TIMEWARRIORDB"); timewConfigEnv != "" {
timewConfigPath = timewConfigEnv
} else if _, err := os.Stat(os.Getenv("HOME") + "/.timewarrior/timewarrior.cfg"); err == nil {
timewConfigPath = os.Getenv("HOME") + "/.timewarrior/timewarrior.cfg"
} else {
// Default to empty string if not found, let TimeSquire handle defaults or errors if necessary
// But TimeSquire seems to only take config location.
// Let's assume standard location if not found or pass empty if it auto-detects.
// Checking TimeSquire implementation: it calls `timew show` which usually works if timew is in path.
timewConfigPath = ""
}
ts := taskwarrior.NewTaskSquire(taskrcPath) ts := taskwarrior.NewTaskSquire(taskrcPath)
tws := timewarrior.NewTimeSquire(timewConfigPath)
ctx := context.Background() ctx := context.Background()
common := common.NewCommon(ctx, ts) common := common.NewCommon(ctx, ts, tws)
file, err := os.OpenFile("app.log", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644) file, err := os.OpenFile("app.log", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil { if err != nil {

View File

@ -4,18 +4,19 @@ import (
"log/slog" "log/slog"
"slices" "slices"
"tasksquire/common" "tasksquire/common"
"tasksquire/components/picker"
"tasksquire/taskwarrior" "tasksquire/taskwarrior"
"github.com/charmbracelet/bubbles/key" "github.com/charmbracelet/bubbles/key"
"github.com/charmbracelet/bubbles/list"
tea "github.com/charmbracelet/bubbletea" tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/huh"
"github.com/charmbracelet/lipgloss" "github.com/charmbracelet/lipgloss"
) )
type ContextPickerPage struct { type ContextPickerPage struct {
common *common.Common common *common.Common
contexts taskwarrior.Contexts contexts taskwarrior.Contexts
form *huh.Form picker *picker.Picker
} }
func NewContextPickerPage(common *common.Common) *ContextPickerPage { func NewContextPickerPage(common *common.Common) *ContextPickerPage {
@ -25,28 +26,36 @@ func NewContextPickerPage(common *common.Common) *ContextPickerPage {
} }
selected := common.TW.GetActiveContext().Name selected := common.TW.GetActiveContext().Name
options := make([]string, 0)
for _, c := range p.contexts { itemProvider := func() []list.Item {
if c.Name != "none" { contexts := common.TW.GetContexts()
options = append(options, c.Name) options := make([]string, 0)
for _, c := range contexts {
if c.Name != "none" {
options = append(options, c.Name)
}
} }
} slices.Sort(options)
slices.Sort(options) options = append([]string{"(none)"}, options...)
options = append([]string{"(none)"}, options...)
p.form = huh.NewForm( items := []list.Item{}
huh.NewGroup( for _, opt := range options {
huh.NewSelect[string](). items = append(items, picker.NewItem(opt))
Key("context"). }
Options(huh.NewOptions(options...)...). return items
Title("Contexts"). }
Description("Choose a context").
Value(&selected). onSelect := func(item list.Item) tea.Cmd {
WithTheme(common.Styles.Form), return func() tea.Msg { return contextSelectedMsg{item: item} }
), }
).
WithShowHelp(false). p.picker = picker.New(common, "Contexts", itemProvider, onSelect)
WithShowErrors(true)
// Set active context
if selected == "" {
selected = "(none)"
}
p.picker.SelectItemByFilterValue(selected)
p.SetSize(common.Width(), common.Height()) p.SetSize(common.Width(), common.Height())
@ -56,76 +65,80 @@ func NewContextPickerPage(common *common.Common) *ContextPickerPage {
func (p *ContextPickerPage) SetSize(width, height int) { func (p *ContextPickerPage) SetSize(width, height int) {
p.common.SetSize(width, height) p.common.SetSize(width, height)
if width >= 20 { // Set list size with some padding/limits to look like a picker
p.form = p.form.WithWidth(20) listWidth := width - 4
} else { if listWidth > 40 {
p.form = p.form.WithWidth(width) listWidth = 40
} }
listHeight := height - 6
if height >= 30 { if listHeight > 20 {
p.form = p.form.WithHeight(30) listHeight = 20
} else {
p.form = p.form.WithHeight(height)
} }
p.picker.SetSize(listWidth, listHeight)
} }
func (p *ContextPickerPage) Init() tea.Cmd { func (p *ContextPickerPage) Init() tea.Cmd {
return p.form.Init() return p.picker.Init()
}
type contextSelectedMsg struct {
item list.Item
} }
func (p *ContextPickerPage) Update(msg tea.Msg) (tea.Model, tea.Cmd) { func (p *ContextPickerPage) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
var cmds []tea.Cmd var cmd tea.Cmd
switch msg := msg.(type) { switch msg := msg.(type) {
case tea.WindowSizeMsg: case tea.WindowSizeMsg:
p.SetSize(msg.Width, msg.Height) p.SetSize(msg.Width, msg.Height)
case tea.KeyMsg: case contextSelectedMsg:
switch { name := msg.item.FilterValue() // Use FilterValue (which is the name/text)
case key.Matches(msg, p.common.Keymap.Back): if name == "(none)" {
model, err := p.common.PopPage() name = ""
if err != nil {
slog.Error("page stack empty")
return nil, tea.Quit
}
return model, BackCmd
} }
}
f, cmd := p.form.Update(msg) ctx := p.common.TW.GetContext(name)
if f, ok := f.(*huh.Form); ok {
p.form = f
cmds = append(cmds, cmd)
}
if p.form.State == huh.StateCompleted {
cmds = append(cmds, p.updateContextCmd)
model, err := p.common.PopPage() model, err := p.common.PopPage()
if err != nil { if err != nil {
slog.Error("page stack empty") slog.Error("page stack empty")
return nil, tea.Quit return nil, tea.Quit
} }
return model, tea.Batch(cmds...) return model, func() tea.Msg { return UpdateContextMsg(ctx) }
case tea.KeyMsg:
if !p.picker.IsFiltering() {
switch {
case key.Matches(msg, p.common.Keymap.Back):
model, err := p.common.PopPage()
if err != nil {
slog.Error("page stack empty")
return nil, tea.Quit
}
return model, BackCmd
}
}
} }
return p, tea.Batch(cmds...) _, cmd = p.picker.Update(msg)
return p, cmd
} }
func (p *ContextPickerPage) View() string { func (p *ContextPickerPage) View() string {
width := p.common.Width() - 4
if width > 40 {
width = 40
}
content := p.picker.View()
styledContent := lipgloss.NewStyle().Width(width).Render(content)
return lipgloss.Place( return lipgloss.Place(
p.common.Width(), p.common.Width(),
p.common.Height(), p.common.Height(),
lipgloss.Center, lipgloss.Center,
lipgloss.Center, lipgloss.Center,
p.common.Styles.Base.Render(p.form.View()), p.common.Styles.Base.Render(styledContent),
) )
} }
func (p *ContextPickerPage) updateContextCmd() tea.Msg { type UpdateContextMsg *taskwarrior.Context
context := p.form.GetString("context")
if context == "(none)" {
context = ""
}
return UpdateContextMsg(p.common.TW.GetContext(context))
}
type UpdateContextMsg *taskwarrior.Context

View File

@ -1,14 +1,18 @@
package pages package pages
import ( import (
tea "github.com/charmbracelet/bubbletea"
"tasksquire/common" "tasksquire/common"
"github.com/charmbracelet/bubbles/key"
tea "github.com/charmbracelet/bubbletea"
) )
type MainPage struct { type MainPage struct {
common *common.Common common *common.Common
activePage common.Component activePage common.Component
taskPage common.Component
timePage common.Component
} }
func NewMainPage(common *common.Common) *MainPage { func NewMainPage(common *common.Common) *MainPage {
@ -16,15 +20,16 @@ func NewMainPage(common *common.Common) *MainPage {
common: common, common: common,
} }
m.activePage = NewReportPage(common, common.TW.GetReport(common.TW.GetConfig().Get("uda.tasksquire.report.default"))) m.taskPage = NewReportPage(common, common.TW.GetReport(common.TW.GetConfig().Get("uda.tasksquire.report.default")))
// m.activePage = NewTaskEditorPage(common, taskwarrior.Task{}) m.timePage = NewTimePage(common)
m.activePage = m.taskPage
return m return m
} }
func (m *MainPage) Init() tea.Cmd { func (m *MainPage) Init() tea.Cmd {
return m.activePage.Init() return tea.Batch(m.taskPage.Init(), m.timePage.Init())
} }
func (m *MainPage) Update(msg tea.Msg) (tea.Model, tea.Cmd) { func (m *MainPage) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
@ -33,6 +38,19 @@ func (m *MainPage) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) { switch msg := msg.(type) {
case tea.WindowSizeMsg: case tea.WindowSizeMsg:
m.common.SetSize(msg.Width, msg.Height) m.common.SetSize(msg.Width, msg.Height)
case tea.KeyMsg:
if key.Matches(msg, m.common.Keymap.Next) {
if m.activePage == m.taskPage {
m.activePage = m.timePage
} else {
m.activePage = m.taskPage
}
// Re-size the new active page just in case
m.activePage.SetSize(m.common.Width(), m.common.Height())
// Trigger a refresh/init on switch? Maybe not needed if we keep state.
// But we might want to refresh data.
return m, m.activePage.Init()
}
} }
activePage, cmd := m.activePage.Update(msg) activePage, cmd := m.activePage.Update(msg)

View File

@ -3,16 +3,17 @@ package pages
import ( import (
"log/slog" "log/slog"
"tasksquire/common" "tasksquire/common"
"tasksquire/components/picker"
"github.com/charmbracelet/bubbles/key" "github.com/charmbracelet/bubbles/key"
"github.com/charmbracelet/bubbles/list"
tea "github.com/charmbracelet/bubbletea" tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/huh"
"github.com/charmbracelet/lipgloss" "github.com/charmbracelet/lipgloss"
) )
type ProjectPickerPage struct { type ProjectPickerPage struct {
common *common.Common common *common.Common
form *huh.Form picker *picker.Picker
} }
func NewProjectPickerPage(common *common.Common, activeProject string) *ProjectPickerPage { func NewProjectPickerPage(common *common.Common, activeProject string) *ProjectPickerPage {
@ -20,30 +21,31 @@ func NewProjectPickerPage(common *common.Common, activeProject string) *ProjectP
common: common, common: common,
} }
var selected string itemProvider := func() []list.Item {
if activeProject == "" { projects := common.TW.GetProjects()
selected = "(none)" items := []list.Item{picker.NewItem("(none)")}
} else { for _, proj := range projects {
selected = activeProject items = append(items, picker.NewItem(proj))
}
return items
} }
projects := common.TW.GetProjects() onSelect := func(item list.Item) tea.Cmd {
options := []string{"(none)"} return func() tea.Msg { return projectSelectedMsg{item: item} }
options = append(options, projects...) }
p.form = huh.NewForm( // onCreate := func(name string) tea.Cmd {
huh.NewGroup( // return func() tea.Msg { return projectSelectedMsg{item: picker.NewItem(name)} }
huh.NewSelect[string](). // }
Key("project").
Options(huh.NewOptions(options...)...). // p.picker = picker.New(common, "Projects", itemProvider, onSelect, picker.WithOnCreate(onCreate))
Title("Projects"). p.picker = picker.New(common, "Projects", itemProvider, onSelect)
Description("Choose a project").
Value(&selected). // Set active project
WithTheme(common.Styles.Form), if activeProject == "" {
), activeProject = "(none)"
). }
WithShowHelp(false). p.picker.SelectItemByFilterValue(activeProject)
WithShowErrors(false)
p.SetSize(common.Width(), common.Height()) p.SetSize(common.Width(), common.Height())
@ -53,76 +55,82 @@ func NewProjectPickerPage(common *common.Common, activeProject string) *ProjectP
func (p *ProjectPickerPage) SetSize(width, height int) { func (p *ProjectPickerPage) SetSize(width, height int) {
p.common.SetSize(width, height) p.common.SetSize(width, height)
if width >= 20 { // Set list size with some padding/limits to look like a picker
p.form = p.form.WithWidth(20) listWidth := width - 4
} else { if listWidth > 40 {
p.form = p.form.WithWidth(width) listWidth = 40
} }
listHeight := height - 6
if height >= 30 { if listHeight > 20 {
p.form = p.form.WithHeight(30) listHeight = 20
} else {
p.form = p.form.WithHeight(height)
} }
p.picker.SetSize(listWidth, listHeight)
} }
func (p *ProjectPickerPage) Init() tea.Cmd { func (p *ProjectPickerPage) Init() tea.Cmd {
return p.form.Init() return p.picker.Init()
}
type projectSelectedMsg struct {
item list.Item
} }
func (p *ProjectPickerPage) Update(msg tea.Msg) (tea.Model, tea.Cmd) { func (p *ProjectPickerPage) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
var cmds []tea.Cmd var cmd tea.Cmd
switch msg := msg.(type) { switch msg := msg.(type) {
case tea.WindowSizeMsg: case tea.WindowSizeMsg:
p.SetSize(msg.Width, msg.Height) p.SetSize(msg.Width, msg.Height)
case tea.KeyMsg: case projectSelectedMsg:
switch { proj := msg.item.FilterValue() // Use FilterValue (text)
case key.Matches(msg, p.common.Keymap.Back): if proj == "(none)" {
model, err := p.common.PopPage() proj = ""
if err != nil {
slog.Error("page stack empty")
return nil, tea.Quit
}
return model, BackCmd
} }
}
f, cmd := p.form.Update(msg)
if f, ok := f.(*huh.Form); ok {
p.form = f
cmds = append(cmds, cmd)
}
if p.form.State == huh.StateCompleted {
cmds = append(cmds, p.updateProjectCmd)
model, err := p.common.PopPage() model, err := p.common.PopPage()
if err != nil { if err != nil {
slog.Error("page stack empty") slog.Error("page stack empty")
return nil, tea.Quit return nil, tea.Quit
} }
return model, tea.Batch(cmds...) return model, func() tea.Msg { return UpdateProjectMsg(proj) }
case tea.KeyMsg:
if !p.picker.IsFiltering() {
switch {
case key.Matches(msg, p.common.Keymap.Back):
model, err := p.common.PopPage()
if err != nil {
slog.Error("page stack empty")
return nil, tea.Quit
}
return model, BackCmd
}
}
} }
return p, tea.Batch(cmds...) _, cmd = p.picker.Update(msg)
return p, cmd
} }
func (p *ProjectPickerPage) View() string { func (p *ProjectPickerPage) View() string {
width := p.common.Width() - 4
if width > 40 {
width = 40
}
content := p.picker.View()
styledContent := lipgloss.NewStyle().Width(width).Render(content)
return lipgloss.Place( return lipgloss.Place(
p.common.Width(), p.common.Width(),
p.common.Height(), p.common.Height(),
lipgloss.Center, lipgloss.Center,
lipgloss.Center, lipgloss.Center,
p.common.Styles.Base.Render(p.form.View()), p.common.Styles.Base.Render(styledContent),
) )
} }
func (p *ProjectPickerPage) updateProjectCmd() tea.Msg { func (p *ProjectPickerPage) updateProjectCmd() tea.Msg {
project := p.form.GetString("project") return nil
if project == "(none)" {
project = ""
}
return UpdateProjectMsg(project)
} }
type UpdateProjectMsg string type UpdateProjectMsg string

View File

@ -94,24 +94,24 @@ func (p *ReportPage) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
return p, tea.Quit return p, tea.Quit
case key.Matches(msg, p.common.Keymap.SetReport): case key.Matches(msg, p.common.Keymap.SetReport):
p.subpage = NewReportPickerPage(p.common, p.activeReport) p.subpage = NewReportPickerPage(p.common, p.activeReport)
p.subpage.Init() cmd := p.subpage.Init()
p.common.PushPage(p) p.common.PushPage(p)
return p.subpage, nil return p.subpage, cmd
case key.Matches(msg, p.common.Keymap.SetContext): case key.Matches(msg, p.common.Keymap.SetContext):
p.subpage = NewContextPickerPage(p.common) p.subpage = NewContextPickerPage(p.common)
p.subpage.Init() cmd := p.subpage.Init()
p.common.PushPage(p) p.common.PushPage(p)
return p.subpage, nil return p.subpage, cmd
case key.Matches(msg, p.common.Keymap.Add): case key.Matches(msg, p.common.Keymap.Add):
p.subpage = NewTaskEditorPage(p.common, taskwarrior.NewTask()) p.subpage = NewTaskEditorPage(p.common, taskwarrior.NewTask())
p.subpage.Init() cmd := p.subpage.Init()
p.common.PushPage(p) p.common.PushPage(p)
return p.subpage, nil return p.subpage, cmd
case key.Matches(msg, p.common.Keymap.Edit): case key.Matches(msg, p.common.Keymap.Edit):
p.subpage = NewTaskEditorPage(p.common, *p.selectedTask) p.subpage = NewTaskEditorPage(p.common, *p.selectedTask)
p.subpage.Init() cmd := p.subpage.Init()
p.common.PushPage(p) p.common.PushPage(p)
return p.subpage, nil return p.subpage, cmd
case key.Matches(msg, p.common.Keymap.Ok): case key.Matches(msg, p.common.Keymap.Ok):
p.common.TW.SetTaskDone(p.selectedTask) p.common.TW.SetTaskDone(p.selectedTask)
return p, p.getTasks() return p, p.getTasks()
@ -120,9 +120,9 @@ func (p *ReportPage) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
return p, p.getTasks() return p, p.getTasks()
case key.Matches(msg, p.common.Keymap.SetProject): case key.Matches(msg, p.common.Keymap.SetProject):
p.subpage = NewProjectPickerPage(p.common, p.activeProject) p.subpage = NewProjectPickerPage(p.common, p.activeProject)
p.subpage.Init() cmd := p.subpage.Init()
p.common.PushPage(p) p.common.PushPage(p)
return p.subpage, nil return p.subpage, cmd
case key.Matches(msg, p.common.Keymap.Tag): case key.Matches(msg, p.common.Keymap.Tag):
if p.selectedTask != nil { if p.selectedTask != nil {
tag := p.common.TW.GetConfig().Get("uda.tasksquire.tag.default") tag := p.common.TW.GetConfig().Get("uda.tasksquire.tag.default")

View File

@ -4,18 +4,19 @@ import (
"log/slog" "log/slog"
"slices" "slices"
"tasksquire/common" "tasksquire/common"
"tasksquire/components/picker"
"tasksquire/taskwarrior" "tasksquire/taskwarrior"
"github.com/charmbracelet/bubbles/key" "github.com/charmbracelet/bubbles/key"
"github.com/charmbracelet/bubbles/list"
tea "github.com/charmbracelet/bubbletea" tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/huh"
"github.com/charmbracelet/lipgloss" "github.com/charmbracelet/lipgloss"
) )
type ReportPickerPage struct { type ReportPickerPage struct {
common *common.Common common *common.Common
reports taskwarrior.Reports reports taskwarrior.Reports
form *huh.Form picker *picker.Picker
} }
func NewReportPickerPage(common *common.Common, activeReport *taskwarrior.Report) *ReportPickerPage { func NewReportPickerPage(common *common.Common, activeReport *taskwarrior.Report) *ReportPickerPage {
@ -24,27 +25,29 @@ func NewReportPickerPage(common *common.Common, activeReport *taskwarrior.Report
reports: common.TW.GetReports(), reports: common.TW.GetReports(),
} }
selected := activeReport.Name itemProvider := func() []list.Item {
options := make([]string, 0)
for _, r := range p.reports {
options = append(options, r.Name)
}
slices.Sort(options)
options := make([]string, 0) items := []list.Item{}
for _, r := range p.reports { for _, opt := range options {
options = append(options, r.Name) items = append(items, picker.NewItem(opt))
}
return items
} }
slices.Sort(options)
p.form = huh.NewForm( onSelect := func(item list.Item) tea.Cmd {
huh.NewGroup( return func() tea.Msg { return reportSelectedMsg{item: item} }
huh.NewSelect[string](). }
Key("report").
Options(huh.NewOptions(options...)...). p.picker = picker.New(common, "Reports", itemProvider, onSelect)
Title("Reports").
Description("Choose a report"). if activeReport != nil {
Value(&selected). p.picker.SelectItemByFilterValue(activeReport.Name)
WithTheme(common.Styles.Form), }
),
).
WithShowHelp(false).
WithShowErrors(false)
p.SetSize(common.Width(), common.Height()) p.SetSize(common.Width(), common.Height())
@ -54,72 +57,76 @@ func NewReportPickerPage(common *common.Common, activeReport *taskwarrior.Report
func (p *ReportPickerPage) SetSize(width, height int) { func (p *ReportPickerPage) SetSize(width, height int) {
p.common.SetSize(width, height) p.common.SetSize(width, height)
if width >= 20 { // Set list size with some padding/limits to look like a picker
p.form = p.form.WithWidth(20) listWidth := width - 4
} else { if listWidth > 40 {
p.form = p.form.WithWidth(width) listWidth = 40
} }
listHeight := height - 6
if height >= 30 { if listHeight > 20 {
p.form = p.form.WithHeight(30) listHeight = 20
} else {
p.form = p.form.WithHeight(height)
} }
p.picker.SetSize(listWidth, listHeight)
} }
func (p *ReportPickerPage) Init() tea.Cmd { func (p *ReportPickerPage) Init() tea.Cmd {
return p.form.Init() return p.picker.Init()
}
type reportSelectedMsg struct {
item list.Item
} }
func (p *ReportPickerPage) Update(msg tea.Msg) (tea.Model, tea.Cmd) { func (p *ReportPickerPage) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
var cmds []tea.Cmd var cmd tea.Cmd
switch msg := msg.(type) { switch msg := msg.(type) {
case tea.WindowSizeMsg: case tea.WindowSizeMsg:
p.SetSize(msg.Width, msg.Height) p.SetSize(msg.Width, msg.Height)
case tea.KeyMsg: case reportSelectedMsg:
switch { reportName := msg.item.FilterValue()
case key.Matches(msg, p.common.Keymap.Back): report := p.common.TW.GetReport(reportName)
model, err := p.common.PopPage()
if err != nil {
slog.Error("page stack empty")
return nil, tea.Quit
}
return model, BackCmd
}
}
f, cmd := p.form.Update(msg)
if f, ok := f.(*huh.Form); ok {
p.form = f
cmds = append(cmds, cmd)
}
if p.form.State == huh.StateCompleted {
cmds = append(cmds, []tea.Cmd{BackCmd, p.updateReportCmd}...)
model, err := p.common.PopPage() model, err := p.common.PopPage()
if err != nil { if err != nil {
slog.Error("page stack empty") slog.Error("page stack empty")
return nil, tea.Quit return nil, tea.Quit
} }
return model, tea.Batch(cmds...) return model, func() tea.Msg { return UpdateReportMsg(report) }
case tea.KeyMsg:
if !p.picker.IsFiltering() {
switch {
case key.Matches(msg, p.common.Keymap.Back):
model, err := p.common.PopPage()
if err != nil {
slog.Error("page stack empty")
return nil, tea.Quit
}
return model, BackCmd
}
}
} }
return p, tea.Batch(cmds...) _, cmd = p.picker.Update(msg)
return p, cmd
} }
func (p *ReportPickerPage) View() string { func (p *ReportPickerPage) View() string {
width := p.common.Width() - 4
if width > 40 {
width = 40
}
content := p.picker.View()
styledContent := lipgloss.NewStyle().Width(width).Render(content)
return lipgloss.Place( return lipgloss.Place(
p.common.Width(), p.common.Width(),
p.common.Height(), p.common.Height(),
lipgloss.Center, lipgloss.Center,
lipgloss.Center, lipgloss.Center,
p.common.Styles.Base.Render(p.form.View()), p.common.Styles.Base.Render(styledContent),
) )
} }
func (p *ReportPickerPage) updateReportCmd() tea.Msg { type UpdateReportMsg *taskwarrior.Report
return UpdateReportMsg(p.common.TW.GetReport(p.form.GetString("report")))
}
type UpdateReportMsg *taskwarrior.Report

168
pages/timeEditor.go Normal file
View File

@ -0,0 +1,168 @@
package pages
import (
"log/slog"
"strings"
"tasksquire/common"
"tasksquire/timewarrior"
"github.com/charmbracelet/bubbles/key"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/huh"
)
type TimeEditorPage struct {
common *common.Common
interval *timewarrior.Interval
form *huh.Form
startStr string
endStr string
tagsStr string
}
func NewTimeEditorPage(com *common.Common, interval *timewarrior.Interval) *TimeEditorPage {
p := &TimeEditorPage{
common: com,
interval: interval,
startStr: interval.Start,
endStr: interval.End,
tagsStr: formatTags(interval.Tags),
}
p.form = huh.NewForm(
huh.NewGroup(
huh.NewInput().
Title("Start").
Value(&p.startStr).
Validate(func(s string) error {
return timewarrior.ValidateDate(s)
}),
huh.NewInput().
Title("End").
Value(&p.endStr).
Validate(func(s string) error {
if s == "" {
return nil // End can be empty (active)
}
return timewarrior.ValidateDate(s)
}),
huh.NewInput().
Title("Tags").
Value(&p.tagsStr).
Description("Space separated, use \"\" for tags with spaces"),
),
).WithTheme(com.Styles.Form)
return p
}
func (p *TimeEditorPage) Init() tea.Cmd {
return p.form.Init()
}
func (p *TimeEditorPage) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
var cmds []tea.Cmd
switch msg := msg.(type) {
case tea.KeyMsg:
if key.Matches(msg, p.common.Keymap.Back) {
model, err := p.common.PopPage()
if err != nil {
slog.Error("page stack empty")
return nil, tea.Quit
}
return model, BackCmd
}
case tea.WindowSizeMsg:
p.SetSize(msg.Width, msg.Height)
}
form, cmd := p.form.Update(msg)
if f, ok := form.(*huh.Form); ok {
p.form = f
}
cmds = append(cmds, cmd)
if p.form.State == huh.StateCompleted {
p.saveInterval()
model, err := p.common.PopPage()
if err != nil {
slog.Error("page stack empty")
return nil, tea.Quit
}
// Return with a command to refresh the intervals
return model, tea.Batch(tea.Batch(cmds...), refreshIntervals)
}
return p, tea.Batch(cmds...)
}
func (p *TimeEditorPage) View() string {
return p.form.View()
}
func (p *TimeEditorPage) SetSize(width, height int) {
p.common.SetSize(width, height)
}
func (p *TimeEditorPage) saveInterval() {
// If it's an existing interval (has ID), delete it first so we can replace it with the modified version
if p.interval.ID != 0 {
err := p.common.TimeW.DeleteInterval(p.interval.ID)
if err != nil {
slog.Error("Failed to delete old interval during edit", "err", err)
// Proceeding to import anyway, attempting to save user data
}
}
p.interval.Start = p.startStr
p.interval.End = p.endStr
// Parse tags
p.interval.Tags = parseTags(p.tagsStr)
err := p.common.TimeW.ModifyInterval(p.interval)
if err != nil {
slog.Error("Failed to modify interval", "err", err)
}
}
func parseTags(tagsStr string) []string {
var tags []string
var current strings.Builder
inQuotes := false
for _, r := range tagsStr {
switch {
case r == '"':
inQuotes = !inQuotes
case r == ' ' && !inQuotes:
if current.Len() > 0 {
tags = append(tags, current.String())
current.Reset()
}
default:
current.WriteRune(r)
}
}
if current.Len() > 0 {
tags = append(tags, current.String())
}
return tags
}
func formatTags(tags []string) string {
var formatted []string
for _, t := range tags {
if strings.Contains(t, " ") {
formatted = append(formatted, "\""+t+"\"")
} else {
formatted = append(formatted, t)
}
}
return strings.Join(formatted, " ")
}

185
pages/timePage.go Normal file
View File

@ -0,0 +1,185 @@
package pages
import (
"time"
"tasksquire/common"
"tasksquire/components/timetable"
"tasksquire/timewarrior"
"github.com/charmbracelet/bubbles/key"
tea "github.com/charmbracelet/bubbletea"
)
type TimePage struct {
common *common.Common
intervals timetable.Model
data timewarrior.Intervals
shouldSelectActive bool
}
func NewTimePage(com *common.Common) *TimePage {
p := &TimePage{
common: com,
}
p.populateTable(timewarrior.Intervals{})
return p
}
func (p *TimePage) Init() tea.Cmd {
return tea.Batch(p.getIntervals(), doTick())
}
func (p *TimePage) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
var cmds []tea.Cmd
switch msg := msg.(type) {
case tea.WindowSizeMsg:
p.SetSize(msg.Width, msg.Height)
case intervalsMsg:
p.data = timewarrior.Intervals(msg)
p.populateTable(p.data)
case RefreshIntervalsMsg:
cmds = append(cmds, p.getIntervals())
case tickMsg:
cmds = append(cmds, p.getIntervals())
cmds = append(cmds, doTick())
case tea.KeyMsg:
switch {
case key.Matches(msg, p.common.Keymap.Quit):
return p, tea.Quit
case key.Matches(msg, p.common.Keymap.StartStop):
row := p.intervals.SelectedRow()
if row != nil {
interval := (*timewarrior.Interval)(row)
if interval.IsActive() {
p.common.TimeW.StopTracking()
} else {
p.common.TimeW.ContinueInterval(interval.ID)
p.shouldSelectActive = true
}
return p, tea.Batch(p.getIntervals(), doTick())
}
case key.Matches(msg, p.common.Keymap.Delete):
row := p.intervals.SelectedRow()
if row != nil {
interval := (*timewarrior.Interval)(row)
p.common.TimeW.DeleteInterval(interval.ID)
return p, tea.Batch(p.getIntervals(), doTick())
}
case key.Matches(msg, p.common.Keymap.Edit):
row := p.intervals.SelectedRow()
if row != nil {
interval := (*timewarrior.Interval)(row)
editor := NewTimeEditorPage(p.common, interval)
p.common.PushPage(p)
return editor, editor.Init()
}
case key.Matches(msg, p.common.Keymap.Add):
interval := timewarrior.NewInterval()
interval.Start = time.Now().UTC().Format("20060102T150405Z")
editor := NewTimeEditorPage(p.common, interval)
p.common.PushPage(p)
return editor, editor.Init()
}
}
var cmd tea.Cmd
p.intervals, cmd = p.intervals.Update(msg)
cmds = append(cmds, cmd)
return p, tea.Batch(cmds...)
}
type RefreshIntervalsMsg struct{}
func refreshIntervals() tea.Msg {
return RefreshIntervalsMsg{}
}
func (p *TimePage) View() string {
if len(p.data) == 0 {
return p.common.Styles.Base.Render("No intervals found for today")
}
return p.intervals.View()
}
func (p *TimePage) SetSize(width int, height int) {
p.common.SetSize(width, height)
p.intervals.SetWidth(width - p.common.Styles.Base.GetHorizontalFrameSize())
p.intervals.SetHeight(height - p.common.Styles.Base.GetVerticalFrameSize())
}
func (p *TimePage) populateTable(intervals timewarrior.Intervals) {
var selectedStart string
currentIdx := p.intervals.Cursor()
if row := p.intervals.SelectedRow(); row != nil {
selectedStart = row.Start
}
columns := []timetable.Column{
{Title: "ID", Name: "id", Width: 4},
{Title: "Start", Name: "start", Width: 16},
{Title: "End", Name: "end", Width: 16},
{Title: "Duration", Name: "duration", Width: 10},
{Title: "Tags", Name: "tags", Width: 0}, // flexible width
}
p.intervals = timetable.New(
p.common,
timetable.WithColumns(columns),
timetable.WithIntervals(intervals),
timetable.WithFocused(true),
timetable.WithWidth(p.common.Width()-p.common.Styles.Base.GetVerticalFrameSize()),
timetable.WithHeight(p.common.Height()-p.common.Styles.Base.GetHorizontalFrameSize()),
timetable.WithStyles(p.common.Styles.TableStyle),
)
if len(intervals) > 0 {
newIdx := -1
if p.shouldSelectActive {
for i, interval := range intervals {
if interval.IsActive() {
newIdx = i
break
}
}
p.shouldSelectActive = false
}
if newIdx == -1 && selectedStart != "" {
for i, interval := range intervals {
if interval.Start == selectedStart {
newIdx = i
break
}
}
}
if newIdx == -1 {
newIdx = currentIdx
}
if newIdx >= len(intervals) {
newIdx = len(intervals) - 1
}
if newIdx < 0 {
newIdx = 0
}
p.intervals.SetCursor(newIdx)
}
}
type intervalsMsg timewarrior.Intervals
func (p *TimePage) getIntervals() tea.Cmd {
return func() tea.Msg {
// ":day" is a timewarrior hint for "today"
intervals := p.common.TimeW.GetIntervals(":day")
return intervalsMsg(intervals)
}
}

View File

@ -12,9 +12,10 @@ type TWConfig struct {
var ( var (
defaultConfig = map[string]string{ defaultConfig = map[string]string{
"uda.tasksquire.report.default": "next", "uda.tasksquire.report.default": "next",
"uda.tasksquire.tag.default": "next", "uda.tasksquire.tag.default": "next",
"uda.tasksquire.tags.default": "low_energy,customer,delegate,code,communication,research", "uda.tasksquire.tags.default": "mngmnt,ops,low_energy,cust,delegate,code,comm,research",
"uda.tasksquire.picker.filter_by_default": "yes",
} }
) )

78
timewarrior/config.go Normal file
View File

@ -0,0 +1,78 @@
package timewarrior
import (
"fmt"
"log/slog"
"strings"
)
type TWConfig struct {
config map[string]string
}
var (
defaultConfig = map[string]string{
"uda.timesquire.default.tag": "",
}
)
func NewConfig(config []string) *TWConfig {
cfg := parseConfig(config)
for key, value := range defaultConfig {
if _, ok := cfg[key]; !ok {
cfg[key] = value
}
}
return &TWConfig{
config: cfg,
}
}
func (tc *TWConfig) GetConfig() map[string]string {
return tc.config
}
func (tc *TWConfig) Get(key string) string {
if _, ok := tc.config[key]; !ok {
slog.Debug(fmt.Sprintf("Key not found in config: %s", key))
return ""
}
return tc.config[key]
}
func parseConfig(config []string) map[string]string {
configMap := make(map[string]string)
for _, line := range config {
// Skip empty lines and comments
line = strings.TrimSpace(line)
if line == "" || strings.HasPrefix(line, "#") {
continue
}
// Timewarrior config format: key = value or key: value
var key, value string
if strings.Contains(line, "=") {
parts := strings.SplitN(line, "=", 2)
if len(parts) == 2 {
key = strings.TrimSpace(parts[0])
value = strings.TrimSpace(parts[1])
}
} else if strings.Contains(line, ":") {
parts := strings.SplitN(line, ":", 2)
if len(parts) == 2 {
key = strings.TrimSpace(parts[0])
value = strings.TrimSpace(parts[1])
}
}
if key != "" {
configMap[key] = value
}
}
return configMap
}

278
timewarrior/models.go Normal file
View File

@ -0,0 +1,278 @@
package timewarrior
import (
"fmt"
"log/slog"
"math"
"strconv"
"strings"
"time"
)
const (
dtformat = "20060102T150405Z"
)
type Intervals []*Interval
type Interval struct {
ID int `json:"-"`
Start string `json:"start,omitempty"`
End string `json:"end,omitempty"`
Tags []string `json:"tags,omitempty"`
}
func NewInterval() *Interval {
return &Interval{
Tags: make([]string, 0),
}
}
func (i *Interval) GetString(field string) string {
switch field {
case "id":
return strconv.Itoa(i.ID)
case "start":
return formatDate(i.Start, "formatted")
case "end":
if i.End == "" {
return "now"
}
return formatDate(i.End, "formatted")
case "tags":
if len(i.Tags) == 0 {
return ""
}
return strings.Join(i.Tags, " ")
case "duration":
return i.GetDuration()
case "active":
if i.End == "" {
return "●"
}
return ""
default:
slog.Error(fmt.Sprintf("Field not implemented: %s", field))
return ""
}
}
func (i *Interval) GetDuration() string {
start, err := time.Parse(dtformat, i.Start)
if err != nil {
slog.Error("Failed to parse start time:", err)
return ""
}
var end time.Time
if i.End == "" {
end = time.Now()
} else {
end, err = time.Parse(dtformat, i.End)
if err != nil {
slog.Error("Failed to parse end time:", err)
return ""
}
}
duration := end.Sub(start)
return formatDuration(duration)
}
func (i *Interval) GetStartTime() time.Time {
dt, err := time.Parse(dtformat, i.Start)
if err != nil {
slog.Error("Failed to parse time:", err)
return time.Time{}
}
return dt
}
func (i *Interval) GetEndTime() time.Time {
if i.End == "" {
return time.Now()
}
dt, err := time.Parse(dtformat, i.End)
if err != nil {
slog.Error("Failed to parse time:", err)
return time.Time{}
}
return dt
}
func (i *Interval) HasTag(tag string) bool {
for _, t := range i.Tags {
if t == tag {
return true
}
}
return false
}
func (i *Interval) AddTag(tag string) {
if !i.HasTag(tag) {
i.Tags = append(i.Tags, tag)
}
}
func (i *Interval) RemoveTag(tag string) {
for idx, t := range i.Tags {
if t == tag {
i.Tags = append(i.Tags[:idx], i.Tags[idx+1:]...)
return
}
}
}
func (i *Interval) IsActive() bool {
return i.End == ""
}
func formatDate(date string, format string) string {
if date == "" {
return ""
}
dt, err := time.Parse(dtformat, date)
if err != nil {
slog.Error("Failed to parse time:", err)
return ""
}
dt = dt.Local()
switch format {
case "formatted", "":
return dt.Format("2006-01-02 15:04")
case "time":
return dt.Format("15:04")
case "date":
return dt.Format("2006-01-02")
case "iso":
return dt.Format("2006-01-02T150405Z")
case "epoch":
return strconv.FormatInt(dt.Unix(), 10)
case "age":
return parseDurationVague(time.Since(dt))
case "relative":
return parseDurationVague(time.Until(dt))
default:
slog.Error(fmt.Sprintf("Date format not implemented: %s", format))
return ""
}
}
func formatDuration(d time.Duration) string {
hours := int(d.Hours())
minutes := int(d.Minutes()) % 60
seconds := int(d.Seconds()) % 60
if hours > 0 {
return fmt.Sprintf("%d:%02d:%02d", hours, minutes, seconds)
}
return fmt.Sprintf("%d:%02d", minutes, seconds)
}
func parseDurationVague(d time.Duration) string {
dur := d.Round(time.Second).Abs()
days := dur.Hours() / 24
var formatted string
if dur >= time.Hour*24*365 {
formatted = fmt.Sprintf("%.1fy", days/365)
} else if dur >= time.Hour*24*90 {
formatted = strconv.Itoa(int(math.Round(days/30))) + "mo"
} else if dur >= time.Hour*24*7 {
formatted = strconv.Itoa(int(math.Round(days/7))) + "w"
} else if dur >= time.Hour*24 {
formatted = strconv.Itoa(int(days)) + "d"
} else if dur >= time.Hour {
formatted = strconv.Itoa(int(dur.Round(time.Hour).Hours())) + "h"
} else if dur >= time.Minute {
formatted = strconv.Itoa(int(dur.Round(time.Minute).Minutes())) + "min"
} else if dur >= time.Second {
formatted = strconv.Itoa(int(dur.Round(time.Second).Seconds())) + "s"
}
if d < 0 {
formatted = "-" + formatted
}
return formatted
}
var (
dateFormats = []string{
"2006-01-02",
"2006-01-02T15:04",
"20060102T150405Z",
}
specialDateFormats = []string{
"",
"now",
"today",
"yesterday",
"tomorrow",
"monday",
"tuesday",
"wednesday",
"thursday",
"friday",
"saturday",
"sunday",
"mon",
"tue",
"wed",
"thu",
"fri",
"sat",
"sun",
}
)
func ValidateDate(s string) error {
for _, f := range dateFormats {
if _, err := time.Parse(f, s); err == nil {
return nil
}
}
for _, f := range specialDateFormats {
if s == f {
return nil
}
}
return fmt.Errorf("invalid date")
}
func ValidateDuration(s string) error {
// TODO: implement proper duration validation
// Should accept formats like: 1h, 30m, 1h30m, etc.
return nil
}
// Summary represents time tracking summary data
type Summary struct {
Range string
TotalTime time.Duration
ByTag map[string]time.Duration
}
func (s *Summary) GetTotalString() string {
return formatDuration(s.TotalTime)
}
func (s *Summary) GetTagTime(tag string) string {
if duration, ok := s.ByTag[tag]; ok {
return formatDuration(duration)
}
return "0:00"
}

321
timewarrior/timewarrior.go Normal file
View File

@ -0,0 +1,321 @@
// TODO: error handling
// TODO: split combinedOutput and handle stderr differently
package timewarrior
import (
"bytes"
"encoding/json"
"fmt"
"log/slog"
"os/exec"
"regexp"
"slices"
"strings"
"sync"
)
const (
twBinary = "timew"
)
type TimeWarrior interface {
GetConfig() *TWConfig
GetTags() []string
GetIntervals(filter ...string) Intervals
StartTracking(tags []string) error
StopTracking() error
ContinueTracking() error
ContinueInterval(id int) error
CancelTracking() error
DeleteInterval(id int) error
ModifyInterval(interval *Interval) error
GetSummary(filter ...string) string
GetActive() *Interval
Undo()
}
type TimeSquire struct {
configLocation string
defaultArgs []string
config *TWConfig
mutex sync.Mutex
}
func NewTimeSquire(configLocation string) *TimeSquire {
if _, err := exec.LookPath(twBinary); err != nil {
slog.Error("Timewarrior not found")
return nil
}
ts := &TimeSquire{
configLocation: configLocation,
defaultArgs: []string{},
mutex: sync.Mutex{},
}
ts.config = ts.extractConfig()
return ts
}
func (ts *TimeSquire) GetConfig() *TWConfig {
ts.mutex.Lock()
defer ts.mutex.Unlock()
return ts.config
}
func (ts *TimeSquire) GetTags() []string {
ts.mutex.Lock()
defer ts.mutex.Unlock()
cmd := exec.Command(twBinary, append(ts.defaultArgs, []string{"tags"}...)...)
output, err := cmd.CombinedOutput()
if err != nil {
slog.Error("Failed getting tags:", err)
return nil
}
tags := make([]string, 0)
lines := strings.Split(string(output), "\n")
// Skip header lines and parse tag names
for i, line := range lines {
if i < 3 || line == "" {
continue
}
// Tags are space-separated, first column is the tag name
fields := strings.Fields(line)
if len(fields) > 0 {
tags = append(tags, fields[0])
}
}
slices.Sort(tags)
return tags
}
func (ts *TimeSquire) GetIntervals(filter ...string) Intervals {
ts.mutex.Lock()
defer ts.mutex.Unlock()
args := append(ts.defaultArgs, "export")
if filter != nil {
args = append(args, filter...)
}
cmd := exec.Command(twBinary, args...)
output, err := cmd.CombinedOutput()
if err != nil {
slog.Error("Failed getting intervals:", err)
return nil
}
intervals := make(Intervals, 0)
err = json.Unmarshal(output, &intervals)
if err != nil {
slog.Error("Failed unmarshalling intervals:", err)
return nil
}
// Reverse the intervals to show newest first
slices.Reverse(intervals)
// Assign IDs based on new order (newest is @1)
for i := range intervals {
intervals[i].ID = i + 1
}
return intervals
}
func (ts *TimeSquire) StartTracking(tags []string) error {
ts.mutex.Lock()
defer ts.mutex.Unlock()
if len(tags) == 0 {
return fmt.Errorf("at least one tag is required")
}
args := append(ts.defaultArgs, "start")
args = append(args, tags...)
cmd := exec.Command(twBinary, args...)
if err := cmd.Run(); err != nil {
slog.Error("Failed starting tracking:", err)
return err
}
return nil
}
func (ts *TimeSquire) StopTracking() error {
ts.mutex.Lock()
defer ts.mutex.Unlock()
cmd := exec.Command(twBinary, append(ts.defaultArgs, "stop")...)
if err := cmd.Run(); err != nil {
slog.Error("Failed stopping tracking:", err)
return err
}
return nil
}
func (ts *TimeSquire) ContinueTracking() error {
ts.mutex.Lock()
defer ts.mutex.Unlock()
cmd := exec.Command(twBinary, append(ts.defaultArgs, "continue")...)
if err := cmd.Run(); err != nil {
slog.Error("Failed continuing tracking:", err)
return err
}
return nil
}
func (ts *TimeSquire) ContinueInterval(id int) error {
ts.mutex.Lock()
defer ts.mutex.Unlock()
cmd := exec.Command(twBinary, append(ts.defaultArgs, []string{"continue", fmt.Sprintf("@%d", id)}...)...)
if err := cmd.Run(); err != nil {
slog.Error("Failed continuing interval:", err)
return err
}
return nil
}
func (ts *TimeSquire) CancelTracking() error {
ts.mutex.Lock()
defer ts.mutex.Unlock()
cmd := exec.Command(twBinary, append(ts.defaultArgs, "cancel")...)
if err := cmd.Run(); err != nil {
slog.Error("Failed canceling tracking:", err)
return err
}
return nil
}
func (ts *TimeSquire) DeleteInterval(id int) error {
ts.mutex.Lock()
defer ts.mutex.Unlock()
cmd := exec.Command(twBinary, append(ts.defaultArgs, []string{"delete", fmt.Sprintf("@%d", id)}...)...)
if err := cmd.Run(); err != nil {
slog.Error("Failed deleting interval:", err)
return err
}
return nil
}
func (ts *TimeSquire) ModifyInterval(interval *Interval) error {
ts.mutex.Lock()
defer ts.mutex.Unlock()
// Export the modified interval
intervals, err := json.Marshal(Intervals{interval})
if err != nil {
slog.Error("Failed marshalling interval:", err)
return err
}
// Import the modified interval
cmd := exec.Command(twBinary, append(ts.defaultArgs, []string{"import"}...)...)
cmd.Stdin = bytes.NewBuffer(intervals)
out, err := cmd.CombinedOutput()
if err != nil {
slog.Error("Failed modifying interval:", err, string(out))
return err
}
return nil
}
func (ts *TimeSquire) GetSummary(filter ...string) string {
ts.mutex.Lock()
defer ts.mutex.Unlock()
args := append(ts.defaultArgs, "summary")
if filter != nil {
args = append(args, filter...)
}
cmd := exec.Command(twBinary, args...)
output, err := cmd.CombinedOutput()
if err != nil {
slog.Error("Failed getting summary:", err)
return ""
}
return string(output)
}
func (ts *TimeSquire) GetActive() *Interval {
ts.mutex.Lock()
defer ts.mutex.Unlock()
cmd := exec.Command(twBinary, append(ts.defaultArgs, []string{"get", "dom.active"}...)...)
output, err := cmd.CombinedOutput()
if err != nil || string(output) == "0\n" {
return nil
}
// Get the active interval
intervals := ts.GetIntervals()
for _, interval := range intervals {
if interval.End == "" {
return interval
}
}
return nil
}
func (ts *TimeSquire) Undo() {
ts.mutex.Lock()
defer ts.mutex.Unlock()
cmd := exec.Command(twBinary, append(ts.defaultArgs, []string{"undo"}...)...)
err := cmd.Run()
if err != nil {
slog.Error("Failed undoing:", err)
}
}
func (ts *TimeSquire) extractConfig() *TWConfig {
cmd := exec.Command(twBinary, append(ts.defaultArgs, []string{"show"}...)...)
output, err := cmd.CombinedOutput()
if err != nil {
slog.Error("Failed getting config:", err)
return nil
}
return NewConfig(strings.Split(string(output), "\n"))
}
func extractTags(config string) []string {
re := regexp.MustCompile(`tag\.([^.]+)\.[^.]+`)
matches := re.FindAllStringSubmatch(config, -1)
uniques := make(map[string]struct{})
for _, match := range matches {
uniques[match[1]] = struct{}{}
}
var tags []string
for tag := range uniques {
tags = append(tags, tag)
}
slices.Sort(tags)
return tags
}