Add niceties to time page

This commit is contained in:
Martin Pander
2026-02-02 12:44:12 +01:00
parent 9940316ace
commit 81b9d87935
6 changed files with 395 additions and 26 deletions

View File

@ -20,6 +20,7 @@ type Interval struct {
Start string `json:"start,omitempty"`
End string `json:"end,omitempty"`
Tags []string `json:"tags,omitempty"`
IsGap bool `json:"-"` // True if this represents an untracked time gap
}
func NewInterval() *Interval {
@ -28,7 +29,31 @@ func NewInterval() *Interval {
}
}
// NewGapInterval creates a new gap interval representing untracked time.
// start and end are the times between which the gap occurred.
func NewGapInterval(start, end time.Time) *Interval {
return &Interval{
ID: -1, // Gap intervals have no real ID
Start: start.UTC().Format(dtformat),
End: end.UTC().Format(dtformat),
Tags: make([]string, 0),
IsGap: true,
}
}
func (i *Interval) GetString(field string) string {
// Special handling for gap intervals
if i.IsGap {
switch field {
case "duration":
return i.GetDuration()
case "gap_display":
return fmt.Sprintf("--- Untracked: %s ---", i.GetDuration())
default:
return ""
}
}
switch field {
case "id":
return strconv.Itoa(i.ID)
@ -36,12 +61,24 @@ func (i *Interval) GetString(field string) string {
case "start":
return formatDate(i.Start, "formatted")
case "start_time":
return formatDate(i.Start, "time")
case "end":
if i.End == "" {
return "now"
}
return formatDate(i.End, "formatted")
case "end_time":
if i.End == "" {
return "now"
}
return formatDate(i.End, "time")
case "weekday":
return formatDate(i.Start, "weekday")
case "tags":
if len(i.Tags) == 0 {
return ""
@ -154,6 +191,8 @@ func formatDate(date string, format string) string {
return dt.Format("15:04")
case "date":
return dt.Format("2006-01-02")
case "weekday":
return dt.Format("Mon")
case "iso":
return dt.Format("2006-01-02T150405Z")
case "epoch":
@ -173,10 +212,7 @@ func formatDuration(d time.Duration) string {
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)
return fmt.Sprintf("%02d:%02d:%02d", hours, minutes, seconds)
}
func parseDurationVague(d time.Duration) string {

View File

@ -31,6 +31,7 @@ type TimeWarrior interface {
CancelTracking() error
DeleteInterval(id int) error
FillInterval(id int) error
JoinInterval(id int) error
ModifyInterval(interval *Interval, adjust bool) error
GetSummary(filter ...string) string
GetActive() *Interval
@ -232,6 +233,21 @@ func (ts *TimeSquire) FillInterval(id int) error {
return nil
}
func (ts *TimeSquire) JoinInterval(id int) error {
ts.mutex.Lock()
defer ts.mutex.Unlock()
// Join the current interval with the previous one
// The previous interval has id+1 (since intervals are ordered newest first)
cmd := exec.Command(twBinary, append(ts.defaultArgs, []string{"join", fmt.Sprintf("@%d", id+1), fmt.Sprintf("@%d", id)}...)...)
if err := cmd.Run(); err != nil {
slog.Error("Failed joining interval:", err)
return err
}
return nil
}
func (ts *TimeSquire) ModifyInterval(interval *Interval, adjust bool) error {
ts.mutex.Lock()
defer ts.mutex.Unlock()