Compare commits
12 Commits
Author | SHA1 | Date | |
---|---|---|---|
9887b8134d | |||
6b25be64d7 | |||
4eaf1e73bb | |||
c7798c5784 | |||
6116e89aac | |||
e1df2dea3b | |||
9a318d3cff | |||
c6d8fee715 | |||
34a94fa269 | |||
163e7abf47 | |||
d51f409497 | |||
e7fa789b2b |
2
.gitignore
vendored
2
.gitignore
vendored
@ -1,2 +1,2 @@
|
||||
# Added by goreleaser init:
|
||||
dist/
|
||||
.DS_Store
|
||||
|
3
.vscode/settings.json
vendored
3
.vscode/settings.json
vendored
@ -1,6 +1,9 @@
|
||||
{
|
||||
"cSpell.ignoreWords": [
|
||||
"FORMENTRY",
|
||||
"TIMEENTRIES",
|
||||
"ancel",
|
||||
"lipgloss",
|
||||
"oday"
|
||||
]
|
||||
}
|
23
README.md
23
README.md
@ -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 .
|
224
mite/mite.go
224
mite/mite.go
@ -3,6 +3,7 @@ package mite
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"time"
|
||||
@ -50,13 +51,15 @@ type Project struct {
|
||||
func (p Project) String() string {
|
||||
return fmt.Sprintf("%d: %s (%s)", p.ID, p.Name, p.CustomerName)
|
||||
}
|
||||
func (p Project) GetID() int { return p.ID }
|
||||
func (p Project) GetName() string { return p.Name + " (" + p.CustomerName + ")" }
|
||||
func (p Project) GetID() int { return p.ID }
|
||||
func (p Project) GetName() string {
|
||||
return p.Name + " (" + p.CustomerName + ")"
|
||||
}
|
||||
|
||||
func (a APIClient) GetProjects() (Projects, error) {
|
||||
// GET /projects.json
|
||||
p := APIProjects{}
|
||||
err := get(a.domain, a.apiKey, "/projects.json", &p)
|
||||
err := a.get("/projects.json", &p)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@ -98,7 +101,7 @@ func (c Customer) GetName() string { return c.Name }
|
||||
func (a APIClient) GetCustomers() (Customers, error) {
|
||||
// GET /customers.json
|
||||
p := apiCustomers{}
|
||||
err := get(a.domain, a.apiKey, "/customers.json", &p)
|
||||
err := a.get("/customers.json", &p)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@ -142,7 +145,7 @@ func (s Service) GetName() string {
|
||||
func (a APIClient) GetServices() (Services, error) {
|
||||
// GET /services.json
|
||||
p := apiServices{}
|
||||
err := get(a.domain, a.apiKey, "/services.json", &p)
|
||||
err := a.get("/services.json", &p)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@ -215,7 +218,7 @@ func (a APIClient) GetTimeEntries(from, to time.Time) (TimeEntries, error) {
|
||||
p := apiTimeEntry{}
|
||||
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 {
|
||||
return nil, err
|
||||
}
|
||||
@ -237,7 +240,9 @@ type requestAddTimeEntry struct {
|
||||
} `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
|
||||
// {
|
||||
// "time_entry": {
|
||||
@ -253,44 +258,113 @@ func (a APIClient) AddTimeEntry(date string, minutes int, notes string, projectI
|
||||
req.RequestTimeEntryHolder.ProjectID = projectId
|
||||
req.RequestTimeEntryHolder.ServiceID = serviceId
|
||||
|
||||
err := post(a.domain, a.apiKey, "/time_entries.json", req)
|
||||
return err
|
||||
res := apiPostTimeEntry{}
|
||||
|
||||
err := a.post("/time_entries.json", req, &res)
|
||||
return res.TimeEntry.ID, err
|
||||
}
|
||||
|
||||
func post(domain, apiKey, path string, data any) error {
|
||||
|
||||
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
|
||||
|
||||
type apiTimeTrackerEntry struct {
|
||||
TimeTrackerHolder timeTrackerHolder `json:"tracker"`
|
||||
}
|
||||
|
||||
func get(domain, apiKey, path string, data any) error {
|
||||
req, err := http.NewRequest("GET", baseurl(domain, path), nil)
|
||||
type timeTrackerHolder struct {
|
||||
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 {
|
||||
return err
|
||||
}
|
||||
req.Header.Add("X-MiteApiKey", apiKey)
|
||||
req.Header.Add("X-MiteApiKey", a.apiKey)
|
||||
resp, err := http.DefaultClient.Do(req)
|
||||
|
||||
if err != nil {
|
||||
@ -305,7 +379,83 @@ func get(domain, apiKey, path string, data any) error {
|
||||
return err
|
||||
}
|
||||
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 {
|
||||
|
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")
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
116
model.go
116
model.go
@ -34,7 +34,17 @@ const MODE_FORMENTRY tuiMode = "MODE_FORMENTRY"
|
||||
|
||||
var version = "dev"
|
||||
|
||||
type tickMsg time.Time
|
||||
|
||||
func tick() tea.Cmd {
|
||||
return tea.Tick(time.Second, func(t time.Time) tea.Msg {
|
||||
return tickMsg(t)
|
||||
})
|
||||
}
|
||||
|
||||
type model struct {
|
||||
currentTime time.Time
|
||||
|
||||
miteAPI mite.APIClient
|
||||
start calendarTime
|
||||
dest calendarTime
|
||||
@ -53,6 +63,7 @@ type model struct {
|
||||
timeData struct {
|
||||
entries mite.TimeEntries
|
||||
table table.Model
|
||||
tracker *mite.TrackingTimeEntry
|
||||
}
|
||||
statusBarMessage string
|
||||
windowWidth int
|
||||
@ -76,10 +87,10 @@ func initialModel(miteDomain, miteApiKey string) model {
|
||||
tab.SetStyles(s)
|
||||
|
||||
tab.SetColumns([]table.Column{
|
||||
table.Column{Title: "min", Width: 5},
|
||||
table.Column{Title: "lck", Width: 4},
|
||||
table.Column{Title: "customer", Width: 10},
|
||||
table.Column{Title: "description", Width: 40},
|
||||
{Title: " min", Width: 4},
|
||||
{Title: "🔐", Width: 2},
|
||||
{Title: "customer", Width: 10},
|
||||
{Title: "description", Width: 40},
|
||||
})
|
||||
|
||||
m.start = calendarTime{time.Now()}
|
||||
@ -96,7 +107,11 @@ func initialModel(miteDomain, miteApiKey string) model {
|
||||
}
|
||||
|
||||
func (m model) Init() tea.Cmd {
|
||||
return m.fetchMiteData()
|
||||
|
||||
return tea.Batch(
|
||||
m.fetchMiteData(),
|
||||
tick(),
|
||||
)
|
||||
}
|
||||
|
||||
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
||||
@ -105,6 +120,10 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
||||
|
||||
switch msg := msg.(type) {
|
||||
|
||||
case tickMsg:
|
||||
m.currentTime = time.Time(msg)
|
||||
return m, tick() // schedule next tick
|
||||
|
||||
case miteDataFetchedMsg:
|
||||
if msg.Error != nil {
|
||||
m.statusBarMessage = fmt.Sprintf("Error fetching: %s", msg.Error.Error())
|
||||
@ -116,6 +135,7 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
||||
m.formData.customers = msg.Customers
|
||||
m.formData.services = msg.Services
|
||||
m.formData.projects = msg.Projects
|
||||
m.timeData.tracker = msg.Tracker
|
||||
m.fetchedData = true
|
||||
|
||||
slices.SortFunc(m.formData.customers, func(a, b mite.Customer) int { return strings.Compare(a.GetName(), b.GetName()) })
|
||||
@ -133,13 +153,14 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
||||
}
|
||||
m.debug = append(m.debug, fmt.Sprintf("Got a %#v", msg))
|
||||
|
||||
if m.tuiMode == MODE_TIMEENTRIES {
|
||||
switch m.tuiMode {
|
||||
case MODE_TIMEENTRIES:
|
||||
return m.updateEntries(msg)
|
||||
} else if m.tuiMode == MODE_CAL {
|
||||
case MODE_CAL:
|
||||
return m.updateCal(msg)
|
||||
} else if m.tuiMode == MODE_FORMENTRY {
|
||||
case MODE_FORMENTRY:
|
||||
return m.updateForm(msg)
|
||||
} else {
|
||||
default:
|
||||
panic(m.tuiMode)
|
||||
}
|
||||
}
|
||||
@ -188,13 +209,29 @@ func (m model) updateEntries(msg tea.Msg) (tea.Model, tea.Cmd) {
|
||||
m.formData.form = m.buildForm()
|
||||
m.formData.form.Init()
|
||||
return m, nil
|
||||
case "c":
|
||||
if m.timeData.tracker != nil {
|
||||
m, msg := m.cancelTracker()
|
||||
return m, msg
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
newTable, tableCmd := m.timeData.table.Update(msg)
|
||||
m.timeData.table = newTable
|
||||
return m, tableCmd
|
||||
}
|
||||
|
||||
func (m model) cancelTracker() (model, tea.Cmd) {
|
||||
stopped, err := m.miteAPI.StopTimeTracker(m.timeData.tracker.ID)
|
||||
if err != nil {
|
||||
m.statusBarMessage = err.Error()
|
||||
return m, nil
|
||||
} else {
|
||||
m.statusBarMessage = fmt.Sprintf("Stopped tracking: %d", stopped.ID)
|
||||
m.timeData.tracker = nil
|
||||
return m, m.fetchMiteData()
|
||||
}
|
||||
}
|
||||
|
||||
func (m model) updateCal(msg tea.Msg) (tea.Model, tea.Cmd) {
|
||||
@ -234,6 +271,12 @@ func (m model) updateCal(msg tea.Msg) (tea.Model, tea.Cmd) {
|
||||
m.statusBarMessage = "Fetching data from API"
|
||||
return m, m.fetchMiteData()
|
||||
|
||||
// cancel tracker
|
||||
case "c":
|
||||
if m.timeData.tracker != nil {
|
||||
return m.cancelTracker()
|
||||
}
|
||||
|
||||
// // The "up" and "k" keys move the cursor up
|
||||
// case "up", "k":
|
||||
// if m.cursor > 0 {
|
||||
@ -275,7 +318,7 @@ func (m model) updateCal(msg tea.Msg) (tea.Model, tea.Cmd) {
|
||||
m.timeData.table.SetRows(m.tableDataForDate(m.dest.Time))
|
||||
return m, nil
|
||||
}
|
||||
// The "enter" key and the spacebar (a literal space) toggle
|
||||
// The "enter" key and the space bar (a literal space) toggle
|
||||
// the selected state for the item that the cursor is pointing at.
|
||||
// case "enter", " ":
|
||||
// _, ok := m.selected[m.cursor]
|
||||
@ -304,7 +347,7 @@ func (m model) tableDataForDate(t time.Time) []table.Row {
|
||||
if entry.Locked {
|
||||
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
|
||||
}
|
||||
@ -315,6 +358,7 @@ type miteDataFetchedMsg struct {
|
||||
Customers mite.Customers
|
||||
Services mite.Services
|
||||
Projects mite.Projects
|
||||
Tracker *mite.TrackingTimeEntry
|
||||
Error error
|
||||
}
|
||||
|
||||
@ -327,12 +371,20 @@ func (m model) fetchMiteData() tea.Cmd {
|
||||
cst, err2 := m.miteAPI.GetCustomers()
|
||||
svc, err3 := m.miteAPI.GetServices()
|
||||
pjt, err4 := m.miteAPI.GetProjects()
|
||||
tt, err5 := m.miteAPI.GetTimeTracker()
|
||||
|
||||
var msgTT = &tt
|
||||
if err5 == mite.ErrNoTracker {
|
||||
err5 = nil
|
||||
msgTT = nil
|
||||
}
|
||||
|
||||
return miteDataFetchedMsg{
|
||||
TimeEntries: te,
|
||||
Customers: cst,
|
||||
Services: svc,
|
||||
Projects: pjt,
|
||||
Tracker: msgTT,
|
||||
Error: errors.Join(err1, err2, err3, err4),
|
||||
start: t0,
|
||||
}
|
||||
@ -346,6 +398,12 @@ func (m model) View() string {
|
||||
lhs := 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
|
||||
// regular colour in preference
|
||||
styles := map[string]lipgloss.Style{}
|
||||
@ -367,29 +425,49 @@ func (m model) View() string {
|
||||
|
||||
if m.tuiMode == MODE_CAL {
|
||||
lhs.WriteString("(f)etch time data\n")
|
||||
lhsS = db
|
||||
} else {
|
||||
lhs.WriteString("\n")
|
||||
}
|
||||
if m.tuiMode != MODE_FORMENTRY {
|
||||
if m.globalKeyMode() {
|
||||
lhs.WriteString("(a)dd time entry\n")
|
||||
} else {
|
||||
lhs.WriteString("\n")
|
||||
}
|
||||
|
||||
if m.tuiMode == MODE_FORMENTRY {
|
||||
lhs.WriteString("(esc) abort form\n")
|
||||
lhs.WriteString("\n")
|
||||
rhsS = db
|
||||
} else {
|
||||
lhs.WriteString("(tab) switch panes\n")
|
||||
lhs.WriteString("(q)uit\n")
|
||||
}
|
||||
|
||||
calendarWidth := 25
|
||||
tableWidth := m.windowWidth - calendarWidth
|
||||
if m.timeData.tracker != nil {
|
||||
activeTime := time.Since(m.timeData.tracker.Since).Truncate(time.Second).String()
|
||||
activeTime = strings.Replace(activeTime, "0ms", "", 1)
|
||||
lhs.WriteString("\nTracker active: " + activeTime + "\n")
|
||||
if m.globalKeyMode() {
|
||||
lhs.WriteString("(c)ancel\n")
|
||||
} else {
|
||||
lhs.WriteString("\n")
|
||||
}
|
||||
}
|
||||
|
||||
if m.tuiMode == MODE_TIMEENTRIES {
|
||||
rhsS = db
|
||||
}
|
||||
|
||||
lhsWidth := 25
|
||||
rhsWidth := m.windowWidth - lhsWidth - 4
|
||||
|
||||
if m.tuiMode == MODE_FORMENTRY {
|
||||
rhs.WriteString(m.formData.form.View())
|
||||
} else {
|
||||
if m.fetchedData {
|
||||
m.timeData.table.Columns()[3].Width = rhsWidth - 30
|
||||
m.timeData.table.SetHeight(14)
|
||||
rhs.WriteString(m.timeData.table.View())
|
||||
rhs.WriteString("\n")
|
||||
}
|
||||
@ -397,8 +475,8 @@ func (m model) View() string {
|
||||
|
||||
out := lipgloss.JoinHorizontal(
|
||||
lipgloss.Top,
|
||||
lipgloss.NewStyle().Width(calendarWidth).Render(lhs.String()),
|
||||
lipgloss.NewStyle().Width(tableWidth).Render(rhs.String()),
|
||||
lhsS.Render(lipgloss.NewStyle().Width(lhsWidth).Render(lhs.String())),
|
||||
rhsS.Render(lipgloss.NewStyle().Width(rhsWidth).Render(rhs.String())),
|
||||
)
|
||||
|
||||
sofar := lipgloss.Height(out)
|
||||
@ -417,3 +495,9 @@ func (m model) View() string {
|
||||
|
||||
return out
|
||||
}
|
||||
|
||||
// globalKeyMode indicates when the UI can receive most keystrokes for global operations -
|
||||
// mostly this means the form is not active
|
||||
func (m model) globalKeyMode() bool {
|
||||
return m.tuiMode != MODE_FORMENTRY
|
||||
}
|
||||
|
@ -20,6 +20,8 @@ func (m model) updateForm(msg tea.Msg) (tea.Model, tea.Cmd) {
|
||||
case "esc":
|
||||
m.tuiMode = MODE_CAL
|
||||
return m, nil
|
||||
case "ctrl+c":
|
||||
return m, tea.Quit
|
||||
}
|
||||
}
|
||||
|
||||
@ -33,6 +35,7 @@ func (m model) updateForm(msg tea.Msg) (tea.Model, tea.Cmd) {
|
||||
projectID := m.formData.form.GetString("project")
|
||||
description := m.formData.form.GetString("description")
|
||||
minutes := m.formData.form.GetString("minutes")
|
||||
tracker := m.formData.form.GetBool("tracker")
|
||||
|
||||
minutesInt, err1 := strconv.ParseInt(minutes, 10, 64)
|
||||
serviceIDInt, err2 := strconv.ParseInt(serviceID, 10, 64)
|
||||
@ -43,14 +46,27 @@ func (m model) updateForm(msg tea.Msg) (tea.Model, tea.Cmd) {
|
||||
m.tuiMode = MODE_CAL
|
||||
} 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 {
|
||||
m.statusBarMessage = err.Error()
|
||||
m.tuiMode = MODE_CAL
|
||||
} else {
|
||||
m.statusBarMessage = "Successfully logged time"
|
||||
m.tuiMode = MODE_CAL
|
||||
return m, m.fetchMiteData()
|
||||
if tracker {
|
||||
_, _, 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()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -99,38 +115,51 @@ func (m model) buildForm() *huh.Form {
|
||||
Key("project").
|
||||
Title("Project").
|
||||
Options(projOptions...).
|
||||
Height(10)
|
||||
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(
|
||||
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"),
|
||||
minutes,
|
||||
confirmTracker,
|
||||
),
|
||||
)
|
||||
return form
|
||||
|
Loading…
x
Reference in New Issue
Block a user