summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-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()
+}