forked from TrenchBoot/u-root
-
Notifications
You must be signed in to change notification settings - Fork 0
/
more.go
62 lines (55 loc) · 1.38 KB
/
more.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
// Copyright 2018 the u-root Authors. All rights reserved
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// More pages through files without any terminal trickery.
//
// Synopsis:
// more [OPTIONS] FILE
//
// Description:
// Admittedly, this does not follow the conventions of GNU more. Instead,
// it is built with the goal of not relying on any special ttys, ioctls or
// special ANSI escapes. This is ideal when your terminal is already
// borked. For bells and whistles, look at less.
//
// Options:
// --lines NUMBER: screen size in number of lines
package main
import (
"bufio"
"fmt"
"log"
"os"
flag "github.com/spf13/pflag"
)
var lines = flag.Int("lines", 40, "screen size in number of lines")
func main() {
flag.Parse()
if flag.NArg() != 1 {
log.Fatal("more can only take one file")
}
if *lines <= 0 {
log.Fatal("lines must be positive")
}
f, err := os.Open(flag.Args()[0])
if err != nil {
log.Fatal(err)
}
defer f.Close()
scanner := bufio.NewScanner(f)
for i := 0; scanner.Scan(); i++ {
if (i+1)%*lines == 0 {
fmt.Print(scanner.Text())
c := make([]byte, 1)
// We expect the OS to echo the newline character.
if _, err := os.Stdin.Read(c); err != nil {
return
}
} else {
fmt.Println(scanner.Text())
}
}
if err := scanner.Err(); err != nil {
log.Fatal(err)
}
}