Skip to content

Instantly share code, notes, and snippets.

@ShawnMilo

ShawnMilo/logtime.go Secret

Created Jan 17, 2018
Embed
What would you like to do?
/*
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