This repository has been archived by the owner on Nov 7, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
96 lines (79 loc) · 2.13 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
package main
import (
"fmt"
"github.com/pepperkit/tabasco/cmd"
"github.com/pepperkit/tabasco/txt"
"github.com/pepperkit/tabasco/writer"
"log"
"os"
)
const byteFactor = 1024
const kiloByteParagraphSize = 8
const megaByteParagraphSize = 25
func main() {
args := cmd.Parse()
cmd.Info(args)
cmd.ValidateFileSize(args)
documentWriter := newDocumentWriter(args)
generateTextBySize(args, documentWriter)
makeReport(documentWriter.FileName())
}
func newDocumentWriter(args *cmd.TabascoArgs) writer.DocumentWriter {
if args.Docx {
return writer.NewDocxWriter(args.FileName)
}
return writer.NewTxtWriter(args.FileName)
}
func makeReport(fileName string) {
file, err := os.Open(fileName)
checkError(err)
fi, _ := file.Stat()
fmt.Printf("Complete! The file %s has been generated.\n", fi.Name())
fileSizeBytes := fi.Size()
fmt.Printf("File size is %d bytes\n", fileSizeBytes)
if fileSizeBytes > byteFactor {
fileSizeKiB := float64(fileSizeBytes / byteFactor)
fmt.Printf("File size is %.2f KiB\n", fileSizeKiB)
}
if fileSizeBytes > (byteFactor * byteFactor) {
fileSizeMiB := float64((fileSizeBytes / byteFactor) / byteFactor)
fmt.Printf("File size is %.2f MiB\n", fileSizeMiB)
}
}
func generateTextBySize(args *cmd.TabascoArgs, wr writer.DocumentWriter) {
expectedSize := args.FileSize
paragraphSize := 1 // default size
if args.UnitKiB {
expectedSize = expectedSize * byteFactor
paragraphSize = kiloByteParagraphSize
}
if args.UnitMiB {
expectedSize = expectedSize * byteFactor * byteFactor
paragraphSize = megaByteParagraphSize
}
totalSize := 0
fmt.Println("Generating...")
for totalSize < expectedSize {
res := txt.GenerateText(paragraphSize, args.Language)
potentialSize := totalSize + len(res)
if potentialSize > expectedSize {
needBytes := expectedSize - totalSize
if totalSize == 0 {
needBytes = expectedSize
}
potentialContent := []byte(res)
str := string(potentialContent[0:needBytes])
wr.WriteText(str)
totalSize += len([]byte(res))
} else {
wr.WriteText(res)
totalSize += len([]byte(res))
}
}
wr.Flush()
}
func checkError(err error) {
if err != nil {
log.Fatalln(err)
}
}