fragment: add option to suppress line numbers

This commit is contained in:
Kyle Isom 2015-11-25 15:01:13 -08:00
parent 8894cbf419
commit afd2f58578
1 changed files with 15 additions and 4 deletions

View File

@ -13,14 +13,17 @@ import (
func usage() { func usage() {
progname := filepath.Base(os.Args[0]) progname := filepath.Base(os.Args[0])
fmt.Printf(`Usage: %s file start [end] fmt.Printf(`Usage: %s [-nl] file start [end]
Print a fragment of a file starting a line 'start' and ending Print a fragment of a file starting a line 'start' and ending
at line 'end', or EOF if no end is specified. at line 'end', or EOF if no end is specified.
The -nl flag will suppress printing of line numbers.
`, progname) `, progname)
} }
func main() { func main() {
quiet := flag.Bool("nl", false, "No line-numbering.")
flag.Parse() flag.Parse()
if flag.NArg() < 2 || flag.NArg() > 3 { if flag.NArg() < 2 || flag.NArg() > 3 {
@ -69,8 +72,12 @@ func main() {
start = tmp start = tmp
} }
var fmtStr string
if !*quiet {
maxLine := fmt.Sprintf("%d", len(lines)) maxLine := fmt.Sprintf("%d", len(lines))
fmtStr := fmt.Sprintf("%%0%dd: %%s", len(maxLine)) fmtStr = fmt.Sprintf("%%0%dd: %%s", len(maxLine))
}
endFunc := func(n int) bool { endFunc := func(n int) bool {
if n == 0 { if n == 0 {
@ -85,6 +92,10 @@ func main() {
fmtStr += "\n" fmtStr += "\n"
for i := start; !endFunc(i); i++ { for i := start; !endFunc(i); i++ {
if *quiet {
fmt.Println(lines[i])
} else {
fmt.Printf(fmtStr, i, lines[i]) fmt.Printf(fmtStr, i, lines[i])
} }
}
} }