Skip to content

Commit ae3739b

Browse files
authored
Merge pull request #3 from mark0smith/dev
merge dev
2 parents 9aa460b + f91cc32 commit ae3739b

File tree

11 files changed

+878
-15
lines changed

11 files changed

+878
-15
lines changed

.github/workflows/go.yml

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# This workflow will build a golang project
2+
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-go
3+
4+
name: Go
5+
6+
on:
7+
release:
8+
types: [created]
9+
10+
permissions:
11+
contents: write
12+
packages: write
13+
14+
jobs:
15+
releases-matrix:
16+
name: Release Go Binary
17+
runs-on: ubuntu-latest
18+
strategy:
19+
matrix:
20+
# build and publish in parallel: linux/386, linux/amd64, linux/arm64, windows/386, windows/amd64, darwin/amd64, darwin/arm64
21+
goos: [linux, windows, darwin]
22+
goarch: ["386", amd64, arm64]
23+
exclude:
24+
- goarch: "386"
25+
goos: darwin
26+
- goarch: arm64
27+
goos: windows
28+
steps:
29+
- uses: actions/checkout@v3
30+
- uses: wangyoucao577/go-release-action@v1
31+
with:
32+
github_token: ${{ secrets.TOKEN }}
33+
goos: ${{ matrix.goos }}
34+
goarch: ${{ matrix.goarch }}
35+
goversion: "https://dl.google.com/go/go1.21.2.linux-amd64.tar.gz"
36+
project_path: "."
37+
binary_name: "goFindMyself"
38+
extra_files: README.md

.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818
# vendor/
1919

2020
# Go workspace file
21-
go.work
21+
# go.work
2222

2323
.vscode
24+
*.txt
25+
hello
26+
*.db

README.md

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,84 @@
11
# goFindMyself
22
For Golang learning and memory practices.
3+
4+
## Usage
5+
```
6+
> ./hello -h
7+
Usage of ./hello:
8+
-hint int
9+
If set, when recall test failes, hint will be given.
10+
0 for no hint, 1 for diff hint, 2 for full hint
11+
-m int
12+
Generated number wont't be bigger than this number. (default 100)
13+
-n int
14+
Number of Random Numbers. (default 30)
15+
-r rememberLogfile
16+
If set, generated numbers will be logged into rememberLogfile.
17+
You should set this if you want to do recall test later!
18+
-r_file string
19+
Filename of remember log (default "log.txt")
20+
-recall
21+
If set, run a recall test, instead of generating random numbers.
22+
-recallLog recallLogfile
23+
If set, recall info will be logged into recallLogfile. (default true)
24+
-recall_file string
25+
Filename of recall log (default "recall_log.txt")
26+
-u If set, all generated numbers will be unique. (default true)
27+
```
28+
29+
If running without any parameters, this program generates 30 random numbers in range of (0,100).
30+
```
31+
> ./hello
32+
2023-10-05 22:06:51 Random Number is [48 17 29 83 97 96 14 44 78 74 35 90 58 38 68 33 77 63 85 2 3 25 81 84 9 32 61 5 53 37]
33+
```
34+
35+
You can specific numbers count by `-n` and range maxium by `-m`.
36+
```
37+
> ./hello -n 4 -m 10
38+
2023-10-05 22:08:28 Random Number is [3 0 6 1]
39+
```
40+
41+
## Memory Practice
42+
43+
If you want to log these numbers for later recalling test, you should specific `-r`, thus numbers will be written into `log.txt`.
44+
You can Specific filename by `-r_file`.
45+
```
46+
> ./hello -r
47+
2023-10-05 22:10:58 Random Number is [64 57 8 54 79 15 97 39 37 7 13 11 67 6 78 80 36 60 32 83 70 59 25 27 92 35 9 75 1 22]
48+
> tail -n1 log.txt
49+
2023-10-05 22:10:58 [64 57 8 54 79 15 97 39 37 7 13 11 67 6 78 80 36 60 32 83 70 59 25 27 92 35 9 75 1 22]
50+
```
51+
52+
If you believe you have remember these numbers and wanna have a recall test, you can add `-recall`.
53+
If `-hint int` is set, some hint will be given if you are wrong, 0 for no hint, 1 for diff hint, 2 for full hint
54+
55+
```
56+
> ./hello -recall
57+
What do you remember?
58+
13 40 70 56 96 55 82 63 92 71 85 83 46 1 4 7 67 87 61 57 81 18 72 58 31 36 52 53 78
59+
60+
You have entered: 13 40 70 56 96 55 82 63 92 71 85 83 46 1 4 7 67 87 61 57 81 18 72 58 31 36 52 53 78
61+
Are you sure you remember it right?
62+
> ./hello -recall -hint 1
63+
What do you remember?
64+
13 40 70 56 96 55 82 63 92 71 85 83 46 1 4 7 67 87 61 57 81 18 72 58 31 36 52 53 78
65+
66+
You have entered: 13 40 70 56 96 55 82 63 92 71 85 83 46 1 4 7 67 87 61 57 81 18 72 58 31 36 52 53 78
67+
Are you sure you remember it right?
68+
69+
Hint Part:
70+
You are missing these numbers: 26
71+
You add these numbers which should't exist:
72+
> ./hello -recall -hint 2
73+
What do you remember?
74+
13 40 70 56 96 55 82 63 92 71 85 83 46 1 4 7 67 87 61 57 81 18 72 58 31 36 52 53 78
75+
76+
You have entered: 13 40 70 56 96 55 82 63 92 71 85 83 46 1 4 7 67 87 61 57 81 18 72 58 31 36 52 53 78
77+
Are you sure you remember it right?
78+
79+
Hint Part:
80+
The Right: 13 40 70 56 96 55 82 63 92 71 26 85 83 46 1 4 7 67 87 61 57 81 18 72 58 31 36 52 53 78
81+
The Wrong: 13 40 70 56 96 55 82 63 92 71 85 83 46 1 4 7 67 87 61 57 81 18 72 58 31 36 52 53 78
82+
```
83+
84+

