~dricottone/digestion

ref: de6aac89edaeb2ceda7412983ac035e0f9912a25 digestion/textwrap/main.go -rw-r--r-- 1.5 KiB
de6aac89Dominic Ricottone Initial commit 4 years ago
                                                                                
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
package main

import (
	"fmt"
	"os"
	"strings"
	"bufio"
	"regexp"
	"flag"
)

const LENGTH = 40

func print_break(length int) {
	fmt.Printf("%s\n", strings.Repeat("-", length))
}

func print_wrapped(line string, length int, quote string) {
	len_quote := len(quote)
	buffer := quote
	for index, rune := range line[len_quote:] {
		buffer += string(rune)
		if (index + 1) % (length - len_quote) == 0 {
			fmt.Printf("%s\n", buffer)
			buffer = quote
		}
	}
	if buffer != "" {
		fmt.Printf("%s\n", buffer)
	}
}

func main() {
	// Open STDIN as scanner
	_, err := os.Stdin.Stat()
	if err != nil {
		fmt.Printf("%s\n", "cannot read input")
		os.Exit(1)
	}
	input := bufio.NewScanner(os.Stdin)

	// Look for arguments
	var width = flag.Int("width", 80, "target width for output")
	flag.Parse()

	// Compile regular expressions
	re_quote, err := regexp.Compile("^([> ]*)")
	if err != nil {
		fmt.Printf("internal error - %v\n", err)
		os.Exit(1)
	}
	re_break, err := regexp.Compile("^(?:-{5,}|={5,})$")
	if err != nil {
		fmt.Printf("internal error - %v\n", err)
		os.Exit(1)
	}

	// Scan line by line
	for input.Scan() {
		line := input.Text()
		line = strings.TrimSpace(line)

		if len(line) > *width {
			if re_break.MatchString(line) {
				print_break(*width)
			} else {
				quote := re_quote.FindString(line)
				print_wrapped(line, *width, quote)
			}
		} else {
			fmt.Printf("%s\n", line)
		}
	}

	// Check for scanner errors
	if err = input.Err(); err != nil {
		fmt.Printf("internal error - %v\n", err)
		os.Exit(1)
	}
}