summaryrefslogtreecommitdiffstats
path: root/fmt.go
blob: e54a1d1905db5d9804f43c0341e3e319fb824a2a (plain) (blame)
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
// 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()
}