Skip to content

Instantly share code, notes, and snippets.

@ShawnMilo
Created January 17, 2018 06:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ShawnMilo/f6b3083c8a73c61f09bc6fa2ad4cdc6e to your computer and use it in GitHub Desktop.
Save ShawnMilo/f6b3083c8a73c61f09bc6fa2ad4cdc6e to your computer and use it in GitHub Desktop.
/*
Runner for mousepad time tracker.
*/
package main
import (
"fmt"
"io/ioutil"
"log"
"os"
"os/exec"
"strings"
"time"
)
var logFile string
func init() {
var timestamp = time.Now()
for {
if timestamp.Weekday() == time.Monday {
break
}
timestamp = timestamp.Add(time.Hour * -24)
}
logFile = fmt.Sprintf("/home/shawn/Documents/time_%s.log", timestamp.Format("2006-01-02"))
}
func main() {
log.SetOutput(os.Stdout)
if alreadyRunning() || screenLocked() || playingTetris() {
return
}
addTimestamp()
cmd := exec.Command("mousepad", "--display", ":0", logFile)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err := cmd.Run()
if err != nil {
log.Printf("Failed to run mousepad: %s\n", err)
}
}
func alreadyRunning() bool {
cmd := exec.Command("pgrep", "-lf", logFile)
err := cmd.Run()
if err != nil {
return false
}
return true
}
func screenLocked() bool {
cmd := exec.Command("pgrep", "i3lock")
err := cmd.Run()
if err != nil {
return false
}
return true
}
func playingTetris() bool {
cmd := exec.Command("pgrep", "fceux")
err := cmd.Run()
if err != nil {
return false
}
return true
}
func addTimestamp() {
data, err := ioutil.ReadFile(logFile)
if err != nil {
log.Printf("Unable to read file: %s\n", err)
data = []byte{}
}
stamp := time.Now().Format("2006-01-02 15:04:05")
data = append(data, []byte(fmt.Sprintf("\n\n%s\n\n%s\n\n", strings.Repeat("-", 100), stamp))...)
err = ioutil.WriteFile(logFile, data, 0644)
if err != nil {
log.Fatalf("Unable to write file: %s\n", err)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment