Een Gastenboek, ter voorkoming van brassen geheel geautomatiseerd
at main 92 lines 1.9 kB view raw
1package main 2 3import ( 4 "bufio" 5 "fmt" 6 "os" 7 "path/filepath" 8 "strconv" 9 "strings" 10 "time" 11) 12 13const ( 14 timeLayout = "3:04pm (MST)" 15) 16 17var ( 18 entries []string 19) 20 21// Get the information from entries and format it. 22func listFilenames() { 23 for i, val := range entries { 24 val = strings.Trim(val, ".txt") 25 val = strings.Trim(val, "\n") 26 parts := strings.Split(val, " ") 27 unix, _ := strconv.ParseInt(parts[0], 10, 64) 28 t := time.Unix(unix, 0) 29 fmt.Printf("%d) %v - %v \n", i+1, t.Format(timeLayout), parts[1]) 30 } 31} 32 33// Adds a file to entries if it's an actual file 34func getFilenames(path string, info os.FileInfo, err error) error { 35 if !info.IsDir() && info.Name() == path && filepath.Ext(path) == ".txt" { 36 entries = append(entries, path) 37 } 38 return nil 39} 40 41// A function that works like "less" 42func showFile(i int) { 43 f, openErr := os.Open(entries[i]) 44 if openErr != nil { 45 fmt.Printf("\n\tCouldn't open file: %v", openErr) 46 return 47 } 48 defer f.Close() 49 reader := bufio.NewReader(f) 50 user := bufio.NewReader(os.Stdin) 51 52 clear() 53 fmt.Print("Druk op enter om volgende regels te zien en uiteindelijk terug te keren naar het keuzemenu.\n") 54 var width, height int = terminalSize() 55 fmt.Print(strings.Repeat("-", width-1) + "\n") 56 57 var lines int 58 var done = false 59 for !done { 60 line, err := reader.ReadString('\n') 61 done = err != nil 62 fmt.Print(line) 63 if lines >= height { 64 user.ReadString('\n') 65 } 66 lines++ 67 } 68 fmt.Print(strings.Repeat("-", width-1) + "\n") 69 user.ReadString('\n') 70} 71 72// The coordinating function to show the different entries. 73func viewEntry() { 74 entries = make([]string, 0) 75 err := filepath.Walk(".", filepath.WalkFunc(getFilenames)) 76 if err != nil { 77 return 78 } 79 80 for { 81 clear() 82 listFilenames() 83 fmt.Printf("\nKies hier welk bericht u graag zou willen lezen, toets 0 om terug te keren naar het menu: ") 84 var i int 85 fmt.Scan(&i) 86 if i > 0 && i <= len(entries) { 87 showFile(i - 1) 88 } else { 89 return 90 } 91 } 92}