Public » Fortune Read More
Clone URL:  
Pushed to one repository · View In Graph Contained in master

initial commit

Changeset 4b2468315be1

committed by Profile picture of Benjamin PollackBenjamin Pollack

Profile picture of Benjamin Pollack authored by Benjamin Pollack

Changes to 5 files · Browse files at 4b2468315be1 Diff from another changeset...

Change 1 of 1 Show Entire File .gitignore Stacked
 
 
 
1
Change 1 of 1 Show Entire File README Stacked
 
 
 
 
1
2
@@ -1,0 +1,2 @@
+This is a quick reimplementation of Fortune in Go, complete with the FreeBSD +normal and offensive fortune files.
Show Entire File freebsd.fortunes Stacked
This file's diff was not loaded because this changeset is very large. Load changes
Change 1 of 1 Show Entire File main.go Stacked
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
@@ -1,0 +1,67 @@
+package main + +import ( + "bufio" + "flag" + "fmt" + "io" + "math/rand" + "os" + "time" +) + +const ( + NORMAL = "freebsd.fortunes" + OFFENSIVE = "offensive.fortunes" +) + +func readFortunes(filename string) (fortunes []string, err error) { + fortuneFile, err := os.Open(filename) + if err != nil { + return + } + defer fortuneFile.Close() + + reader := bufio.NewReader(fortuneFile) + fortunes = make([]string, 0, 100) + currentFortune := "" + for line, err := reader.ReadString('\n'); err == nil; line, err = reader.ReadString('\n') { + if line[0] == '%' { + fortunes = append(fortunes, currentFortune) + currentFortune = "" + } else { + currentFortune += line + } + } + + if len(currentFortune) > 0 { + fortunes = append(fortunes, currentFortune) + } + + if err == io.EOF { + err = nil + } + + return +} + +func main() { + offensive := flag.Bool("o", false, "be offensive") + flag.Parse() + fortunes, err := readFortunes(NORMAL) + if err != nil { + fmt.Fprintf(os.Stderr, "unable to load normal fortunes: %v\n", err) + os.Exit(1) + } + if *offensive { + offensiveFortunes, err := readFortunes(OFFENSIVE) + if err != nil { + fmt.Fprintf(os.Stderr, "unable to load offensive fortunes: %v\n", err) + os.Exit(1) + } + fortunes = append(fortunes, offensiveFortunes...) + } + + rand.Seed(time.Now().UnixNano()) + fmt.Print(fortunes[rand.Intn(len(fortunes))]) +}
Show Entire File offensive.fortunes Stacked
This file's diff was not loaded because this changeset is very large. Load changes