5 Commits
0.0.2 ... 0.0.5

Author SHA1 Message Date
e7fa789b2b Clean up display, add focus indication
All checks were successful
CI / test (push) Successful in 1m33s
2025-06-20 12:11:17 +02:00
d7f0ac16d4 Show version info
All checks were successful
CI / test (push) Successful in 1m10s
2025-06-19 10:34:35 +02:00
f9fddf496a Show how long the fetches took
All checks were successful
CI / test (push) Successful in 58s
2025-06-19 10:04:43 +02:00
f6a92bc99e Allow empty project submission, remove redundant customer list
All checks were successful
CI / test (push) Successful in 1m44s
2025-06-19 09:56:54 +02:00
1bf7aa8a66 Using goreleaser now, no need for the workflow build
All checks were successful
CI / test (push) Successful in 2m4s
2025-06-18 14:43:47 +02:00
4 changed files with 137 additions and 176 deletions

View File

@@ -40,83 +40,3 @@ jobs:
with: with:
name: coverage.html name: coverage.html
path: ./cover.html path: ./cover.html
build:
needs: 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 }}
# build all binaries
- name: build binaries
run: |
GOOS=darwin GOARCH=amd64 CGO_ENABLED=0 go build -o charmite_darwin_amd64 .
GOOS=darwin GOARCH=arm64 CGO_ENABLED=0 go build -o charmite_darwin_arm64 .
GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go build -o charmite_linux_amd64 .
GOOS=windows GOARCH=amd64 CGO_ENABLED=0 go build -o charmite_windows_amd64.exe .
# upload
- name: Upload artifacts to Gitea package repository
env:
GITEA_API_URL: https://code.ppl.town/api
GITEA_TOKEN: ${{ secrets.PACKAGE_API_KEY }}
OWNER: justin
VERSION: 0.0.1
run: |
# charmite_darwin_amd64
curl -D- -s -X DELETE "$GITEA_API_URL/packages/$OWNER/generic/charmite/$VERSION/charmite_darwin_amd64" \
-H "Authorization: token $GITEA_TOKEN"
curl -D- -s -X PUT "$GITEA_API_URL/packages/$OWNER/generic/charmite/$VERSION/charmite_darwin_amd64" \
-H "Authorization: token $GITEA_TOKEN" \
-H "Content-Type: application/octet-stream" \
--data-binary @charmite_darwin_amd64
curl -D- -s -X DELETE "$GITEA_API_URL/packages/$OWNER/generic/charmite/$VERSION/charmite_darwin_arm64" \
-H "Authorization: token $GITEA_TOKEN"
curl -D- -s -X PUT "$GITEA_API_URL/packages/$OWNER/generic/charmite/$VERSION/charmite_darwin_arm64" \
-H "Authorization: token $GITEA_TOKEN" \
-H "Content-Type: application/octet-stream" \
--data-binary @charmite_darwin_arm64
curl -D- -s -X DELETE "$GITEA_API_URL/packages/$OWNER/generic/charmite/$VERSION/charmite_linux_amd64" \
-H "Authorization: token $GITEA_TOKEN"
curl -D- -s -X PUT "$GITEA_API_URL/packages/$OWNER/generic/charmite/$VERSION/charmite_linux_amd64" \
-H "Authorization: token $GITEA_TOKEN" \
-H "Content-Type: application/octet-stream" \
--data-binary @charmite_linux_amd64
curl -D- -s -X DELETE "$GITEA_API_URL/packages/$OWNER/generic/charmite/$VERSION/charmite_windows_amd64.exe" \
-H "Authorization: token $GITEA_TOKEN"
curl -D- -s -X PUT "$GITEA_API_URL/packages/$OWNER/generic/charmite/$VERSION/charmite_windows_amd64.exe" \
-H "Authorization: token $GITEA_TOKEN" \
-H "Content-Type: application/octet-stream" \
--data-binary @charmite_windows_amd64.exe

View File

@@ -51,7 +51,7 @@ 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
@@ -231,8 +231,8 @@ 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"`
} }

137
model.go
View File

@@ -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,6 +320,7 @@ type miteDataFetchedMsg struct {
func (m model) fetchMiteData() tea.Cmd { func (m model) fetchMiteData() tea.Cmd {
return func() tea.Msg { return func() tea.Msg {
t0 := time.Now()
from := time.Now().Add(-time.Hour * 24 * 30 * 6) // about 6 months from := time.Now().Add(-time.Hour * 24 * 30 * 6) // about 6 months
to := time.Now().Add(time.Hour * 20 * 30) // about 1 month to := time.Now().Add(time.Hour * 20 * 30) // about 1 month
te, err1 := m.miteAPI.GetTimeEntries(from, to) te, err1 := m.miteAPI.GetTimeEntries(from, to)
@@ -404,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,
} }
} }
} }
@@ -415,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{}
@@ -436,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")
} }
@@ -447,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")
} }
@@ -466,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)
statusMsg := strings.ReplaceAll(m.statusBarMessage, "\n", " ") statusMsg := strings.ReplaceAll(m.statusBarMessage, "\n", " ")
out += styleStatusBar.MarginTop(19 - sofar).Width(m.windowWidth).Render(statusMsg)
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
} }

View File

@@ -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
} }
} }
@@ -43,7 +47,7 @@ func (m model) updateForm(msg tea.Msg) (tea.Model, tea.Cmd) {
err := m.miteAPI.AddTimeEntry(m.dest.Format(time.DateOnly), int(minutesInt), description, int(projectIDInt), int(serviceIDInt)) 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" m.statusBarMessage = "Successfully logged time"
@@ -56,3 +60,87 @@ 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(10)
form := huh.NewForm(
huh.NewGroup(
pl,
sl,
),
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()),
}
}