-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
105 lines (89 loc) · 1.57 KB
/
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package main
import (
"bufio"
"bytes"
"fmt"
"os"
"os/exec"
"strconv"
"strings"
)
func main() {
args := os.Args[1:]
length := 22
files := make([]string, 0)
if len(args) > 0 {
for k, v := range args {
if strings.HasPrefix(args[k], "-") {
var err error
length, err = strconv.Atoi(strings.TrimLeft(args[k], "-"))
if (err != nil) {
panic("Unable to interpret line count")
}
} else {
files = append(files, v)
}
}
}
tty, err := os.Open("/dev/tty")
if err != nil {
panic("Could not open /dev/tty")
}
r := bufio.NewReader(tty)
if len(files) > 0 {
for _, v := range files {
fp, err := os.Open(v)
if (err != nil) {
panic(err)
}
defer fp.Close()
s := bufio.NewScanner(fp)
process(length, s, r)
}
} else {
s := bufio.NewScanner(os.Stdin)
process(length, s, r)
}
}
func process(l int, s *bufio.Scanner, r *bufio.Reader) {
s.Split(bufio.ScanLines)
var buf strings.Builder
clear()
i := 1
for s.Scan() {
buf.WriteString(s.Text() + "\n")
i++
if (i >= l) {
output(&buf)
wait(r)
clear()
i = 1
}
}
output(&buf)
wait(r)
}
func output(buf *strings.Builder) {
fmt.Print(buf.String())
buf.Reset()
}
func wait(r *bufio.Reader) {
str, err := r.ReadString('\n')
if err != nil {
panic(err)
}
if (strings.HasPrefix(str, "q")) {
os.Exit(0);
}
if (strings.HasPrefix(str, "!")) {
cmd := exec.Command("sh", "-c", strings.TrimLeft(str, "!"))
var out bytes.Buffer
cmd.Stdout = &out
cmd.Run()
fmt.Println(out.String())
wait(r)
}
}
func clear() {
fmt.Print("\033[H\033[2J")
}