From cf3e061007d8cf6258ea0e764c32f80fdb585533 Mon Sep 17 00:00:00 2001 From: Jeffrey Serio Date: Wed, 29 Dec 2021 20:57:15 -0600 Subject: [PATCH] Add comments; update README.md --- README.md | 2 +- main.go | 25 +++++++++++++++++++------ 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index a3a67d1..8e5759a 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ 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 be cleaner and simpler than its predecessor. ## Installation ```bash diff --git a/main.go b/main.go index 7657ca9..b7686c9 100644 --- a/main.go +++ b/main.go @@ -105,7 +105,7 @@ func improveFocus() { } }() - // For checking if /etc/hyperfocus_distractors or /etc/hyperfocus_distractors_predefined + // For checking if /etc/hf_distractors or /etc/hf_distractors_predefined // exist. var ( distractorsFileWarn bool @@ -194,9 +194,11 @@ func takeBreak(minutes int) { fmt.Println("Your (probably) well-deserved break is commencing...") loseFocus() + // Create notify context for os.Interrupt signal ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt) defer cancel() + // Create context with timeout for duration of minutes argument ctx, cancel = context.WithTimeout(ctx, time.Duration(minutes*60)*time.Second) defer cancel() @@ -206,8 +208,11 @@ func takeBreak(minutes int) { } // Prints the current list of distractors to be blocked +// If neither /etc/hf_distractors nor /etc/hf_predef_distractors exist, +// print error message to console but do not exit. func listDistractors() { + // Open /etc/hf_distractors and store it as *os.File type userDistractorsFile, err := os.Open(filepath.Clean(distractorsFile)) if err != nil { fmt.Println(err) @@ -218,11 +223,14 @@ func listDistractors() { } }() + // Initialize a new scanner, scan /etc/hf_distractors, and print to + // stdout line by line. scanner := bufio.NewScanner(userDistractorsFile) for scanner.Scan() { fmt.Println(scanner.Text()) } + // Open /etc/hf_predef_distractors and store it as *os.File type predefDistractorsFile, err := os.Open(filepath.Clean(predefDistractorsFile)) if err != nil { fmt.Println(err) @@ -233,6 +241,8 @@ func listDistractors() { } }() + // Initialize a new scanner, scan /etc/hf_predef_distractors, and print to + // stdout line by line. scanner = bufio.NewScanner(predefDistractorsFile) for scanner.Scan() { fmt.Println(scanner.Text()) @@ -240,9 +250,10 @@ func listDistractors() { } -// Adds predefined distractors to .hyperfocus_distractors file +// Adds predefined distractors to /etc/hf_predef_distractors file func addPredefinedDistractors() { + // Slice of distractor domains distractors := []string{ "anandtech.com", "answerbag.com", @@ -364,6 +375,7 @@ func addPredefinedDistractors() { "zillow.com", } + // Open or create /etc/hf_predef_distractors and store it in *os.File type predefDistractorsFileObj, err := os.OpenFile(filepath.Clean(predefDistractorsFile), os.O_CREATE|os.O_WRONLY, 0666) if err != nil { log.Fatalln(err) @@ -374,8 +386,9 @@ func addPredefinedDistractors() { } }() + // Initialize a new writer, range loop over distractors slice, and write + // each distractor domain to /etc/hf_predef_distractors, line by line. dataWriter := bufio.NewWriter(predefDistractorsFileObj) - for _, v := range distractors { _, _ = dataWriter.WriteString(v + "\n") @@ -388,7 +401,7 @@ func addPredefinedDistractors() { func main() { - parser := argparse.NewParser("hf", "Block distracting websites and hyperfocus on your work") + parser := argparse.NewParser("go-hyperfocus", "Block distracting websites and hyperfocus on your work") improveCmd := parser.NewCommand("improve", "Improve focus, block the distractors") loseCmd := parser.NewCommand("lose", "Lose focus, unblock the distractors") @@ -396,8 +409,8 @@ func main() { breakCmd := parser.NewCommand("break", "Take a break for a number of minutes") minutesForBreak := breakCmd.Int("", "minutes", &argparse.Options{Help: "Number of minutes to break for."}) - listCmd := parser.NewCommand("list", "List the distractors defined in the block file (/etc/hyperfocus_distractors)") - predefinedCmd := parser.NewCommand("predefined", "Add predefined set of distractors to block file") + listCmd := parser.NewCommand("list", "List the distractors defined in the block file (/etc/hf_distractors)") + predefinedCmd := parser.NewCommand("predefined", "Add predefined set of distractors to /etc/hf_predef_distractors") if isRoot() {