From cc45805a871bde6a2a3319c8103ec9ebde46bb41 Mon Sep 17 00:00:00 2001 From: "Jip J. Dekker" Date: Sun, 12 Oct 2014 01:48:32 +0200 Subject: [PATCH] Add comments and optimizing gocode --- gastenboek.go | 2 ++ menu.go | 34 +++++++++++++++++++++------------- newEntry.go | 9 +++++++-- viewEntry.go | 6 +++++- 4 files changed, 35 insertions(+), 16 deletions(-) diff --git a/gastenboek.go b/gastenboek.go index 226d576..1c2b4ba 100644 --- a/gastenboek.go +++ b/gastenboek.go @@ -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() diff --git a/menu.go b/menu.go index 4e163ec..1509160 100644 --- a/menu.go +++ b/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() diff --git a/newEntry.go b/newEntry.go index 757311b..1bfd269 100644 --- a/newEntry.go +++ b/newEntry.go @@ -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() diff --git a/viewEntry.go b/viewEntry.go index f480ce6..b1a35a2 100644 --- a/viewEntry.go +++ b/viewEntry.go @@ -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))