-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.go
104 lines (92 loc) · 1.82 KB
/
main.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package main
import (
"flag"
"fmt"
"os"
"strings"
"github.com/briandowns/super-hacker/templates"
term "github.com/nsf/termbox-go"
)
var (
name string
version string
gitSHA string
)
// defaultBufferSize holds the default size, in bytes,
// of the output buffer.
const defaultBufferSize = 3
const usage = `version: %s - git: %s
Usage: %s [-blvh]
Options:
-h help menu
-v show version
-b output buffer size
-l language [default: Go]
Examples:
%[3]s -b 24
`
// reset resets the terminal back to defaults.
func reset() {
term.Sync()
}
func main() {
flag.Usage = func() {
w := os.Stderr
for _, arg := range os.Args {
if arg == "-h" {
w = os.Stdout
break
}
}
fmt.Fprintf(w, usage, version, gitSHA, name)
}
var vers bool
var bufSize int
var lang string
flag.BoolVar(&vers, "v", false, "")
flag.IntVar(&bufSize, "b", defaultBufferSize, "")
flag.StringVar(&lang, "l", "go", "")
flag.Parse()
if vers {
fmt.Fprintf(os.Stdout, "version: %s - %s\n", version, gitSHA)
return
}
if err := term.Init(); err != nil {
fmt.Println(err)
return
}
defer term.Close()
code, err := templates.Random(lang)
if err != nil {
fmt.Println(err)
return
}
codeBuf := strings.NewReader(code)
buffer := make([]byte, bufSize)
keyPressListenerLoop:
for {
switch ev := term.PollEvent(); ev.Type {
case term.EventKey:
switch ev.Key {
case term.KeyEsc, term.KeyCtrlC:
reset()
break keyPressListenerLoop
default:
read, err := codeBuf.Read(buffer)
if err != nil {
code, err = templates.Random(lang)
if err != nil {
fmt.Println(err)
return
}
codeBuf = strings.NewReader(code)
goto keyPressListenerLoop
}
fmt.Print(string(buffer[:read]))
}
case term.EventError:
fmt.Println(ev.Err)
return
}
}
}