-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
62 lines (51 loc) · 1017 Bytes
/
main.go
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
package main
import (
"bufio"
"fmt"
"io"
"os"
"regexp"
"strings"
)
type Line struct {
Text string
Index []int
}
func main() {
if len(os.Args) != 2 {
fmt.Println("Usage: align-pattern PATTERN")
os.Exit(1)
}
pattern := regexp.MustCompile(os.Args[1])
reader := bufio.NewReader(os.Stdin)
lines := make([]Line, 0)
for {
text, err := reader.ReadString('\n')
text = strings.TrimSuffix(text, "\n")
if err != nil && err != io.EOF {
panic(err)
}
index := pattern.FindStringIndex(text)
lines = append(lines, Line{Text: text, Index: index})
if err == io.EOF {
break
}
}
maxIndex := 0
for _, line := range lines {
if line.Index != nil && line.Index[0] > maxIndex {
maxIndex = line.Index[0]
}
}
for _, line := range lines {
if line.Index == nil {
fmt.Println(line.Text)
} else {
index := line.Index[0]
head := line.Text[:index]
tail := line.Text[index:]
padding := strings.Repeat(" ", maxIndex-index)
fmt.Println(head + padding + tail)
}
}
}