-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
102 lines (88 loc) · 2.64 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
package main
import (
"flag"
"fmt"
"log"
"os"
"dev.ansgrb.xslicer/slicer"
)
func main() {
// Define command-line flags
textPtr := flag.String("text", "", "The text to split into tweets")
filePtr := flag.String("file", "", "Path to a text file containing the input")
outputPtr := flag.String("output", "", "Path to save the output tweets (e.g., output.txt)")
appendPtr := flag.Bool("append", false, "Append tweets to the output file instead of overwriting it")
verbosePtr := flag.Bool("verbose", false, "Print additional information")
maxLengthPtr := flag.Int("maxlength", 280, "Maximum length of each tweet")
flag.Parse()
// Validate input
if *textPtr == "" && *filePtr == "" {
fmt.Println("Please provide text using the -text flag or a file using the -file flag.")
return
}
var inputText string
// Prioritize file input over direct text input
if *filePtr != "" {
// Read text from file
content, err := os.ReadFile(*filePtr)
if err != nil {
log.Fatalf("Error reading file: %v", err)
}
inputText = string(content)
} else {
inputText = *textPtr
}
// Split the text into tweets
tweets := slicer.SplitTextIntoTweets(inputText, *maxLengthPtr)
// Add thread numbering
tweets = slicer.AddThreadNumbers(tweets)
// Print the tweets to the console
for _, tweet := range tweets {
fmt.Println(tweet)
}
// Save tweets to output file if -output flag is provided
if *outputPtr != "" {
err := saveTweetsToFile(tweets, *outputPtr, *appendPtr)
if err != nil {
log.Fatalf("Error saving tweets to file: %v", err)
}
fmt.Printf("Tweets saved to %s\n", *outputPtr)
}
// Print verbose information if -verbose flag is enabled
if *verbosePtr {
fmt.Printf("Generated %d tweets.\n", len(tweets))
fmt.Printf("Max tweet length: %d characters.\n", *maxLengthPtr)
if *outputPtr != "" {
if *appendPtr {
fmt.Println("Tweets were appended to the output file.")
} else {
fmt.Println("Tweets were written to the output file (overwritten if it existed).")
}
}
}
}
// saveTweetsToFile writes the tweets to a file.
func saveTweetsToFile(tweets []string, filePath string, append bool) error {
// Open the file in the appropriate mode
var file *os.File
var err error
if append {
// Append to the file if it exists, or create it if it doesn't
file, err = os.OpenFile(filePath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
} else {
// Create or truncate the file
file, err = os.Create(filePath)
}
if err != nil {
return err
}
defer file.Close()
// Write each tweet to the file with a separator
for _, tweet := range tweets {
_, err := file.WriteString(tweet + "\n---\n")
if err != nil {
return err
}
}
return nil
}