Should be better at documenting things.
This commit is contained in:
40
cmd/fragment/README
Normal file
40
cmd/fragment/README
Normal file
@@ -0,0 +1,40 @@
|
||||
fragment
|
||||
|
||||
This is a program to print a fragment of a file. It takes a filename,
|
||||
a starting line (lines are numbered starting at 1) and an optional
|
||||
end line or offset.
|
||||
|
||||
For example, compare the output to `nl -ba fragment.go`:
|
||||
|
||||
$ fragment fragment.go 23 +6
|
||||
23: func main() {
|
||||
24: flag.Parse()
|
||||
25:
|
||||
26: if flag.NArg() < 2 || flag.NArg() > 3 {
|
||||
27: usage()
|
||||
28: os.Exit(1)
|
||||
29: }
|
||||
|
||||
$ fragment fragment.go 25 30
|
||||
25:
|
||||
26: if flag.NArg() < 2 || flag.NArg() > 3 {
|
||||
27: usage()
|
||||
28: os.Exit(1)
|
||||
29: }
|
||||
30:
|
||||
|
||||
$ fragment fragment.go 80
|
||||
80
|
||||
80: if n > end {
|
||||
81: return true
|
||||
82: }
|
||||
83: return false
|
||||
84: }
|
||||
85:
|
||||
86: fmt.Println(start)
|
||||
87: fmtStr += "\n"
|
||||
88: for i := start; !endFunc(i); i++ {
|
||||
89: fmt.Printf(fmtStr, i, lines[i])
|
||||
90: }
|
||||
91: }
|
||||
|
||||
@@ -46,13 +46,6 @@ func main() {
|
||||
}
|
||||
}
|
||||
|
||||
if end < start {
|
||||
fmt.Println("[!] end < start, swapping values")
|
||||
tmp := end
|
||||
end = start
|
||||
start = tmp
|
||||
}
|
||||
|
||||
file, err := os.Open(flag.Arg(0))
|
||||
die.If(err)
|
||||
defer file.Close()
|
||||
@@ -65,6 +58,17 @@ func main() {
|
||||
lines = append(lines, scanner.Text())
|
||||
}
|
||||
|
||||
if end == 0 {
|
||||
end = len(lines) - 1
|
||||
}
|
||||
|
||||
if end < start {
|
||||
fmt.Println("[!] end < start, swapping values")
|
||||
tmp := end
|
||||
end = start
|
||||
start = tmp
|
||||
}
|
||||
|
||||
maxLine := fmt.Sprintf("%d", len(lines))
|
||||
fmtStr := fmt.Sprintf("%%0%dd: %%s", len(maxLine))
|
||||
|
||||
@@ -72,12 +76,14 @@ func main() {
|
||||
if n == 0 {
|
||||
return false
|
||||
}
|
||||
|
||||
if n > end {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
fmt.Println(start)
|
||||
fmtStr += "\n"
|
||||
for i := start; !endFunc(i); i++ {
|
||||
fmt.Printf(fmtStr, i, lines[i])
|
||||
|
||||
Reference in New Issue
Block a user