Skip to content

Commit a7e3e76

Browse files
committed
Add start/end line
1 parent c4fb54c commit a7e3e76

2 files changed

Lines changed: 22 additions & 0 deletions

File tree

cmd/catp/catp/app.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,11 @@ func Main(options ...func(o *Options)) error { //nolint:funlen,cyclop,gocognit,g
7979
"files will be written to out dir with original base names\n"+
8080
"disables output flag")
8181

82+
flag.IntVar(&r.startLine, "start-line", 0, "start printing lines from this line (inclusive),\n"+
83+
"default is 0 (first line), each input file is counted separately")
84+
flag.IntVar(&r.endLine, "end-line", 0, "stop printing lines at this line (exclusive),\n"+
85+
"default is 0 (no limit), each input file is counted separately")
86+
8287
flag.Usage = func() {
8388
fmt.Println("catp", version.Module("github.com/bool64/progress").Version+",",
8489
version.Info().GoVersion, strings.Join(versionExtra, " "))

cmd/catp/catp/catp.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@ type runner struct {
5959
noProgress bool
6060
countLines bool
6161

62+
startLine int
63+
endLine int
64+
6265
hasOptions bool
6366
options Options
6467

@@ -200,6 +203,7 @@ func (r *runner) scanFile(filename string, rd io.Reader, out io.Writer) {
200203
s := bufio.NewScanner(rd)
201204
s.Buffer(make([]byte, 64*1024), 10*1024*1024)
202205

206+
fileLines := 0
203207
lines := 0
204208
buf := make([]byte, 64*1024)
205209

@@ -213,8 +217,17 @@ func (r *runner) scanFile(filename string, rd io.Reader, out io.Writer) {
213217
})
214218

215219
for s.Scan() {
220+
fileLines++
216221
lines++
217222

223+
if r.startLine > 0 && fileLines <= r.startLine {
224+
continue
225+
}
226+
227+
if r.endLine > 0 && fileLines > r.endLine {
228+
break
229+
}
230+
218231
if atomic.LoadInt64(&r.closed) > 0 {
219232
break
220233
}
@@ -399,6 +412,10 @@ func (r *runner) cat(filename string) (err error) { //nolint:gocyclo
399412
r.limiter = rate.NewLimiter(rate.Limit(r.rateLimit), 100)
400413
}
401414

415+
if r.startLine != 0 || r.endLine != 0 {
416+
r.countLines = true
417+
}
418+
402419
if r.filters.isSet() || r.parallel > 1 || r.hasOptions || r.countLines || r.rateLimit > 0 {
403420
r.scanFile(filename, rd, out)
404421
} else {

0 commit comments

Comments
 (0)