go.mod

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1-
module example/hello
1+
module findmyself
22

33
go 1.21.1
4+
5+
require github.com/fatih/color v1.15.0
6+
7+
require (
8+
github.com/mattn/go-colorable v0.1.13 // indirect
9+
github.com/mattn/go-isatty v0.0.17 // indirect
10+
golang.org/x/sys v0.6.0 // indirect
11+
)

go.sum

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
github.com/fatih/color v1.15.0 h1:kOqh6YHBtK8aywxGerMG2Eq3H6Qgoqeo13Bk2Mv/nBs=
2+
github.com/fatih/color v1.15.0/go.mod h1:0h5ZqXfHYED7Bhv2ZJamyIOUej9KtShiJESRwBDUSsw=
3+
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
4+
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
5+
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
6+
github.com/mattn/go-isatty v0.0.17 h1:BTarxUcIeDqL27Mc+vyvdWYSL28zpIhv3RoTdsLMPng=
7+
github.com/mattn/go-isatty v0.0.17/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
8+
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
9+
golang.org/x/sys v0.6.0 h1:MVltZSvRTcU2ljQOhs94SXPftV6DCNnZViHeQps87pQ=
10+
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=

go.work

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
go 1.21.2
2+
3+
use (
4+
.
5+
./utils
6+
)

hello.go

Lines changed: 92 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,103 @@
11
package main
22

33
import (
4+
"flag"
45
"fmt"
5-
"math/rand"
6+
"log"
7+
"strings"
8+
"time"
9+
10+
"utils"
11+
12+
"github.com/fatih/color"
613
)
714

