-
Notifications
You must be signed in to change notification settings - Fork 0
/
util_general.go
67 lines (55 loc) · 1.02 KB
/
util_general.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package main
import (
"bufio"
"fmt"
"io/ioutil"
"os"
"strings"
)
var userError = `Commands:
rich new <habit name> [streak]
rich delete <habit name>
rich mark <habit name> ...
rich todo
rich [list]
See the README for more information.`
func hdl(err error, message string) {
if err != nil {
printError(message)
}
}
func printError(message string) {
fmt.Fprintln(os.Stderr, message)
os.Exit(1)
}
func getDigits(num int) int {
var digits int
if num == 0 {
digits = 1
} else {
digits = 0
for num > 0 {
num /= 10
digits++
}
}
return digits
}
func writeLines(habitPath string, lines []string) {
err := ioutil.WriteFile(habitPath, []byte(strings.Join(lines, "\n")), 0644)
hdl(err, "Error: Couldn't write to habit file")
}
func getLine(habitPath string, i int) string {
file, err := os.Open(habitPath)
defer file.Close()
hdl(err, "Error: Couldn't read habit file")
line := 0
scanner := bufio.NewScanner(file)
for scanner.Scan() {
if line == i {
return scanner.Text()
}
line++
}
return ""
}