Compare commits
28 Commits
682c04ca79
...
0.0.6
| Author | SHA1 | Date | |
|---|---|---|---|
| c7798c5784 | |||
| 6116e89aac | |||
| e1df2dea3b | |||
| 9a318d3cff | |||
| c6d8fee715 | |||
| 34a94fa269 | |||
| 163e7abf47 | |||
| d51f409497 | |||
| e7fa789b2b | |||
| d7f0ac16d4 | |||
| f9fddf496a | |||
| f6a92bc99e | |||
| 1bf7aa8a66 | |||
| 4d3deaad32 | |||
| d05ed50116 | |||
| 0bd387caf7 | |||
| 0d027788f1 | |||
| 43845d28f1 | |||
| eb61fdb757 | |||
| 9cb0e7346b | |||
| a9cd39e9e8 | |||
| ddfbc89c5a | |||
| 5d1c24cab7 | |||
| 43e7417037 | |||
| 2581343b6b | |||
| 387a710bfd | |||
| 52947b898e | |||
| 0fbbba08d9 |
42
.gitea/workflows/ci.yaml
Normal file
42
.gitea/workflows/ci.yaml
Normal file
@@ -0,0 +1,42 @@
|
|||||||
|
name: CI
|
||||||
|
run-name: ${{ gitea.actor }} running CI
|
||||||
|
on: [push]
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
test:
|
||||||
|
runs-on: ubuntu-22.04
|
||||||
|
env:
|
||||||
|
RUNNER_TOOL_CACHE: /toolcache # Runner Tool Cache
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v3
|
||||||
|
- uses: actions/setup-go@v4
|
||||||
|
with:
|
||||||
|
go-version: '1.24.4' # The Go version to download (if necessary) and use.
|
||||||
|
- uses: https://gitea.com/actions/go-hashfiles@v0.0.1
|
||||||
|
id: hash-go
|
||||||
|
with:
|
||||||
|
patterns: |
|
||||||
|
go.mod
|
||||||
|
go.sum
|
||||||
|
- name: Echo hash
|
||||||
|
run: echo ${{ steps.hash-go.outputs.hash }}
|
||||||
|
- name: Cache go
|
||||||
|
id: cache-go
|
||||||
|
uses: https://github.com/actions/cache@v3 # Action cache
|
||||||
|
with: # specify with your GOMODCACHE and GOCACHE
|
||||||
|
path: |-
|
||||||
|
/root/go/pkg/mod
|
||||||
|
/root/.cache/go-build
|
||||||
|
key: go_cache-${{ steps.hash-go.outputs.hash }}
|
||||||
|
restore-keys: |-
|
||||||
|
go_cache-${{ steps.hash-go.outputs.hash }}
|
||||||
|
- run: go version
|
||||||
|
|
||||||
|
# unit and integration tests, coverage
|
||||||
|
- run: go test ./... -coverprofile cover.out
|
||||||
|
- run: go tool cover -html=cover.out -o cover.html
|
||||||
|
|
||||||
|
- uses: actions/upload-artifact@v3
|
||||||
|
with:
|
||||||
|
name: coverage.html
|
||||||
|
path: ./cover.html
|
||||||
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
dist/
|
||||||
|
.DS_Store
|
||||||
47
.goreleaser.yaml
Normal file
47
.goreleaser.yaml
Normal file
@@ -0,0 +1,47 @@
|
|||||||
|
# This is an example .goreleaser.yml file with some sensible defaults.
|
||||||
|
# Make sure to check the documentation at https://goreleaser.com
|
||||||
|
|
||||||
|
# The lines below are called `modelines`. See `:help modeline`
|
||||||
|
# Feel free to remove those if you don't want/need to use them.
|
||||||
|
# yaml-language-server: $schema=https://goreleaser.com/static/schema.json
|
||||||
|
# vim: set ts=2 sw=2 tw=0 fo=cnqoj
|
||||||
|
|
||||||
|
version: 2
|
||||||
|
|
||||||
|
before:
|
||||||
|
hooks:
|
||||||
|
- go mod tidy
|
||||||
|
|
||||||
|
builds:
|
||||||
|
- env:
|
||||||
|
- CGO_ENABLED=0
|
||||||
|
goos:
|
||||||
|
- linux
|
||||||
|
- windows
|
||||||
|
- darwin
|
||||||
|
|
||||||
|
archives:
|
||||||
|
- formats: [tar.gz]
|
||||||
|
# this name template makes the OS and Arch compatible with the results of `uname`.
|
||||||
|
name_template: >-
|
||||||
|
{{ .ProjectName }}_
|
||||||
|
{{- title .Os }}_
|
||||||
|
{{- if eq .Arch "amd64" }}x86_64
|
||||||
|
{{- else if eq .Arch "386" }}i386
|
||||||
|
{{- else }}{{ .Arch }}{{ end }}
|
||||||
|
{{- if .Arm }}v{{ .Arm }}{{ end }}
|
||||||
|
# use zip for windows archives
|
||||||
|
format_overrides:
|
||||||
|
- goos: windows
|
||||||
|
formats: [zip]
|
||||||
|
|
||||||
|
gitea_urls:
|
||||||
|
api: https://code.ppl.town/api/v1
|
||||||
|
download: https://code.ppl.town
|
||||||
|
|
||||||
|
release:
|
||||||
|
footer: >-
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
Released by [GoReleaser](https://github.com/goreleaser/goreleaser).
|
||||||
6
.vscode/settings.json
vendored
Normal file
6
.vscode/settings.json
vendored
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
{
|
||||||
|
"cSpell.ignoreWords": [
|
||||||
|
"FORMENTRY",
|
||||||
|
"oday"
|
||||||
|
]
|
||||||
|
}
|
||||||
23
README.md
Normal file
23
README.md
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
# charmite
|
||||||
|
|
||||||
|
Charmite is a simple TUI to add/view time entries in the [Mite](https://mite.de/en/)
|
||||||
|
time tracking tool.
|
||||||
|
|
||||||
|
## Installation
|
||||||
|
|
||||||
|
Download the appropriate binary from https://code.ppl.town/justin/charmite/releases
|
||||||
|
|
||||||
|
Or compile from the source code with:
|
||||||
|
|
||||||
|
go build .
|
||||||
|
|
||||||
|
## Using
|
||||||
|
|
||||||
|
You'll need to set the environment variables:
|
||||||
|
|
||||||
|
* MITE_API
|
||||||
|
* MITE_DOMAIN
|
||||||
|
|
||||||
|
To appropriate values.
|
||||||
|
|
||||||
|
See the [Mite FAQ](https://mite.de/en/faq.html) under "How do I allow API access for my account? Where can I get an API key?" for details on how to obtain your API key.
|
||||||
10
build.sh
10
build.sh
@@ -1,10 +0,0 @@
|
|||||||
#!/bin/sh
|
|
||||||
|
|
||||||
GOOS=linux GOARCH=amd64 go build -o cal_linux_amd64
|
|
||||||
GOOS=darwin GOARCH=arm64 go build -o cal_darwin_arm64
|
|
||||||
GOOS=windows GOARCH=amd64 go build -o cal_windows_lol_amd64
|
|
||||||
|
|
||||||
zip cal.zip cal_linux_amd64 cal_darwin_arm64 cal_windows_lol_amd64
|
|
||||||
|
|
||||||
|
|
||||||
open .
|
|
||||||
8
go.mod
8
go.mod
@@ -2,10 +2,7 @@ module charmcal
|
|||||||
|
|
||||||
go 1.24.1
|
go 1.24.1
|
||||||
|
|
||||||
require (
|
require github.com/charmbracelet/bubbles v0.21.0
|
||||||
github.com/charmbracelet/bubbles v0.21.0
|
|
||||||
github.com/stretchr/testify v1.10.0
|
|
||||||
)
|
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/atotto/clipboard v0.1.4 // indirect
|
github.com/atotto/clipboard v0.1.4 // indirect
|
||||||
@@ -36,8 +33,5 @@ require (
|
|||||||
github.com/charmbracelet/bubbletea v1.3.5
|
github.com/charmbracelet/bubbletea v1.3.5
|
||||||
github.com/charmbracelet/huh v0.7.0
|
github.com/charmbracelet/huh v0.7.0
|
||||||
github.com/charmbracelet/lipgloss v1.1.0
|
github.com/charmbracelet/lipgloss v1.1.0
|
||||||
github.com/davecgh/go-spew v1.1.1 // indirect
|
|
||||||
github.com/dustin/go-humanize v1.0.1
|
github.com/dustin/go-humanize v1.0.1
|
||||||
github.com/pmezard/go-difflib v1.0.0 // indirect
|
|
||||||
gopkg.in/yaml.v3 v3.0.1 // indirect
|
|
||||||
)
|
)
|
||||||
|
|||||||
10
go.sum
10
go.sum
@@ -38,8 +38,6 @@ github.com/charmbracelet/x/xpty v0.1.2 h1:Pqmu4TEJ8KeA9uSkISKMU3f+C1F6OGBn8ABuGl
|
|||||||
github.com/charmbracelet/x/xpty v0.1.2/go.mod h1:XK2Z0id5rtLWcpeNiMYBccNNBrP2IJnzHI0Lq13Xzq4=
|
github.com/charmbracelet/x/xpty v0.1.2/go.mod h1:XK2Z0id5rtLWcpeNiMYBccNNBrP2IJnzHI0Lq13Xzq4=
|
||||||
github.com/creack/pty v1.1.24 h1:bJrF4RRfyJnbTJqzRLHzcGaZK1NeM5kTC9jGgovnR1s=
|
github.com/creack/pty v1.1.24 h1:bJrF4RRfyJnbTJqzRLHzcGaZK1NeM5kTC9jGgovnR1s=
|
||||||
github.com/creack/pty v1.1.24/go.mod h1:08sCNb52WyoAwi2QDyzUCTgcvVFhUzewun7wtTfvcwE=
|
github.com/creack/pty v1.1.24/go.mod h1:08sCNb52WyoAwi2QDyzUCTgcvVFhUzewun7wtTfvcwE=
|
||||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
|
||||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
|
||||||
github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY=
|
github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY=
|
||||||
github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto=
|
github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto=
|
||||||
github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f h1:Y/CXytFA4m6baUTXGLOoWe4PQhGxaX0KpnayAqC48p4=
|
github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f h1:Y/CXytFA4m6baUTXGLOoWe4PQhGxaX0KpnayAqC48p4=
|
||||||
@@ -60,13 +58,9 @@ github.com/muesli/cancelreader v0.2.2 h1:3I4Kt4BQjOR54NavqnDogx/MIoWBFa0StPA8ELU
|
|||||||
github.com/muesli/cancelreader v0.2.2/go.mod h1:3XuTXfFS2VjM+HTLZY9Ak0l6eUKfijIfMUZ4EgX0QYo=
|
github.com/muesli/cancelreader v0.2.2/go.mod h1:3XuTXfFS2VjM+HTLZY9Ak0l6eUKfijIfMUZ4EgX0QYo=
|
||||||
github.com/muesli/termenv v0.16.0 h1:S5AlUN9dENB57rsbnkPyfdGuWIlkmzJjbFf0Tf5FWUc=
|
github.com/muesli/termenv v0.16.0 h1:S5AlUN9dENB57rsbnkPyfdGuWIlkmzJjbFf0Tf5FWUc=
|
||||||
github.com/muesli/termenv v0.16.0/go.mod h1:ZRfOIKPFDYQoDFF4Olj7/QJbW60Ol/kL1pU3VfY/Cnk=
|
github.com/muesli/termenv v0.16.0/go.mod h1:ZRfOIKPFDYQoDFF4Olj7/QJbW60Ol/kL1pU3VfY/Cnk=
|
||||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
|
||||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
|
||||||
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
|
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
|
||||||
github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ=
|
github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ=
|
||||||
github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
|
github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
|
||||||
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
|
|
||||||
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
|
|
||||||
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e h1:JVG44RsyaB9T2KIHavMF/ppJZNG9ZpyihvCd0w101no=
|
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e h1:JVG44RsyaB9T2KIHavMF/ppJZNG9ZpyihvCd0w101no=
|
||||||
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e/go.mod h1:RbqR21r5mrJuqunuUZ/Dhy/avygyECGrLceyNeo4LiM=
|
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e/go.mod h1:RbqR21r5mrJuqunuUZ/Dhy/avygyECGrLceyNeo4LiM=
|
||||||
golang.org/x/exp v0.0.0-20231006140011-7918f672742d h1:jtJma62tbqLibJ5sFQz8bKtEM8rJBtfilJ2qTU199MI=
|
golang.org/x/exp v0.0.0-20231006140011-7918f672742d h1:jtJma62tbqLibJ5sFQz8bKtEM8rJBtfilJ2qTU199MI=
|
||||||
@@ -79,7 +73,3 @@ golang.org/x/sys v0.32.0 h1:s77OFDvIQeibCmezSnk/q6iAfkdiQaJi4VzroCFrN20=
|
|||||||
golang.org/x/sys v0.32.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k=
|
golang.org/x/sys v0.32.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k=
|
||||||
golang.org/x/text v0.23.0 h1:D71I7dUrlY+VX0gQShAThNGHFxZ13dGLBHQLVl1mJlY=
|
golang.org/x/text v0.23.0 h1:D71I7dUrlY+VX0gQShAThNGHFxZ13dGLBHQLVl1mJlY=
|
||||||
golang.org/x/text v0.23.0/go.mod h1:/BLNzu4aZCJ1+kcD0DNRotWKage4q2rGVAg4o22unh4=
|
golang.org/x/text v0.23.0/go.mod h1:/BLNzu4aZCJ1+kcD0DNRotWKage4q2rGVAg4o22unh4=
|
||||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
|
|
||||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
|
||||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
|
||||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
|
||||||
|
|||||||
239
mite/mite.go
239
mite/mite.go
@@ -3,6 +3,7 @@ package mite
|
|||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
"time"
|
"time"
|
||||||
@@ -50,13 +51,15 @@ type Project struct {
|
|||||||
func (p Project) String() string {
|
func (p Project) String() string {
|
||||||
return fmt.Sprintf("%d: %s (%s)", p.ID, p.Name, p.CustomerName)
|
return fmt.Sprintf("%d: %s (%s)", p.ID, p.Name, p.CustomerName)
|
||||||
}
|
}
|
||||||
func (p Project) GetID() int { return p.ID }
|
func (p Project) GetID() int { return p.ID }
|
||||||
func (p Project) GetName() string { return p.Name }
|
func (p Project) GetName() string {
|
||||||
|
return p.Name + " (" + p.CustomerName + ")"
|
||||||
|
}
|
||||||
|
|
||||||
func (a APIClient) GetProjects() (Projects, error) {
|
func (a APIClient) GetProjects() (Projects, error) {
|
||||||
// GET /projects.json
|
// GET /projects.json
|
||||||
p := APIProjects{}
|
p := APIProjects{}
|
||||||
err := get(a.domain, a.apiKey, "/projects.json", &p)
|
err := a.get("/projects.json", &p)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -98,7 +101,7 @@ func (c Customer) GetName() string { return c.Name }
|
|||||||
func (a APIClient) GetCustomers() (Customers, error) {
|
func (a APIClient) GetCustomers() (Customers, error) {
|
||||||
// GET /customers.json
|
// GET /customers.json
|
||||||
p := apiCustomers{}
|
p := apiCustomers{}
|
||||||
err := get(a.domain, a.apiKey, "/customers.json", &p)
|
err := a.get("/customers.json", &p)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -129,13 +132,20 @@ type Service struct {
|
|||||||
func (s Service) String() string {
|
func (s Service) String() string {
|
||||||
return fmt.Sprintf("%d: %s", s.ID, s.Name)
|
return fmt.Sprintf("%d: %s", s.ID, s.Name)
|
||||||
}
|
}
|
||||||
func (s Service) GetID() int { return s.ID }
|
func (s Service) GetID() int { return s.ID }
|
||||||
func (s Service) GetName() string { return s.Name }
|
func (s Service) GetName() string {
|
||||||
|
prefix := "💰"
|
||||||
|
if !s.Billable {
|
||||||
|
prefix = "☮️"
|
||||||
|
}
|
||||||
|
return prefix + " " + s.Name
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
func (a APIClient) GetServices() (Services, error) {
|
func (a APIClient) GetServices() (Services, error) {
|
||||||
// GET /services.json
|
// GET /services.json
|
||||||
p := apiServices{}
|
p := apiServices{}
|
||||||
err := get(a.domain, a.apiKey, "/services.json", &p)
|
err := a.get("/services.json", &p)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -208,7 +218,7 @@ func (a APIClient) GetTimeEntries(from, to time.Time) (TimeEntries, error) {
|
|||||||
p := apiTimeEntry{}
|
p := apiTimeEntry{}
|
||||||
u := fmt.Sprintf("/time_entries.json?from=%s&to=%s", from.Format(time.DateOnly), to.Format(time.DateOnly))
|
u := fmt.Sprintf("/time_entries.json?from=%s&to=%s", from.Format(time.DateOnly), to.Format(time.DateOnly))
|
||||||
|
|
||||||
err := get(a.domain, a.apiKey, u, &p)
|
err := a.get(u, &p)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -224,13 +234,15 @@ type requestAddTimeEntry struct {
|
|||||||
RequestTimeEntryHolder struct {
|
RequestTimeEntryHolder struct {
|
||||||
DateAt string `json:"date_at"`
|
DateAt string `json:"date_at"`
|
||||||
Minutes int `json:"minutes"`
|
Minutes int `json:"minutes"`
|
||||||
ProjectID int `json:"project_id,omit_empty"`
|
ProjectID int `json:"project_id,omitempty"`
|
||||||
ServiceID int `json:"service_id,omit_empty"`
|
ServiceID int `json:"service_id,omitempty"`
|
||||||
Note string `json:"note"`
|
Note string `json:"note"`
|
||||||
} `json:"time_entry"`
|
} `json:"time_entry"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a APIClient) AddTimeEntry(date string, minutes int, notes string, projectId, serviceId int) error {
|
type apiPostTimeEntry timeEntryHolder
|
||||||
|
|
||||||
|
func (a APIClient) AddTimeEntry(date string, minutes int, notes string, projectId, serviceId int) (int, error) {
|
||||||
// POST /time_entries.json
|
// POST /time_entries.json
|
||||||
// {
|
// {
|
||||||
// "time_entry": {
|
// "time_entry": {
|
||||||
@@ -246,44 +258,113 @@ func (a APIClient) AddTimeEntry(date string, minutes int, notes string, projectI
|
|||||||
req.RequestTimeEntryHolder.ProjectID = projectId
|
req.RequestTimeEntryHolder.ProjectID = projectId
|
||||||
req.RequestTimeEntryHolder.ServiceID = serviceId
|
req.RequestTimeEntryHolder.ServiceID = serviceId
|
||||||
|
|
||||||
err := post(a.domain, a.apiKey, "/time_entries.json", req)
|
res := apiPostTimeEntry{}
|
||||||
return err
|
|
||||||
|
|
||||||
|
err := a.post("/time_entries.json", req, &res)
|
||||||
|
return res.TimeEntry.ID, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func post(domain, apiKey, path string, data any) error {
|
type apiTimeTrackerEntry struct {
|
||||||
|
TimeTrackerHolder timeTrackerHolder `json:"tracker"`
|
||||||
b, err := json.Marshal(data)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
req, err := http.NewRequest("POST", baseurl(domain, path), bytes.NewBuffer(b))
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
req.Header.Add("X-MiteApiKey", apiKey)
|
|
||||||
req.Header.Add("Content-Type", "application/json")
|
|
||||||
resp, err := http.DefaultClient.Do(req)
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
defer resp.Body.Close()
|
|
||||||
if resp.StatusCode >= 300 {
|
|
||||||
return fmt.Errorf("expected 2XX, got %d", resp.StatusCode)
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func get(domain, apiKey, path string, data any) error {
|
type timeTrackerHolder struct {
|
||||||
req, err := http.NewRequest("GET", baseurl(domain, path), nil)
|
TrackingTimeEntry *TrackingTimeEntry `json:"tracking_time_entry"`
|
||||||
|
StoppedTimeEntry *StoppedTimeEntry `json:"stopped_time_entry"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type TrackingTimeEntry struct {
|
||||||
|
ID int `json:"id"`
|
||||||
|
Minutes int `json:"minutes"`
|
||||||
|
Since time.Time `json:"since"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type StoppedTimeEntry struct {
|
||||||
|
ID int `json:"id"`
|
||||||
|
Minutes int `json:"minutes"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// {
|
||||||
|
// "tracker": {
|
||||||
|
// "tracking_time_entry": {
|
||||||
|
// "id": 36135322,
|
||||||
|
// "minutes": 0,
|
||||||
|
// "since": "2015-10-15T17:33:52+02:00"
|
||||||
|
// },
|
||||||
|
// "stopped_time_entry": {
|
||||||
|
// "id": 36134329,
|
||||||
|
// "minutes": 46
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
|
||||||
|
// {
|
||||||
|
// "tracker": {
|
||||||
|
// "tracking_time_entry": {
|
||||||
|
// "id": 36135321,
|
||||||
|
// "minutes": 247,
|
||||||
|
// "since": "2015-10-15T17:05:04+02:00"
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
|
||||||
|
var ErrNoTracker = errors.New("no time tracker running")
|
||||||
|
|
||||||
|
// GetTimeTracker gets the current running time tracker. If no tracker is
|
||||||
|
// running, the error returned will be ErrNoTracker
|
||||||
|
func (a APIClient) GetTimeTracker() (TrackingTimeEntry, error) {
|
||||||
|
r := apiTimeTrackerEntry{}
|
||||||
|
err := a.get("/tracker.json", &r)
|
||||||
|
if err != nil {
|
||||||
|
return TrackingTimeEntry{}, err
|
||||||
|
}
|
||||||
|
if r.TimeTrackerHolder.TrackingTimeEntry == nil {
|
||||||
|
return TrackingTimeEntry{}, ErrNoTracker
|
||||||
|
}
|
||||||
|
return *r.TimeTrackerHolder.TrackingTimeEntry, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a APIClient) StartTimeTracker(id int) (TrackingTimeEntry, *StoppedTimeEntry, error) {
|
||||||
|
url := fmt.Sprintf("/tracker/%d.json", id)
|
||||||
|
r := apiTimeTrackerEntry{}
|
||||||
|
|
||||||
|
err := a.patch(url, &r)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return TrackingTimeEntry{}, nil, err
|
||||||
|
}
|
||||||
|
if r.TimeTrackerHolder.TrackingTimeEntry == nil {
|
||||||
|
// I don't think this should happen, the patch should have been a 404?
|
||||||
|
|
||||||
|
panic(fmt.Sprintf("unexpected failure to find a tracking entry in a successful PATCH\n%#v", r.TimeTrackerHolder))
|
||||||
|
}
|
||||||
|
return *r.TimeTrackerHolder.TrackingTimeEntry, r.TimeTrackerHolder.StoppedTimeEntry, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a APIClient) StopTimeTracker(id int) (StoppedTimeEntry, error) {
|
||||||
|
url := fmt.Sprintf("/tracker/%d.json", id)
|
||||||
|
r := apiTimeTrackerEntry{}
|
||||||
|
|
||||||
|
err := a.delete(url, &r)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return StoppedTimeEntry{}, err
|
||||||
|
}
|
||||||
|
if r.TimeTrackerHolder.StoppedTimeEntry == nil {
|
||||||
|
// I don't think this should happen, the patch should have been a 404?
|
||||||
|
|
||||||
|
panic(fmt.Sprintf("unexpected failure to find a tracking entry in a successful DELETE\n%#v", r.TimeTrackerHolder))
|
||||||
|
}
|
||||||
|
return *r.TimeTrackerHolder.StoppedTimeEntry, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a APIClient) delete(path string, data any) error {
|
||||||
|
|
||||||
|
req, err := http.NewRequest("DELETE", baseurl(a.domain, path), nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
req.Header.Add("X-MiteApiKey", apiKey)
|
req.Header.Add("X-MiteApiKey", a.apiKey)
|
||||||
resp, err := http.DefaultClient.Do(req)
|
resp, err := http.DefaultClient.Do(req)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -298,7 +379,83 @@ func get(domain, apiKey, path string, data any) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a APIClient) patch(path string, data any) error {
|
||||||
|
|
||||||
|
req, err := http.NewRequest("PATCH", baseurl(a.domain, path), nil)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
req.Header.Add("X-MiteApiKey", a.apiKey)
|
||||||
|
resp, err := http.DefaultClient.Do(req)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer resp.Body.Close()
|
||||||
|
if resp.StatusCode >= 300 {
|
||||||
|
return fmt.Errorf("expected 2XX, got %d", resp.StatusCode)
|
||||||
|
}
|
||||||
|
err = json.NewDecoder(resp.Body).Decode(&data)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a APIClient) post(path string, dataIn any, dataOut any) error {
|
||||||
|
|
||||||
|
b, err := json.Marshal(dataIn)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
req, err := http.NewRequest("POST", baseurl(a.domain, path), bytes.NewBuffer(b))
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
req.Header.Add("X-MiteApiKey", a.apiKey)
|
||||||
|
req.Header.Add("Content-Type", "application/json")
|
||||||
|
resp, err := http.DefaultClient.Do(req)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer resp.Body.Close()
|
||||||
|
if resp.StatusCode >= 300 {
|
||||||
|
return fmt.Errorf("expected 2XX, got %d", resp.StatusCode)
|
||||||
|
}
|
||||||
|
|
||||||
|
err = json.NewDecoder(resp.Body).Decode(&dataOut)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a APIClient) get(path string, data any) error {
|
||||||
|
req, err := http.NewRequest("GET", baseurl(a.domain, path), nil)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
req.Header.Add("X-MiteApiKey", a.apiKey)
|
||||||
|
resp, err := http.DefaultClient.Do(req)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer resp.Body.Close()
|
||||||
|
if resp.StatusCode >= 300 {
|
||||||
|
return fmt.Errorf("expected 2XX, got %d", resp.StatusCode)
|
||||||
|
}
|
||||||
|
err = json.NewDecoder(resp.Body).Decode(&data)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func baseurl(domain, path string) string {
|
func baseurl(domain, path string) string {
|
||||||
|
|||||||
82
mite/mite_test.go
Normal file
82
mite/mite_test.go
Normal file
@@ -0,0 +1,82 @@
|
|||||||
|
package mite
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestUnmarshalTimeTracking(t *testing.T) {
|
||||||
|
|
||||||
|
trackerOff := []byte(`{
|
||||||
|
"tracker": {}
|
||||||
|
}`)
|
||||||
|
off := apiTimeTrackerEntry{}
|
||||||
|
err := json.Unmarshal(trackerOff, &off)
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
} else {
|
||||||
|
if off.TimeTrackerHolder.TrackingTimeEntry != nil {
|
||||||
|
t.Error("expected nil, but is not")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
trackerOn := []byte(`
|
||||||
|
{
|
||||||
|
"tracker": {
|
||||||
|
"tracking_time_entry": {
|
||||||
|
"id": 36135321,
|
||||||
|
"minutes": 247,
|
||||||
|
"since": "2015-10-15T17:05:04+02:00"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}`)
|
||||||
|
on := apiTimeTrackerEntry{}
|
||||||
|
err = json.Unmarshal(trackerOn, &on)
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
} else {
|
||||||
|
if on.TimeTrackerHolder.TrackingTimeEntry == nil {
|
||||||
|
t.Error("expected not nil, but is not")
|
||||||
|
} else {
|
||||||
|
if on.TimeTrackerHolder.TrackingTimeEntry.ID != 36135321 {
|
||||||
|
t.Error("bad unmarshal?")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
trackerStart := []byte(`{
|
||||||
|
"tracker": {
|
||||||
|
"tracking_time_entry": {
|
||||||
|
"id": 36135322,
|
||||||
|
"minutes": 0,
|
||||||
|
"since": "2015-10-15T17:33:52+02:00"
|
||||||
|
},
|
||||||
|
"stopped_time_entry": {
|
||||||
|
"id": 36134329,
|
||||||
|
"minutes": 46
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}`)
|
||||||
|
start := apiTimeTrackerEntry{}
|
||||||
|
err = json.Unmarshal(trackerStart, &start)
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
} else {
|
||||||
|
if start.TimeTrackerHolder.TrackingTimeEntry == nil {
|
||||||
|
t.Error("expected not nil, but is not")
|
||||||
|
} else {
|
||||||
|
if start.TimeTrackerHolder.TrackingTimeEntry.ID != 36135322 {
|
||||||
|
t.Error("bad unmarshal?")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if start.TimeTrackerHolder.StoppedTimeEntry == nil {
|
||||||
|
t.Error("expected not nil, but is nil")
|
||||||
|
} else {
|
||||||
|
if start.TimeTrackerHolder.StoppedTimeEntry.ID != 36134329 {
|
||||||
|
t.Error("bad unmarshal")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
148
model.go
148
model.go
@@ -4,7 +4,7 @@ import (
|
|||||||
"charmcal/mite"
|
"charmcal/mite"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"strconv"
|
"slices"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@@ -32,6 +32,8 @@ const MODE_CAL tuiMode = "MODE_CAL"
|
|||||||
const MODE_TIMEENTRIES tuiMode = "MODE_TIMEENTRIES"
|
const MODE_TIMEENTRIES tuiMode = "MODE_TIMEENTRIES"
|
||||||
const MODE_FORMENTRY tuiMode = "MODE_FORMENTRY"
|
const MODE_FORMENTRY tuiMode = "MODE_FORMENTRY"
|
||||||
|
|
||||||
|
var version = "dev"
|
||||||
|
|
||||||
type model struct {
|
type model struct {
|
||||||
miteAPI mite.APIClient
|
miteAPI mite.APIClient
|
||||||
start calendarTime
|
start calendarTime
|
||||||
@@ -54,6 +56,7 @@ type model struct {
|
|||||||
}
|
}
|
||||||
statusBarMessage string
|
statusBarMessage string
|
||||||
windowWidth int
|
windowWidth int
|
||||||
|
windowHeight int
|
||||||
}
|
}
|
||||||
|
|
||||||
func initialModel(miteDomain, miteApiKey string) model {
|
func initialModel(miteDomain, miteApiKey string) model {
|
||||||
@@ -73,10 +76,10 @@ func initialModel(miteDomain, miteApiKey string) model {
|
|||||||
tab.SetStyles(s)
|
tab.SetStyles(s)
|
||||||
|
|
||||||
tab.SetColumns([]table.Column{
|
tab.SetColumns([]table.Column{
|
||||||
table.Column{Title: "min", Width: 5},
|
{Title: " min", Width: 4},
|
||||||
table.Column{Title: "lck", Width: 4},
|
{Title: "🔐", Width: 2},
|
||||||
table.Column{Title: "customer", Width: 10},
|
{Title: "customer", Width: 10},
|
||||||
table.Column{Title: "description", Width: 40},
|
{Title: "description", Width: 40},
|
||||||
})
|
})
|
||||||
|
|
||||||
m.start = calendarTime{time.Now()}
|
m.start = calendarTime{time.Now()}
|
||||||
@@ -92,86 +95,6 @@ func initialModel(miteDomain, miteApiKey string) model {
|
|||||||
return m
|
return m
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m model) buildForm() *huh.Form {
|
|
||||||
|
|
||||||
clOptions := []huh.Option[string]{}
|
|
||||||
for _, cust := range m.formData.customers {
|
|
||||||
op := miteToHuhOption(cust)
|
|
||||||
clOptions = append(clOptions, op)
|
|
||||||
}
|
|
||||||
|
|
||||||
svcOptions := []huh.Option[string]{}
|
|
||||||
for _, svc := range m.formData.services {
|
|
||||||
op := miteToHuhOption(svc)
|
|
||||||
svcOptions = append(svcOptions, op)
|
|
||||||
}
|
|
||||||
|
|
||||||
cl := huh.NewSelect[string]().
|
|
||||||
Key("client").
|
|
||||||
Options(clOptions...).
|
|
||||||
Title("Client").Height(5).Value(&m.formData.selected.customer)
|
|
||||||
|
|
||||||
sl := huh.NewSelect[string]().
|
|
||||||
Key("service").
|
|
||||||
Options(svcOptions...).
|
|
||||||
Title("Service").Height(5)
|
|
||||||
|
|
||||||
pl := huh.NewSelect[string]().
|
|
||||||
Key("project").
|
|
||||||
Title("Project").Height(3).
|
|
||||||
OptionsFunc(func() []huh.Option[string] {
|
|
||||||
out := []huh.Option[string]{}
|
|
||||||
for _, proj := range m.formData.projects {
|
|
||||||
if fmt.Sprint(proj.CustomerID) == m.formData.selected.customer {
|
|
||||||
out = append(out, miteToHuhOption(proj))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return out
|
|
||||||
}, &m.formData.selected.customer)
|
|
||||||
|
|
||||||
form := huh.NewForm(
|
|
||||||
huh.NewGroup(
|
|
||||||
cl,
|
|
||||||
sl,
|
|
||||||
pl,
|
|
||||||
),
|
|
||||||
huh.NewGroup(
|
|
||||||
huh.NewText().
|
|
||||||
Key("description").
|
|
||||||
Title("description").
|
|
||||||
Validate(func(s string) error {
|
|
||||||
if s == "" {
|
|
||||||
return errors.New("must enter a description")
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}),
|
|
||||||
huh.NewInput().
|
|
||||||
Key("minutes").
|
|
||||||
CharLimit(5).
|
|
||||||
Validate(
|
|
||||||
func(s string) error {
|
|
||||||
h, err := strconv.ParseInt(s, 10, 64)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if h < 0 {
|
|
||||||
return errors.New("must be positive")
|
|
||||||
}
|
|
||||||
return err
|
|
||||||
}).
|
|
||||||
Title("Minutes"),
|
|
||||||
),
|
|
||||||
)
|
|
||||||
return form
|
|
||||||
}
|
|
||||||
|
|
||||||
func miteToHuhOption[T mite.APIObject](i T) huh.Option[string] {
|
|
||||||
return huh.Option[string]{
|
|
||||||
Key: i.GetName(),
|
|
||||||
Value: fmt.Sprint(i.GetID()),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m model) Init() tea.Cmd {
|
func (m model) Init() tea.Cmd {
|
||||||
return m.fetchMiteData()
|
return m.fetchMiteData()
|
||||||
}
|
}
|
||||||
@@ -188,13 +111,17 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
|||||||
m.fetchedData = false
|
m.fetchedData = false
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
m.statusBarMessage = fmt.Sprintf("Fetched %d time entries", len(msg.TimeEntries))
|
m.statusBarMessage = fmt.Sprintf("Fetched %d time entries in %s", len(msg.TimeEntries), time.Since(msg.start).Truncate(time.Millisecond))
|
||||||
m.timeData.entries = msg.TimeEntries
|
m.timeData.entries = msg.TimeEntries
|
||||||
m.formData.customers = msg.Customers
|
m.formData.customers = msg.Customers
|
||||||
m.formData.services = msg.Services
|
m.formData.services = msg.Services
|
||||||
m.formData.projects = msg.Projects
|
m.formData.projects = msg.Projects
|
||||||
m.fetchedData = true
|
m.fetchedData = true
|
||||||
|
|
||||||
|
slices.SortFunc(m.formData.customers, func(a, b mite.Customer) int { return strings.Compare(a.GetName(), b.GetName()) })
|
||||||
|
slices.SortFunc(m.formData.services, func(a, b mite.Service) int { return strings.Compare(a.GetName(), b.GetName()) })
|
||||||
|
slices.SortFunc(m.formData.projects, func(a, b mite.Project) int { return strings.Compare(a.GetName(), b.GetName()) })
|
||||||
|
|
||||||
// just in case there is data for the currently focused day
|
// just in case there is data for the currently focused day
|
||||||
m.timeData.table.SetRows(m.tableDataForDate(m.dest.Time))
|
m.timeData.table.SetRows(m.tableDataForDate(m.dest.Time))
|
||||||
}
|
}
|
||||||
@@ -275,6 +202,7 @@ func (m model) updateCal(msg tea.Msg) (tea.Model, tea.Cmd) {
|
|||||||
switch msg := msg.(type) {
|
switch msg := msg.(type) {
|
||||||
case tea.WindowSizeMsg:
|
case tea.WindowSizeMsg:
|
||||||
m.windowWidth = msg.Width
|
m.windowWidth = msg.Width
|
||||||
|
m.windowHeight = msg.Height
|
||||||
|
|
||||||
// Is it a key press?
|
// Is it a key press?
|
||||||
case tea.KeyMsg:
|
case tea.KeyMsg:
|
||||||
@@ -376,12 +304,13 @@ func (m model) tableDataForDate(t time.Time) []table.Row {
|
|||||||
if entry.Locked {
|
if entry.Locked {
|
||||||
lock = "🔐"
|
lock = "🔐"
|
||||||
}
|
}
|
||||||
rows = append(rows, table.Row{fmt.Sprint(entry.Minutes), lock, entry.CustomerName, entry.Note})
|
rows = append(rows, table.Row{fmt.Sprintf("%4d", entry.Minutes), lock, entry.CustomerName, entry.Note})
|
||||||
}
|
}
|
||||||
return rows
|
return rows
|
||||||
}
|
}
|
||||||
|
|
||||||
type miteDataFetchedMsg struct {
|
type miteDataFetchedMsg struct {
|
||||||
|
start time.Time
|
||||||
TimeEntries mite.TimeEntries
|
TimeEntries mite.TimeEntries
|
||||||
Customers mite.Customers
|
Customers mite.Customers
|
||||||
Services mite.Services
|
Services mite.Services
|
||||||
@@ -391,12 +320,10 @@ type miteDataFetchedMsg struct {
|
|||||||
|
|
||||||
func (m model) fetchMiteData() tea.Cmd {
|
func (m model) fetchMiteData() tea.Cmd {
|
||||||
return func() tea.Msg {
|
return func() tea.Msg {
|
||||||
// time.Sleep(time.Second * 5)
|
t0 := time.Now()
|
||||||
loc := time.Local
|
from := time.Now().Add(-time.Hour * 24 * 30 * 6) // about 6 months
|
||||||
te, err1 := m.miteAPI.GetTimeEntries(
|
to := time.Now().Add(time.Hour * 20 * 30) // about 1 month
|
||||||
time.Date(2024, time.January, 1, 0, 0, 0, 0, loc),
|
te, err1 := m.miteAPI.GetTimeEntries(from, to)
|
||||||
time.Date(2025, time.December, 31, 0, 0, 0, 0, loc),
|
|
||||||
)
|
|
||||||
cst, err2 := m.miteAPI.GetCustomers()
|
cst, err2 := m.miteAPI.GetCustomers()
|
||||||
svc, err3 := m.miteAPI.GetServices()
|
svc, err3 := m.miteAPI.GetServices()
|
||||||
pjt, err4 := m.miteAPI.GetProjects()
|
pjt, err4 := m.miteAPI.GetProjects()
|
||||||
@@ -407,6 +334,7 @@ func (m model) fetchMiteData() tea.Cmd {
|
|||||||
Services: svc,
|
Services: svc,
|
||||||
Projects: pjt,
|
Projects: pjt,
|
||||||
Error: errors.Join(err1, err2, err3, err4),
|
Error: errors.Join(err1, err2, err3, err4),
|
||||||
|
start: t0,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -418,6 +346,12 @@ func (m model) View() string {
|
|||||||
lhs := strings.Builder{}
|
lhs := strings.Builder{}
|
||||||
rhs := strings.Builder{}
|
rhs := strings.Builder{}
|
||||||
|
|
||||||
|
nb := lipgloss.NewStyle().Border(lipgloss.NormalBorder())
|
||||||
|
db := lipgloss.NewStyle().Border(lipgloss.DoubleBorder())
|
||||||
|
|
||||||
|
lhsS := nb
|
||||||
|
rhsS := nb
|
||||||
|
|
||||||
// if any entries are not "locked", we always use that
|
// if any entries are not "locked", we always use that
|
||||||
// regular colour in preference
|
// regular colour in preference
|
||||||
styles := map[string]lipgloss.Style{}
|
styles := map[string]lipgloss.Style{}
|
||||||
@@ -439,6 +373,7 @@ func (m model) View() string {
|
|||||||
|
|
||||||
if m.tuiMode == MODE_CAL {
|
if m.tuiMode == MODE_CAL {
|
||||||
lhs.WriteString("(f)etch time data\n")
|
lhs.WriteString("(f)etch time data\n")
|
||||||
|
lhsS = db
|
||||||
} else {
|
} else {
|
||||||
lhs.WriteString("\n")
|
lhs.WriteString("\n")
|
||||||
}
|
}
|
||||||
@@ -450,18 +385,24 @@ func (m model) View() string {
|
|||||||
if m.tuiMode == MODE_FORMENTRY {
|
if m.tuiMode == MODE_FORMENTRY {
|
||||||
lhs.WriteString("(esc) abort form\n")
|
lhs.WriteString("(esc) abort form\n")
|
||||||
lhs.WriteString("\n")
|
lhs.WriteString("\n")
|
||||||
|
rhsS = db
|
||||||
} else {
|
} else {
|
||||||
lhs.WriteString("(tab) switch panes\n")
|
lhs.WriteString("(tab) switch panes\n")
|
||||||
lhs.WriteString("(q)uit\n")
|
lhs.WriteString("(q)uit\n")
|
||||||
}
|
}
|
||||||
|
if m.tuiMode == MODE_TIMEENTRIES {
|
||||||
|
rhsS = db
|
||||||
|
}
|
||||||
|
|
||||||
calendarWidth := 25
|
lhsWidth := 25
|
||||||
tableWidth := m.windowWidth - calendarWidth
|
rhsWidth := m.windowWidth - lhsWidth - 4
|
||||||
|
|
||||||
if m.tuiMode == MODE_FORMENTRY {
|
if m.tuiMode == MODE_FORMENTRY {
|
||||||
rhs.WriteString(m.formData.form.View())
|
rhs.WriteString(m.formData.form.View())
|
||||||
} else {
|
} else {
|
||||||
if m.fetchedData {
|
if m.fetchedData {
|
||||||
|
m.timeData.table.Columns()[3].Width = rhsWidth - 30
|
||||||
|
m.timeData.table.SetHeight(14)
|
||||||
rhs.WriteString(m.timeData.table.View())
|
rhs.WriteString(m.timeData.table.View())
|
||||||
rhs.WriteString("\n")
|
rhs.WriteString("\n")
|
||||||
}
|
}
|
||||||
@@ -469,14 +410,23 @@ func (m model) View() string {
|
|||||||
|
|
||||||
out := lipgloss.JoinHorizontal(
|
out := lipgloss.JoinHorizontal(
|
||||||
lipgloss.Top,
|
lipgloss.Top,
|
||||||
lipgloss.NewStyle().Width(calendarWidth).Render(lhs.String()),
|
lhsS.Render(lipgloss.NewStyle().Width(lhsWidth).Render(lhs.String())),
|
||||||
lipgloss.NewStyle().Width(tableWidth).Render(rhs.String()),
|
rhsS.Render(lipgloss.NewStyle().Width(rhsWidth).Render(rhs.String())),
|
||||||
)
|
)
|
||||||
|
|
||||||
sofar := lipgloss.Height(out)
|
sofar := lipgloss.Height(out)
|
||||||
|
|
||||||
out +=
|
statusMsg := strings.ReplaceAll(m.statusBarMessage, "\n", " ")
|
||||||
styleStatusBar.MarginTop(19 - sofar).Width(m.windowWidth).Render(m.statusBarMessage)
|
|
||||||
|
mainMsg := styleStatusBar.Width(m.windowWidth - len(version)).MarginTop(m.windowHeight - sofar).Render(statusMsg)
|
||||||
|
versionMsg := styleStatusBar.MarginTop(m.windowHeight - sofar).Render(version)
|
||||||
|
|
||||||
|
bar := lipgloss.JoinHorizontal(lipgloss.Top,
|
||||||
|
mainMsg,
|
||||||
|
versionMsg,
|
||||||
|
)
|
||||||
|
|
||||||
|
out += bar
|
||||||
|
|
||||||
return out
|
return out
|
||||||
}
|
}
|
||||||
|
|||||||
124
model_form.go
124
model_form.go
@@ -1,7 +1,9 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"charmcal/mite"
|
||||||
"errors"
|
"errors"
|
||||||
|
"fmt"
|
||||||
"strconv"
|
"strconv"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@@ -18,6 +20,8 @@ func (m model) updateForm(msg tea.Msg) (tea.Model, tea.Cmd) {
|
|||||||
case "esc":
|
case "esc":
|
||||||
m.tuiMode = MODE_CAL
|
m.tuiMode = MODE_CAL
|
||||||
return m, nil
|
return m, nil
|
||||||
|
case "ctrl+c":
|
||||||
|
return m, tea.Quit
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -31,6 +35,7 @@ func (m model) updateForm(msg tea.Msg) (tea.Model, tea.Cmd) {
|
|||||||
projectID := m.formData.form.GetString("project")
|
projectID := m.formData.form.GetString("project")
|
||||||
description := m.formData.form.GetString("description")
|
description := m.formData.form.GetString("description")
|
||||||
minutes := m.formData.form.GetString("minutes")
|
minutes := m.formData.form.GetString("minutes")
|
||||||
|
tracker := m.formData.form.GetBool("tracker")
|
||||||
|
|
||||||
minutesInt, err1 := strconv.ParseInt(minutes, 10, 64)
|
minutesInt, err1 := strconv.ParseInt(minutes, 10, 64)
|
||||||
serviceIDInt, err2 := strconv.ParseInt(serviceID, 10, 64)
|
serviceIDInt, err2 := strconv.ParseInt(serviceID, 10, 64)
|
||||||
@@ -41,13 +46,27 @@ func (m model) updateForm(msg tea.Msg) (tea.Model, tea.Cmd) {
|
|||||||
m.tuiMode = MODE_CAL
|
m.tuiMode = MODE_CAL
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
err := m.miteAPI.AddTimeEntry(m.dest.Format(time.DateOnly), int(minutesInt), description, int(projectIDInt), int(serviceIDInt))
|
id, err := m.miteAPI.AddTimeEntry(m.dest.Format(time.DateOnly), int(minutesInt), description, int(projectIDInt), int(serviceIDInt))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
m.statusBarMessage = errors.Join(err1, err2, err3).Error()
|
m.statusBarMessage = err.Error()
|
||||||
m.tuiMode = MODE_CAL
|
m.tuiMode = MODE_CAL
|
||||||
} else {
|
} else {
|
||||||
m.statusBarMessage = "Successfully logged time"
|
if tracker {
|
||||||
m.tuiMode = MODE_CAL
|
_, _, err = m.miteAPI.StartTimeTracker(id)
|
||||||
|
if err != nil {
|
||||||
|
m.statusBarMessage = err.Error()
|
||||||
|
m.tuiMode = MODE_CAL
|
||||||
|
} else {
|
||||||
|
m.statusBarMessage = "Successfully logged time and started tracker"
|
||||||
|
m.tuiMode = MODE_CAL
|
||||||
|
return m, m.fetchMiteData()
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
|
||||||
|
m.statusBarMessage = "Successfully logged time"
|
||||||
|
m.tuiMode = MODE_CAL
|
||||||
|
return m, m.fetchMiteData()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -55,3 +74,100 @@ func (m model) updateForm(msg tea.Msg) (tea.Model, tea.Cmd) {
|
|||||||
|
|
||||||
return m, formCmd
|
return m, formCmd
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (m model) buildForm() *huh.Form {
|
||||||
|
|
||||||
|
clOptions := []huh.Option[string]{}
|
||||||
|
for _, cust := range m.formData.customers {
|
||||||
|
op := miteToHuhOption(cust)
|
||||||
|
clOptions = append(clOptions, op)
|
||||||
|
}
|
||||||
|
|
||||||
|
svcOptions := []huh.Option[string]{}
|
||||||
|
for _, svc := range m.formData.services {
|
||||||
|
op := miteToHuhOption(svc)
|
||||||
|
svcOptions = append(svcOptions, op)
|
||||||
|
}
|
||||||
|
|
||||||
|
projOptions := []huh.Option[string]{
|
||||||
|
{
|
||||||
|
Key: "[no project]",
|
||||||
|
Value: "0",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
for _, proj := range m.formData.projects {
|
||||||
|
op := miteToHuhOption(proj)
|
||||||
|
projOptions = append(projOptions, op)
|
||||||
|
}
|
||||||
|
|
||||||
|
// cl := huh.NewSelect[string]().
|
||||||
|
// Key("client").
|
||||||
|
// Options(clOptions...).
|
||||||
|
// Title("Client").Height(5).Value(&m.formData.selected.customer)
|
||||||
|
|
||||||
|
sl := huh.NewSelect[string]().
|
||||||
|
Key("service").
|
||||||
|
Options(svcOptions...).
|
||||||
|
Title("Service").
|
||||||
|
Height(6)
|
||||||
|
|
||||||
|
pl := huh.NewSelect[string]().
|
||||||
|
Key("project").
|
||||||
|
Title("Project").
|
||||||
|
Options(projOptions...).
|
||||||
|
Height(6)
|
||||||
|
|
||||||
|
description := huh.NewText().
|
||||||
|
Key("description").
|
||||||
|
Title("description").
|
||||||
|
Validate(func(s string) error {
|
||||||
|
if s == "" {
|
||||||
|
return errors.New("must enter a description")
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
|
||||||
|
defMinutes := "0"
|
||||||
|
minutes := huh.NewInput().
|
||||||
|
Key("minutes").
|
||||||
|
CharLimit(5).
|
||||||
|
Validate(
|
||||||
|
func(s string) error {
|
||||||
|
h, err := strconv.ParseInt(s, 10, 64)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if h < 0 {
|
||||||
|
return errors.New("must be positive")
|
||||||
|
}
|
||||||
|
return err
|
||||||
|
}).
|
||||||
|
Title("Minutes").
|
||||||
|
Value(&defMinutes)
|
||||||
|
|
||||||
|
confirmTracker := huh.NewConfirm().
|
||||||
|
Key("tracker").
|
||||||
|
Affirmative("Yes").
|
||||||
|
Negative("No").
|
||||||
|
Title("Start tracker")
|
||||||
|
|
||||||
|
form := huh.NewForm(
|
||||||
|
huh.NewGroup(
|
||||||
|
pl,
|
||||||
|
sl,
|
||||||
|
description,
|
||||||
|
),
|
||||||
|
huh.NewGroup(
|
||||||
|
minutes,
|
||||||
|
confirmTracker,
|
||||||
|
),
|
||||||
|
)
|
||||||
|
return form
|
||||||
|
}
|
||||||
|
|
||||||
|
func miteToHuhOption[T mite.APIObject](i T) huh.Option[string] {
|
||||||
|
return huh.Option[string]{
|
||||||
|
Key: i.GetName(),
|
||||||
|
Value: fmt.Sprint(i.GetID()),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user