8-
func generateUniqueRandomNumbers(n, max int) []int {
9-
set := make(map[int]bool)
10-
var result []int
11-
for len(set) < n {
12-
value := rand.Intn(max)
13-
if !set[value] {
14-
set[value] = true
15-
result = append(result, value)
16-
}
15+
// recall func
16+
func checkRecall(rememberLogfile string, recallLogfile string, recallLog bool, showhint int, dbfile string) {
17+
18+
fmt.Println("What do you remember?")
19+
recallString := utils.ReadAndFormat()
20+
fmt.Printf("\nYou have entered: %s\n", recallString)
21+
22+
// toCheck := fmt.Sprintf("[%s]", recallString)
23+
// correctStr := utils.FindContentInFile(rememberLogfile, recallString)
24+
correctStr := utils.FindContentInDB(dbfile, recallString)
25+
recallResult := utils.CompareHint(dbfile, recallString, correctStr, showhint, true)
26+
27+
datetime := time.Now()
28+
datetimeFormatted := datetime.Format("2006-01-02 15:04:05")
29+
utils.AddRecalls(dbfile, []string{datetimeFormatted}, []string{recallString}, []string{fmt.Sprintf("%t", recallResult)})
30+
31+
if recallLog {
32+
datetime := time.Now()
33+
datetimeFormatted := datetime.Format("2006-01-02 15:04:05")
34+
info := fmt.Sprintf("%s Recall: %s, Result: %v\n", datetimeFormatted, recallString, recallResult)
35+
utils.WriteInfo(recallLogfile, info)
1736
}
18-
return result
1937
}
2038

2139
func main() {
22-
uniqueRandomNumbers := generateUniqueRandomNumbers(10, 100)
23-
fmt.Printf("Random Number is %d\n", uniqueRandomNumbers)
40+
log.SetFlags(log.Ldate | log.Ltime | log.Lmicroseconds | log.Llongfile)
41+
42+
num := flag.Int("n", 30, "Number of Random Numbers.")
43+
maxium := flag.Int("m", 100, "Generated number wont't be bigger than this number.")
44+
unique := flag.Bool("u", true, "If set, all generated numbers will be unique.")
45+
remember := flag.Bool("r", false, "If set, generated numbers will be logged into `rememberLogfile`.\nYou should set this if you want to do recall test later!")
46+
rememberLogfile := flag.String("r_file", "log.txt", "Filename of remember log")
47+
recall := flag.Bool("recall", false, "If set, run a recall test, instead of generating random numbers.")
48+
recallLog := flag.Bool("recallLog", true, "If set, recall info will be logged into `recallLogfile`.")
49+
recallLogfile := flag.String("recall_file", "recall_log.txt", "Filename of recall log")
50+
recallShowHint := flag.Int("hint", 0, "If set, when recall test failes, hint will be given.\n0 for no hint, 1 for diff hint, 2 for full hint")
51+
dbFilename := flag.String("db", "data.db", "Filename of database")
52+
migratedb := flag.Bool("migratedb", true, "If set, recall info will be logged into `recallLogfile`.")
53+
54+
flag.Parse()
55+
56+
if *migratedb {
57+
utils.InitDB(*rememberLogfile, *recallLogfile, *dbFilename)
58+
}
59+
60+
if *recall {
61+
defer utils.Timer("checkRecall")()
62+
checkRecall(*rememberLogfile, *recallLogfile, *recallLog, *recallShowHint, *dbFilename)
63+
return
64+
}
65+
66+
datetime := time.Now()
67+
datetimeFormatted := datetime.Format("2006-01-02 15:04:05")
68+
69+
uniqueRandomNumbers := utils.GenerateRandomNumbers(*num, *maxium, *unique)
70+
71+
// format output string, making it easier to remember
72+
var outputStr string
73+
boldBlue := color.New(color.Bold, color.FgHiBlue).SprintFunc()
74+
for idx, val := range uniqueRandomNumbers {
75+
info := ""
76+
if (idx+1)%5 == 0 {
77+
info = fmt.Sprintf(" %-2s", boldBlue(fmt.Sprintf("%-2d", val)))
78+
if (idx+1)%10 == 0 {
79+
info += "\n"
80+
}
81+
} else {
82+
if (idx+1)%10 == 1 {
83+
info = fmt.Sprintf("%-2d", val)
84+
} else {
85+
info = fmt.Sprintf(" %-2d", val)
86+
}
87+
}
88+
outputStr += info
89+
}
90+
91+
fmt.Printf("%s Random Number Generated:\n%s\n", datetimeFormatted, outputStr)
92+
93+
// type of remember is a pointer, add `*` prefix to get its value
94+
if *remember {
95+
info := fmt.Sprintf("%s %d\n", datetimeFormatted, uniqueRandomNumbers)
96+
filename := *rememberLogfile
97+
utils.WriteInfo(filename, info)
98+
numbersStr := fmt.Sprintf("%v", uniqueRandomNumbers)
99+
numbersStr = strings.TrimPrefix(numbersStr, "[")
100+
numbersStr = strings.TrimSuffix(numbersStr, "]")
101+
utils.AddNumbers(*dbFilename, []string{datetimeFormatted}, []string{numbersStr})
102+
}
24103
}

0 commit comments

Comments
 (0)