mirror of
https://codeberg.org/hyperreal/hyperfocus
synced 2024-11-25 12:13:43 +01:00
Update takeBreak()
This commit is contained in:
parent
b8f3bacd74
commit
8ca9296146
@ -1,7 +1,12 @@
|
|||||||
# go-hyperfocus
|
# go-hyperfocus
|
||||||
|
|
||||||
> Work-In-Progress: Do not use this software. It doesn't work yet. There are likely to be logic errors, and some features aren't fully implemented.
|
> Work-In-Progress: Not tested on Windows; there are some file paths I need to fix before it can work.
|
||||||
|
|
||||||
Block distracting websites and hyperfocus on your tasks!
|
Block distracting websites and hyperfocus on your tasks!
|
||||||
|
|
||||||
This is a Go implementation of the Python-written [concentration](https://github.com/timothycrosley/concentration) program. This program aims to allow the user somewhat more freedom in defining distractors than its predecessor.
|
This is a Go implementation of the Python-written [concentration](https://github.com/timothycrosley/concentration) program. This program aims to allow the user somewhat more freedom in defining distractors than its predecessor.
|
||||||
|
|
||||||
|
## Installation
|
||||||
|
```bash
|
||||||
|
go install git.envs.net/hyperreal/go-hyperfocus@latest
|
||||||
|
```
|
||||||
|
127
main.go
127
main.go
@ -2,6 +2,7 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bufio"
|
"bufio"
|
||||||
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
@ -9,6 +10,7 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
"os/user"
|
"os/user"
|
||||||
|
"path/filepath"
|
||||||
"runtime"
|
"runtime"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@ -16,9 +18,8 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
distractorsFile string = "/etc/hyperfocus_distractors"
|
distractorsFile string = "/etc/hf_distractors"
|
||||||
predefDistractorsFile string = "/etc/hyperfocus_distractors_predefined"
|
predefDistractorsFile string = "/etc/hf_predef_distractors"
|
||||||
hostBackupFile string = "/etc/.backup_hosts"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const resetDNSMsg string = `
|
const resetDNSMsg string = `
|
||||||
@ -39,12 +40,12 @@ func fileExists(path string) bool {
|
|||||||
// Copy src to dest
|
// Copy src to dest
|
||||||
func copyFile(src string, dest string) error {
|
func copyFile(src string, dest string) error {
|
||||||
|
|
||||||
origFile, err := os.Open(src)
|
origFile, err := os.Open(filepath.Clean(src))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("could not open original file %s for copying: %s", src, err)
|
return fmt.Errorf("could not open original file %s for copying: %s", src, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
backupFile, err := os.OpenFile(dest, os.O_CREATE|os.O_WRONLY, 0666)
|
backupFile, err := os.OpenFile(filepath.Clean(dest), os.O_CREATE|os.O_WRONLY, 0666)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("could not open backup file %s for writing: %s", dest, err)
|
return fmt.Errorf("could not open backup file %s for writing: %s", dest, err)
|
||||||
}
|
}
|
||||||
@ -87,8 +88,8 @@ func isRoot() bool {
|
|||||||
func improveFocus() {
|
func improveFocus() {
|
||||||
|
|
||||||
// Backup host file if a backup does not already exist
|
// Backup host file if a backup does not already exist
|
||||||
if !fileExists(hostBackupFile) {
|
if !fileExists("/etc/.hosts.backup") {
|
||||||
if err := copyFile(getHostFile(), hostBackupFile); err != nil {
|
if err := copyFile(getHostFile(), "/etc/.hosts.backup"); err != nil {
|
||||||
log.Fatalln(err)
|
log.Fatalln(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -98,7 +99,11 @@ func improveFocus() {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalln(err)
|
log.Fatalln(err)
|
||||||
}
|
}
|
||||||
defer hostFile.Close()
|
defer func() {
|
||||||
|
if err := hostFile.Close(); err != nil {
|
||||||
|
log.Printf("Error closing file: %s\n", err)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
// For checking if /etc/hyperfocus_distractors or /etc/hyperfocus_distractors_predefined
|
// For checking if /etc/hyperfocus_distractors or /etc/hyperfocus_distractors_predefined
|
||||||
// exist.
|
// exist.
|
||||||
@ -107,15 +112,19 @@ func improveFocus() {
|
|||||||
predefDistractorsFileWarn bool
|
predefDistractorsFileWarn bool
|
||||||
)
|
)
|
||||||
|
|
||||||
// If /etc/hyperfocus_distractors exists, take each host from it and append it
|
// If /etc/hf_distractors exists, take each host from it and append it
|
||||||
// to /etc/hosts for blocking. Else set distractorsFileWarn to true.
|
// to /etc/hosts for blocking. Else set distractorsFileWarn to true.
|
||||||
if fileExists(distractorsFile) {
|
if fileExists(filepath.Clean(distractorsFile)) {
|
||||||
|
|
||||||
distractorsFileObj, err := os.Open(distractorsFile)
|
distractorsFileObj, err := os.Open(filepath.Clean(distractorsFile))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalln(err)
|
log.Fatalln(err)
|
||||||
}
|
}
|
||||||
defer distractorsFileObj.Close()
|
defer func() {
|
||||||
|
if err := distractorsFileObj.Close(); err != nil {
|
||||||
|
log.Printf("Error closing file: %s\n", err)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
scanner := bufio.NewScanner(distractorsFileObj)
|
scanner := bufio.NewScanner(distractorsFileObj)
|
||||||
for scanner.Scan() {
|
for scanner.Scan() {
|
||||||
@ -128,15 +137,19 @@ func improveFocus() {
|
|||||||
distractorsFileWarn = true
|
distractorsFileWarn = true
|
||||||
}
|
}
|
||||||
|
|
||||||
// If /etc/hyperfocus_distractors_predefined exists, take each host from it and,
|
// If /etc/hf_predef_distractors exists, take each host from it and,
|
||||||
// append it to /etc/hosts for blocking. Else set predefDistractorsFileWarn to true.
|
// append it to /etc/hosts for blocking. Else set predefDistractorsFileWarn to true.
|
||||||
if fileExists(predefDistractorsFile) {
|
if fileExists(filepath.Clean(predefDistractorsFile)) {
|
||||||
|
|
||||||
predefDistractorsFileObj, err := os.Open(predefDistractorsFile)
|
predefDistractorsFileObj, err := os.Open(filepath.Clean(predefDistractorsFile))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalln(err)
|
log.Fatalln(err)
|
||||||
}
|
}
|
||||||
defer predefDistractorsFileObj.Close()
|
defer func() {
|
||||||
|
if err := predefDistractorsFileObj.Close(); err != nil {
|
||||||
|
log.Printf("Error closing file: %s\n", err)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
scanner := bufio.NewScanner(predefDistractorsFileObj)
|
scanner := bufio.NewScanner(predefDistractorsFileObj)
|
||||||
for scanner.Scan() {
|
for scanner.Scan() {
|
||||||
@ -167,7 +180,7 @@ func loseFocus() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Restore the backup of /etc/hosts
|
// Restore the backup of /etc/hosts
|
||||||
if err := copyFile(hostBackupFile, getHostFile()); err != nil {
|
if err := copyFile("/etc/.hosts.backup", getHostFile()); err != nil {
|
||||||
log.Fatalln(err)
|
log.Fatalln(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -181,57 +194,48 @@ func takeBreak(minutes int) {
|
|||||||
fmt.Println("Your (probably) well-deserved break is commencing...")
|
fmt.Println("Your (probably) well-deserved break is commencing...")
|
||||||
loseFocus()
|
loseFocus()
|
||||||
|
|
||||||
// Create a channel to receive os.Interrupt signal from user
|
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt)
|
||||||
c := make(chan os.Signal, 1)
|
defer cancel()
|
||||||
signal.Notify(c, os.Interrupt)
|
|
||||||
|
|
||||||
// TODO: Read up on Goroutines and channels and figure how to 'pipe' the os.Signal
|
ctx, cancel = context.WithTimeout(ctx, time.Duration(minutes*60)*time.Second)
|
||||||
// channel to the goroutine below.
|
defer cancel()
|
||||||
|
|
||||||
// Prepare channel to receive signal. Sleep for t minus <minutes * 60> seconds,
|
<-ctx.Done()
|
||||||
// or break the countdown when interrupt signal is received from user, then
|
|
||||||
// improve focus again.
|
|
||||||
ticker := time.NewTicker(1 * time.Second)
|
|
||||||
done := make(chan bool)
|
|
||||||
|
|
||||||
go func() {
|
|
||||||
for {
|
|
||||||
select {
|
|
||||||
case <-done:
|
|
||||||
return
|
|
||||||
case t := <-ticker.C:
|
|
||||||
// TODO: Figure how to get the number of seconds as integers from t
|
|
||||||
fmt.Println(t)
|
|
||||||
case <-c:
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
|
|
||||||
time.Sleep(time.Duration(minutes*60) * time.Second)
|
|
||||||
ticker.Stop()
|
|
||||||
done <- true
|
|
||||||
|
|
||||||
improveFocus()
|
improveFocus()
|
||||||
fmt.Println("Welcome back to hyperfocus mode 😊 ")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Prints the current list of distractors to be blocked
|
// Prints the current list of distractors to be blocked
|
||||||
func listDistractors() {
|
func listDistractors() {
|
||||||
|
|
||||||
for _, v := range []string{distractorsFile, predefDistractorsFile} {
|
userDistractorsFile, err := os.Open(filepath.Clean(distractorsFile))
|
||||||
|
if err != nil {
|
||||||
userDistractorsFile, err := os.Open(v)
|
fmt.Println(err)
|
||||||
if err != nil {
|
}
|
||||||
fmt.Println(err)
|
defer func() {
|
||||||
|
if err := userDistractorsFile.Close(); err != nil {
|
||||||
|
log.Printf("Error closing file: %s\n", err)
|
||||||
}
|
}
|
||||||
defer userDistractorsFile.Close()
|
}()
|
||||||
|
|
||||||
scanner := bufio.NewScanner(userDistractorsFile)
|
scanner := bufio.NewScanner(userDistractorsFile)
|
||||||
|
for scanner.Scan() {
|
||||||
|
fmt.Println(scanner.Text())
|
||||||
|
}
|
||||||
|
|
||||||
for scanner.Scan() {
|
predefDistractorsFile, err := os.Open(filepath.Clean(predefDistractorsFile))
|
||||||
fmt.Println(scanner.Text())
|
if err != nil {
|
||||||
|
fmt.Println(err)
|
||||||
|
}
|
||||||
|
defer func() {
|
||||||
|
if err := predefDistractorsFile.Close(); err != nil {
|
||||||
|
log.Printf("Error closing file: %s\n", err)
|
||||||
}
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
scanner = bufio.NewScanner(predefDistractorsFile)
|
||||||
|
for scanner.Scan() {
|
||||||
|
fmt.Println(scanner.Text())
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -360,11 +364,15 @@ func addPredefinedDistractors() {
|
|||||||
"zillow.com",
|
"zillow.com",
|
||||||
}
|
}
|
||||||
|
|
||||||
predefDistractorsFileObj, err := os.OpenFile(predefDistractorsFile, os.O_CREATE|os.O_WRONLY, 0666)
|
predefDistractorsFileObj, err := os.OpenFile(filepath.Clean(predefDistractorsFile), os.O_CREATE|os.O_WRONLY, 0666)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalln(err)
|
log.Fatalln(err)
|
||||||
}
|
}
|
||||||
defer predefDistractorsFileObj.Close()
|
defer func() {
|
||||||
|
if err := predefDistractorsFileObj.Close(); err != nil {
|
||||||
|
log.Printf("Error closing file: %s\n", err)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
dataWriter := bufio.NewWriter(predefDistractorsFileObj)
|
dataWriter := bufio.NewWriter(predefDistractorsFileObj)
|
||||||
|
|
||||||
@ -372,7 +380,10 @@ func addPredefinedDistractors() {
|
|||||||
|
|
||||||
_, _ = dataWriter.WriteString(v + "\n")
|
_, _ = dataWriter.WriteString(v + "\n")
|
||||||
}
|
}
|
||||||
dataWriter.Flush()
|
|
||||||
|
if err := dataWriter.Flush(); err != nil {
|
||||||
|
log.Fatalln(err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
Loading…
Reference in New Issue
Block a user