Add comments and optimizing gocode
This commit is contained in:
parent
20df2f2a9f
commit
cc45805a87
@ -5,6 +5,7 @@ import (
|
||||
"os/signal"
|
||||
)
|
||||
|
||||
// IT'S A TRAP! (This catches all signals so the program can't be interrupted)
|
||||
func signalCatcher() {
|
||||
c := make(chan os.Signal)
|
||||
signal.Notify(c)
|
||||
@ -13,6 +14,7 @@ func signalCatcher() {
|
||||
}
|
||||
}
|
||||
|
||||
// This is where it all starts
|
||||
func main() {
|
||||
go signalCatcher()
|
||||
showMenu()
|
||||
|
34
menu.go
34
menu.go
@ -10,17 +10,22 @@ import (
|
||||
)
|
||||
|
||||
const (
|
||||
ascii string = " `sMMMMMMMh:\n.-------..-------- -----------------.`sMMMMMMMd: \n`sNMMMMMhyMMMMMMMM/hdMMMMMMMMMMMMMMhhMMMMMMMd: \n `sNMMMhyMMMMMMMM+MNhhMMMMMMMMMMhhMMMMMMMd: \n `sNMhyMMMMMMMM+MMMNhhMMMMMMy-+ooooooo: \n `ohyMMMMMMMM+MMMMMNhhMMs. \n `........:MMMMMMMNy- \n :MMMMMMMMMNo` \n :MMMMMMMMMMMN: \n :MMMMMMMMMMd: \n :MMMMMMMMd: \n :MMMMMMd: \n :MMMMdhy \n :MMdhNMd \n :dhNMMMd \n `oNMMMMMd \n `+NMMMMMMMd \n `+NMMMMMMMMMd \n `dMMMMMMMMMMMd \n /mMMMMMMMMMd \n /mMMMMMMMd. \n /mMMMMMdyd: \n /mMMMhyMMd: \n /mMhyMMMMd: \n /syMMMMMMd/ "
|
||||
asciiLen int = 50
|
||||
tekst string = "Thalia Constitutieborrel Gastenboek\n1) Schrijf een nieuw bericht.\n2) Lees oude berichten."
|
||||
// ASCII Art of the Thalia Logo
|
||||
ascii string = " `sMMMMMMMh:\n.-------..-------- -----------------.`sMMMMMMMd: \n`sNMMMMMhyMMMMMMMM/hdMMMMMMMMMMMMMMhhMMMMMMMd: \n `sNMMMhyMMMMMMMM+MNhhMMMMMMMMMMhhMMMMMMMd: \n `sNMhyMMMMMMMM+MMMNhhMMMMMMy-+ooooooo: \n `ohyMMMMMMMM+MMMMMNhhMMs. \n `........:MMMMMMMNy- \n :MMMMMMMMMNo` \n :MMMMMMMMMMMN: \n :MMMMMMMMMMd: \n :MMMMMMMMd: \n :MMMMMMd: \n :MMMMdhy \n :MMdhNMd \n :dhNMMMd \n `oNMMMMMd \n `+NMMMMMMMd \n `+NMMMMMMMMMd \n `dMMMMMMMMMMMd \n /mMMMMMMMMMd \n /mMMMMMMMd. \n /mMMMMMdyd: \n /mMMMhyMMd: \n /mMhyMMMMd: \n /syMMMMMMd/ "
|
||||
// Width of the ASCII art
|
||||
asciiLen int = 50
|
||||
// Menu text
|
||||
tekst string = "Thalia Constitutieborrel Gastenboek\n1) Schrijf een nieuw bericht.\n2) Lees oude berichten."
|
||||
)
|
||||
|
||||
var (
|
||||
menu string = ""
|
||||
menuwidth int = 0
|
||||
Message string = ""
|
||||
menu string
|
||||
menuwidth int
|
||||
message string
|
||||
)
|
||||
|
||||
// Function that determines the size of the executing terminal
|
||||
// WARN: Only works if stty is available (So at least linux)
|
||||
func terminalSize() (width, height int) {
|
||||
cmd := exec.Command("stty", "size")
|
||||
cmd.Stdin = os.Stdin
|
||||
@ -32,18 +37,20 @@ func terminalSize() (width, height int) {
|
||||
return width, height
|
||||
}
|
||||
|
||||
// Clears the terminal screen
|
||||
func clear() {
|
||||
_, height := terminalSize()
|
||||
fmt.Printf("%v", strings.Repeat("\n", height+1))
|
||||
}
|
||||
|
||||
// Generates a menu string, takes up the whole screen.
|
||||
func generateMenu() string {
|
||||
var width, height int = terminalSize()
|
||||
if menu == "" && width != menuwidth {
|
||||
menuwidth = width
|
||||
if width >= asciiLen+4 {
|
||||
var content []string = strings.Split(ascii, "\n")
|
||||
var options []string = strings.Split(tekst, "\n")
|
||||
var content = strings.Split(ascii, "\n")
|
||||
var options = strings.Split(tekst, "\n")
|
||||
|
||||
content = append(content, make([]string, ((height-4)-len(content)-len(options))/2)...)
|
||||
content = append(content, options...)
|
||||
@ -51,7 +58,7 @@ func generateMenu() string {
|
||||
menu += strings.Repeat("-", width-1) + "\n"
|
||||
|
||||
for _, value := range content {
|
||||
var length int = len(value)
|
||||
var length = len(value)
|
||||
menu += "|"
|
||||
menu += strings.Repeat(" ", int(math.Floor(float64((width-length-3)/2.0))))
|
||||
menu += value
|
||||
@ -67,11 +74,12 @@ func generateMenu() string {
|
||||
return menu
|
||||
}
|
||||
|
||||
// Function that keeps the user going!
|
||||
func showMenu() {
|
||||
for {
|
||||
clear()
|
||||
fmt.Print(generateMenu())
|
||||
fmt.Println(Message)
|
||||
fmt.Println(message)
|
||||
|
||||
fmt.Print("Maak uw keuze: ")
|
||||
var i int
|
||||
@ -79,12 +87,12 @@ func showMenu() {
|
||||
|
||||
switch i {
|
||||
default:
|
||||
Message = "Was dat een keuze? Ik dacht van niet, probeer maar opnieuw!"
|
||||
message = "Was dat een keuze? Ik dacht van niet, probeer maar opnieuw!"
|
||||
case 1:
|
||||
if makeEntry() {
|
||||
Message = ""
|
||||
message = ""
|
||||
} else {
|
||||
Message = "Something went terribly wrong! Wat heb je nou weer gedaan??"
|
||||
message = "Something went terribly wrong! Wat heb je nou weer gedaan??"
|
||||
}
|
||||
case 2:
|
||||
viewEntry()
|
||||
|
@ -16,9 +16,10 @@ const (
|
||||
)
|
||||
|
||||
var (
|
||||
reader *bufio.Reader = bufio.NewReader(os.Stdin)
|
||||
reader = bufio.NewReader(os.Stdin)
|
||||
)
|
||||
|
||||
// The function that handles the actual content of the message
|
||||
func msgContent() (string, bool) {
|
||||
var msg string
|
||||
|
||||
@ -42,9 +43,10 @@ S:
|
||||
return msg, true
|
||||
}
|
||||
|
||||
// The function that handles the signing of a message
|
||||
func msgSign() (string, bool) {
|
||||
var sign string
|
||||
var end int = -1
|
||||
var end = -1
|
||||
|
||||
S:
|
||||
for {
|
||||
@ -70,6 +72,8 @@ S:
|
||||
return sign, true
|
||||
}
|
||||
|
||||
// Creates an files with filename in the current directory
|
||||
// the content of the file will of course be content.
|
||||
func writeToFile(filename, content string) bool {
|
||||
f, err := os.Create(filename)
|
||||
if err != nil {
|
||||
@ -91,6 +95,7 @@ func writeToFile(filename, content string) bool {
|
||||
return true
|
||||
}
|
||||
|
||||
// The coordinating function that calls all the necessary functions
|
||||
func makeEntry() bool {
|
||||
clear()
|
||||
|
||||
|
@ -18,6 +18,7 @@ var (
|
||||
entries []string
|
||||
)
|
||||
|
||||
// Get the information from entries and format it.
|
||||
func listFilenames() {
|
||||
for i, val := range entries {
|
||||
val = strings.Trim(val, ".txt")
|
||||
@ -29,6 +30,7 @@ func listFilenames() {
|
||||
}
|
||||
}
|
||||
|
||||
// Adds a file to entries if it's an actual file
|
||||
func getFilenames(path string, info os.FileInfo, err error) error {
|
||||
if !info.IsDir() && info.Name() == path && filepath.Ext(path) == ".txt" {
|
||||
entries = append(entries, path)
|
||||
@ -36,6 +38,7 @@ func getFilenames(path string, info os.FileInfo, err error) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// A function that works like "less"
|
||||
func showFile(i int) {
|
||||
f, openErr := os.Open(entries[i])
|
||||
if openErr != nil {
|
||||
@ -52,7 +55,7 @@ func showFile(i int) {
|
||||
fmt.Print(strings.Repeat("-", width-1) + "\n")
|
||||
|
||||
var lines int
|
||||
var done bool = false
|
||||
var done = false
|
||||
for !done {
|
||||
line, err := reader.ReadString('\n')
|
||||
done = err != nil
|
||||
@ -66,6 +69,7 @@ func showFile(i int) {
|
||||
user.ReadString('\n')
|
||||
}
|
||||
|
||||
// The coordinating function to show the different entries.
|
||||
func viewEntry() {
|
||||
entries = make([]string, 0)
|
||||
err := filepath.Walk(".", filepath.WalkFunc(getFilenames))
|
||||
|
Reference in New Issue
Block a user