diff options
| author | Sam Anthony <sam@samanthony.xyz> | 2024-08-26 18:25:07 -0400 |
|---|---|---|
| committer | Sam Anthony <sam@samanthony.xyz> | 2024-08-26 18:25:07 -0400 |
| commit | f707a8510f52c1b9f4e0653d8f14c16a06492f40 (patch) | |
| tree | 744032c0191b550db4197e46d39c4e7431b66ea3 /fmt.go | |
| parent | 1e9aba735b06294818188bf407c18460ba4fd4fa (diff) | |
| download | markov-f707a8510f52c1b9f4e0653d8f14c16a06492f40.zip | |
formatter
Diffstat (limited to 'fmt.go')
| -rw-r--r-- | fmt.go | 33 |
1 files changed, 33 insertions, 0 deletions
@@ -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() +} |