-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstriper.go
83 lines (70 loc) · 1.89 KB
/
striper.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package main
import ("fmt"
"bufio"
"os"
"io"
"bytes"
"flag"
"syscall"
"unsafe"
)
/****/
type winsize struct {
Row uint16
Col uint16
Xpixel uint16
Ypixel uint16
}
func getWidth() uint {
ws := &winsize{}
retCode, _, errno := syscall.Syscall(syscall.SYS_IOCTL,
uintptr(syscall.Stdin),
uintptr(syscall.TIOCGWINSZ),
uintptr(unsafe.Pointer(ws)))
if int(retCode) == -1 {
panic(errno)
}
return uint(ws.Col)
}
/****/
/**
pull each line and process
*/
func ReadLines(reader io.Reader, foo func(string) string) string {
buf := bufio.NewReader(reader)
var buffer bytes.Buffer
line, err := buf.ReadBytes('\n');
for err == nil {
if foo!=nil {
line = []byte(foo(string(line)))
}
buffer.Write(line)
line, err = buf.ReadBytes('\n')
}
return buffer.String()
}
func main() {
//args := os.Args
lineMode := flag.Bool("line", false, "line mode, trim each line")
edgeMode := flag.Bool("edge", false, "edge mode, trim just edges")
//allMode := flag.Bool("all", false, "all mode, trim everything")
//otherMode := flag.Bool("other", false, "other mode, TBD")
flag.Parse()
if *lineMode {//each line gets trimmed, you get a trim, you get a trim
str := ReadLines(os.Stdin, sansSpace)
fmt.Printf("%s", str)
} else if *edgeMode {//only first and last
str := ReadLines(os.Stdin, pass)
fmt.Printf("%s", string(bytes.TrimSpace([]byte(str))))
} /*else if *otherMode {
str := ReadLines(os.Stdin, all)
fmt.Printf("%s", string(bytes.TrimSpace([]byte(str))))
}*/
}
func pass(line string) string{return line}
func sansSpace(line string) string{
return string(bytes.TrimSpace([]byte(line))) + "\n"
}
func all(line string) string{
return string(bytes.TrimSpace([]byte(line)))
}