gitlab.com/apertussolutions/u-root@v7.0.0+incompatible/cmds/core/more/more.go (about)

     1  // Copyright 2018 the u-root Authors. All rights reserved
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  // More pages through files without any terminal trickery.
     6  //
     7  // Synopsis:
     8  //     more [OPTIONS] FILE
     9  //
    10  // Description:
    11  //     Admittedly, this does not follow the conventions of GNU more. Instead,
    12  //     it is built with the goal of not relying on any special ttys, ioctls or
    13  //     special ANSI escapes. This is ideal when your terminal is already
    14  //     borked. For bells and whistles, look at less.
    15  //
    16  // Options:
    17  //     --lines NUMBER: screen size in number of lines
    18  package main
    19  
    20  import (
    21  	"bufio"
    22  	"fmt"
    23  	"log"
    24  	"os"
    25  
    26  	flag "github.com/spf13/pflag"
    27  )
    28  
    29  var lines = flag.Int("lines", 40, "screen size in number of lines")
    30  
    31  func main() {
    32  	flag.Parse()
    33  	if flag.NArg() != 1 {
    34  		log.Fatal("more can only take one file")
    35  	}
    36  	if *lines <= 0 {
    37  		log.Fatal("lines must be positive")
    38  	}
    39  
    40  	f, err := os.Open(flag.Args()[0])
    41  	if err != nil {
    42  		log.Fatal(err)
    43  	}
    44  	defer f.Close()
    45  
    46  	scanner := bufio.NewScanner(f)
    47  	for i := 0; scanner.Scan(); i++ {
    48  		if (i+1)%*lines == 0 {
    49  			fmt.Print(scanner.Text())
    50  			c := make([]byte, 1)
    51  			// We expect the OS to echo the newline character.
    52  			if _, err := os.Stdin.Read(c); err != nil {
    53  				return
    54  			}
    55  		} else {
    56  			fmt.Println(scanner.Text())
    57  		}
    58  	}
    59  	if err := scanner.Err(); err != nil {
    60  		log.Fatal(err)
    61  	}
    62  }