Skip to content

Commit

Permalink
change column order
Browse files Browse the repository at this point in the history
  • Loading branch information
tillkuhn committed Jan 2, 2025
1 parent 6639d56 commit a31dbbc
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 14 deletions.
18 changes: 11 additions & 7 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ issues:
- text: "G404:" # Ignore weak random number generator lint. We do not need strong randomness here.
linters:
- gosec
- text: "G204:" # Ignore Subprocess launched with a potential tainted input or cmd arguments (uses a constant)
path: pkg/tracker/ioreg.go
linters:
- gosec
- path: _test\.go
linters:
- gochecknoglobals
Expand All @@ -57,16 +61,16 @@ issues:
- noctx
# this is the exception for commit, data and version variable which with pass in using ldflags
- path: internal/version/version.go
linters: [gochecknoglobals]
linters: [ gochecknoglobals ]
- path: cmd/punch.go
linters: [gochecknoglobals,mnd]
linters: [ gochecknoglobals,mnd ]
- path: cmd/report.go
linters: [gochecknoglobals,mnd]
linters: [ gochecknoglobals,mnd ]
- path: cmd/root.go
linters: [gochecknoglobals,mnd]
linters: [ gochecknoglobals,mnd ]
- path: cmd/track.go
linters: [gochecknoglobals,mnd]
linters: [ gochecknoglobals,mnd ]
- path: pkg/tracker/report.go
linters: [mnd] # magic number detection is annoying here
linters: [ mnd ] # magic number detection is annoying here
- path: pkg/tracker/tracker.go
linters: [mnd]
linters: [ mnd ]
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ OSNAME = $(shell uname -o)
PROJECT_PKG = $(shell grep -e ^module go.mod|cut -d' ' -f2|xargs)
# git info for ldflags inspired by https://github.com/oras-project/oras/blob/main/Makefile
GIT_COMMIT = $(shell git rev-parse --short HEAD)
GIT_TAG = $(shell git describe --tags --abbrev=0 2>/dev/null)
GIT_TAG = $(shell git describe --tags --abbrev=0 2>/dev/null)
#GIT_DIRTY = $(shell test -n "`git status --porcelain`" && echo "dirty" || echo "clean")
LDFLAGS = -w
LDFLAGS += -X main.cmmit=${GIT_COMMIT}
Expand Down
3 changes: 2 additions & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ var ctxCancel context.CancelFunc
var rootCmd = &cobra.Command{
Use: "billy-idle",
Short: "Simple busy / idle time tracker inspired by the ancient article 'Inactivity and Idle Time on OS X'.",
Long: `Simple busy / idle time tracker based on the macOS timer called HIDIdleTime that tracks the last time you interacted with the computer, e.g. moved the mouse, typed a key, or interacted with the computer.
Long: `Simple busy / idle time tracker based on the macOS timer called HIDIdleTime that tracks the last time you interacted with the computer,
e.g. moved the mouse, typed a key, or interacted with the computer.
billy-idle simply queries this value periodically using the ioreg utility that ships with macOS, and matches it against a pre-defined threshold.
If exceeded, it will create a record for the busy time period in database. This data can later be used as input for time tracking tools or statistics.`,
Expand Down
5 changes: 4 additions & 1 deletion pkg/tracker/ioreg.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,15 @@ import (
"time"
)

// limitDepth is passed to "-d" arg (limit tree to the given depth) to reduce amount of output
const limitDepth = 4 // 4 should be sufficient to catch relevant HIDIdleTime figures

var idleMatcher = regexp.MustCompile("\"HIDIdleTime\"\\s*=\\s*(\\d+)")

// IdleTime gets the current idle time (HIDIdleTime) in milliseconds from the external ioreg command
// todo optimize by limit depth, e.g. -d 4 should be sufficient ... -d limit tree to the given depth
func IdleTime(ctx context.Context, cmd string) (int64, error) {
cmdExec := exec.CommandContext(ctx, cmd, "-d", "4", "-c", "IOHIDSystem")
cmdExec := exec.CommandContext(ctx, cmd, "-d", strconv.Itoa(limitDepth), "-c", "IOHIDSystem")
stdout, err := cmdExec.Output()
if err != nil {
return 0, err
Expand Down
8 changes: 4 additions & 4 deletions pkg/tracker/punch.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func (t *Tracker) PunchReport(ctx context.Context) error {
var spentBusyTotal, plannedBusyTotal time.Duration
table := tablewriter.NewWriter(t.opts.Out)
bold := tablewriter.Colors{tablewriter.Bold}
table.SetHeader([]string{"🕰 Date", "CW", "Weekday", "🐝 Busy", "⏲️ Planned", "Overtime"})
table.SetHeader([]string{"CW", "📅 Date", "Weekday", "🐝 Busy", "⏲️ Plan", "🕰 Overtime"})
table.SetHeaderColor(bold, bold, bold, bold, bold, bold)
table.SetBorder(false)
table.SetAlignment(tablewriter.ALIGN_LEFT)
Expand All @@ -46,8 +46,8 @@ func (t *Tracker) PunchReport(ctx context.Context) error {
curWeek = week

table.Append([]string{
r.Day.Format(" 2006-01-02"),
strconv.Itoa(week),
r.Day.Format(" 2006-01-02"),
r.Day.Format("Monday"),
FDur(spentDay),
FDur(plannedDay),
Expand Down Expand Up @@ -100,7 +100,7 @@ func (t *Tracker) UpsertPunchRecordWithPlannedDuration(ctx context.Context, busy
}

// No update - let's insert a new row
iQuery := `INSERT INTO ` + tablePunch + ` (day,busy_secs,client,planned_secs) VALUES ($1,$2,$3,$4) RETURNING id`
iQuery := `INSERT ` + `INTO ` + tablePunch + ` (day,busy_secs,client,planned_secs) VALUES ($1,$2,$3,$4) RETURNING id`
var id int
if err := t.db.QueryRowContext(ctx, iQuery, day, busyDuration.Seconds(), t.opts.ClientID, plannedDuration.Seconds()).Scan(&id); err != nil {
return errors.Wrap(err, "unable to insert new record in busy table")
Expand All @@ -113,7 +113,7 @@ func (t *Tracker) UpsertPunchRecordWithPlannedDuration(ctx context.Context, busy
func (t *Tracker) PunchRecords(ctx context.Context) ([]PunchRecord, error) {
// select sum(ROUND((JULIANDAY(busy_end) - JULIANDAY(busy_start)) * 86400)) || ' secs' AS total from track
// current month: select * from punch where substr(day, 6, 2) = strftime('%m', 'now')
query := `SELECT day,busy_secs,planned_secs FROM ` + tablePunch + ` WHERE substr(day, 6, 2) = strftime('%m', 'now') ` +
query := `SELECT day,busy_secs,planned_secs ` + `FROM ` + tablePunch + ` WHERE substr(day, 6, 2) = strftime('%m', 'now') ` +
`ORDER BY DAY` // WHERE busy_start >= DATE('now', '-7 days') ORDER BY busy_start LIMIT 500`
// We could use get since we expect a single result, but this would return an error if nothing is found
// which is a likely use case
Expand Down

0 comments on commit a31dbbc

Please sign in to comment.