Skip to content

Instantly share code, notes, and snippets.

@nf
Last active January 12, 2016 21:14
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save nf/7d378cb9417144caf272 to your computer and use it in GitHub Desktop.
Save nf/7d378cb9417144caf272 to your computer and use it in GitHub Desktop.
'spent' script to log where time is spent
package main
import (
"bufio"
"fmt"
"net/url"
"os"
"regexp"
"sort"
"strconv"
"strings"
"time"
)
const idleSecs = 120 // seconds to be regarded as "idle"
var (
lineRe = regexp.MustCompile(`^([^ ]+ [^ ]+) ([^ ]+) ([^,]+), (.*)$`)
webPrefixes = []string{
"Google Calendar",
"Google.com - Calendar",
"Twitter",
}
webSuffixes = []string{
"Code Review",
"Gmail",
"Google Docs",
"Google Drive",
"Google+",
"Google.com Mail",
"YouTube",
}
)
func main() {
f, err := os.Open("spent.log")
check(err)
defer f.Close()
s := bufio.NewScanner(f)
counts := map[string]int{}
for s.Scan() {
m := lineRe.FindStringSubmatch(s.Text())
if m == nil {
continue
}
ts, d, app, title := m[1], m[2], m[3], m[4]
secs, _ := strconv.ParseFloat(d, 63)
if secs > idleSecs {
// Ignore idle state.
continue
}
classify:
switch app {
case "Google Chrome", "Chrome":
var host string
if i := strings.LastIndex(title, " "); i > -1 {
if u, err := url.Parse(title[i+1:]); err == nil && u.Host != "" {
host = u.Host
title = title[:i]
}
}
for _, s := range webPrefixes {
if strings.HasPrefix(title, s) {
counts[s]++
break classify
}
}
for _, s := range webSuffixes {
if strings.HasSuffix(title, s) {
counts[s]++
break classify
}
}
if host != "" {
counts["Web: "+host]++
break classify
}
counts["Web: unknown"]++
default:
counts[app]++
}
_ = ts
}
var data []datum
var total int
for label, count := range counts {
data = append(data, datum{label, count})
total += count
}
sort.Sort(dataByCount(data))
fmt.Println("Time tracked:", time.Duration(total)*10*time.Second)
for _, d := range data {
fmt.Printf("%v %s\n", time.Duration(d.count)*10*time.Second, d.label)
}
}
type datum struct {
label string
count int
}
type dataByCount []datum
func (s dataByCount) Len() int { return len(s) }
func (s dataByCount) Less(i, j int) bool { return s[i].count < s[j].count }
func (s dataByCount) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
func check(err error) {
if err != nil {
panic(err)
}
}
#!/usr/bin/osascript
global frontApp, frontAppName, windowTitle
set windowTitle to ""
tell application "System Events"
set frontApp to first application process whose frontmost is true
set frontAppName to name of frontApp
tell process frontAppName
tell (1st window whose value of attribute "AXMain" is true)
set windowTitle to value of attribute "AXTitle"
end tell
end tell
if frontAppName is "Google Chrome" then
tell application "Google Chrome"
set windowTitle to windowTitle & " " & URL of active tab of first window
end tell
end if
end tell
tell application "System Events" to set the saveractivated to (exists process "ScreenSaverEngine")
if saveractivated then
return "Screen saver active"
else
return {frontAppName, windowTitle}
end if
#!/bin/bash
while true; do
idletime=$(/usr/sbin/ioreg -c IOHIDSystem | /usr/bin/awk '/HIDIdleTime/ {print $NF/1000000000; exit}')
echo $(date '+%F %H:%M:%S') $idletime $(front) >> ~/spent.log
sleep 10
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment