summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSam Anthony <sam@samanthony.xyz>2024-08-26 18:25:07 -0400
committerSam Anthony <sam@samanthony.xyz>2024-08-26 18:25:07 -0400
commitf707a8510f52c1b9f4e0653d8f14c16a06492f40 (patch)
tree744032c0191b550db4197e46d39c4e7431b66ea3
parent1e9aba735b06294818188bf407c18460ba4fd4fa (diff)
downloadmarkov-f707a8510f52c1b9f4e0653d8f14c16a06492f40.zip
formatter
-rw-r--r--.gitignore1
-rw-r--r--fmt.go33
2 files changed, 34 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore
index c94b1c4..ba9e3e6 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1 +1,2 @@
markov
+fmt
diff --git a/fmt.go b/fmt.go
new file mode 100644
index 0000000..e54a1d1
--- /dev/null
+++ b/fmt.go
@@ -0,0 +1,33 @@
+// This program wraps its input into short lines.
+package main
+
+import (
+ "bufio"
+ "fmt"
+ "os"
+)
+
+const maxWidth = 80 // maximum number of characters per line
+
+func main() {
+ input := bufio.NewScanner(os.Stdin)
+ input.Split(bufio.ScanWords)
+ width := 0
+ for input.Scan() {
+ word := input.Text()
+
+ if width+1+len(word) > maxWidth {
+ fmt.Println()
+ width = 0
+ }
+
+ if width == 0 {
+ fmt.Print(word)
+ width += len(word)
+ } else {
+ fmt.Printf(" %s", word)
+ width += 1 + len(word)
+ }
+ }
+ fmt.Println()
+}