File: fragment.go

package info (click to toggle)
golang-github-kisom-goutils 0.0~git20161101.0.858c9cb-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 396 kB
  • sloc: makefile: 6
file content (101 lines) | stat: -rw-r--r-- 1,652 bytes parent folder | download | duplicates (4)
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
package main

import (
	"bufio"
	"flag"
	"fmt"
	"os"
	"path/filepath"
	"strconv"

	"github.com/kisom/goutils/die"
)

func usage() {
	progname := filepath.Base(os.Args[0])
	fmt.Printf(`Usage: %s [-nl] file start [end]

	Print a fragment of a file starting a line 'start' and ending
	at line 'end', or EOF if no end is specified.

	The -nl flag will suppress printing of line numbers.
`, progname)
}

func main() {
	quiet := flag.Bool("nl", false, "No line-numbering.")
	flag.Parse()

	if flag.NArg() < 2 || flag.NArg() > 3 {
		usage()
		os.Exit(1)
	}

	start, err := strconv.Atoi(flag.Arg(1))
	die.If(err)

	var end int
	var offset bool
	if flag.NArg() == 3 {
		endStr := flag.Arg(2)
		if endStr[0] == '+' {
			offset = true
			endStr = endStr[1:]
		}
		end, err = strconv.Atoi(endStr)
		die.If(err)
		if offset {
			end += start
		}
	}

	file, err := os.Open(flag.Arg(0))
	die.If(err)
	defer file.Close()

	scanner := bufio.NewScanner(file)

	// initial empty line to start numbering at 1.
	var lines = make([]string, 1)
	for scanner.Scan() {
		lines = append(lines, scanner.Text())
	}

	if end == 0 {
		end = len(lines) - 1
	}

	if end < start {
		fmt.Fprintln(os.Stderr, "[!] end < start, swapping values")
		tmp := end
		end = start
		start = tmp
	}

	var fmtStr string

	if !*quiet {
		maxLine := fmt.Sprintf("%d", len(lines))
		fmtStr = fmt.Sprintf("%%0%dd: %%s", len(maxLine))
	}

	endFunc := func(n int) bool {
		if n == 0 {
			return false
		}

		if n > end {
			return true
		}
		return false
	}

	fmtStr += "\n"
	for i := start; !endFunc(i); i++ {
		if *quiet {
			fmt.Println(lines[i])
		} else {
			fmt.Printf(fmtStr, i, lines[i])
		}
	}
}