-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
follow mode now allows to read entire file
- Loading branch information
Peter
committed
Apr 3, 2024
1 parent
f558c6b
commit abe8593
Showing
9 changed files
with
226 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package utils | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"io" | ||
"os" | ||
|
||
"github.com/cheggaaa/pb/v3" | ||
) | ||
|
||
type Line struct { | ||
seq int | ||
Line []byte | ||
} | ||
|
||
func LineCounterWithChannel(r io.Reader, fn func(line Line, cancel func())) error { | ||
ctx, cancel := context.WithCancel(context.Background()) | ||
const bufferSize = 64 * 1024 | ||
buf := make([]byte, bufferSize) | ||
var previousLine = []byte{} | ||
seq := 0 | ||
|
||
for { | ||
|
||
if ctx.Err() != nil { | ||
cancel() | ||
return nil | ||
} | ||
|
||
i, err := r.Read(buf) | ||
if err != nil && err != io.EOF { | ||
panic(err) | ||
} | ||
|
||
if i == 0 && len(previousLine) == 0 { | ||
cancel() | ||
return nil | ||
} | ||
|
||
previousLine = append(previousLine, buf[:i]...) | ||
newlineIndex := bytes.IndexByte(previousLine, '\n') | ||
|
||
if newlineIndex == -1 { | ||
|
||
if i == bufferSize { | ||
continue | ||
} | ||
|
||
if err == io.EOF || (i == 0 || i < bufferSize) { | ||
fn(Line{seq: seq, Line: previousLine}, cancel) | ||
return nil | ||
} | ||
} | ||
|
||
lines := bytes.Split(previousLine, []byte{'\n'}) | ||
previousLine = lines[len(lines)-1] | ||
lines = lines[:len(lines)-1] | ||
|
||
for _, line := range lines { | ||
seq++ | ||
fn(Line{seq: seq, Line: line}, cancel) | ||
} | ||
|
||
if err == io.EOF { | ||
fn(Line{seq: seq, Line: previousLine}, cancel) | ||
return nil | ||
} | ||
} | ||
} | ||
|
||
func OpenFileForReading(file string) (io.Reader, int64, *pb.ProgressBar) { | ||
reader, err := os.Open(file) | ||
|
||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
fi, _ := reader.Stat() | ||
bar := pb.Full.Start64(fi.Size()) | ||
return bar.NewProxyReader(reader), fi.Size(), bar | ||
// return reader, fi.Size() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package utils | ||
|
||
import ( | ||
"bytes" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestLineCounterWithChannel(t *testing.T) { | ||
|
||
tests := []struct { | ||
input string | ||
buffer int | ||
linesCount int | ||
}{ | ||
{input: "123", buffer: 5, linesCount: 1}, | ||
{input: "123456", buffer: 5, linesCount: 1}, | ||
{input: "123456790abc", buffer: 5, linesCount: 1}, | ||
|
||
{input: "12\n3", buffer: 5, linesCount: 2}, | ||
{input: "12\n3456789", buffer: 5, linesCount: 2}, | ||
{input: "12\n3456\n789", buffer: 5, linesCount: 3}, | ||
{input: "12\n34aaabbb56\n789", buffer: 5, linesCount: 3}, | ||
{input: "12\n34aaabbbcccddd56\n789", buffer: 5, linesCount: 3}, | ||
|
||
{input: "1\n\n2", buffer: 5, linesCount: 3}, | ||
{input: "1\n\n\n\n2", buffer: 5, linesCount: 5}, | ||
} | ||
|
||
for _, tc := range tests { | ||
|
||
chars := "" | ||
c := 0 | ||
LineCounterWithChannel(bytes.NewBufferString(tc.input), func(line Line, cancel func()) { | ||
c++ | ||
chars = chars + string(line.Line) | ||
}) | ||
|
||
assert.Equal(t, tc.linesCount, c) | ||
assert.Equal(t, strings.ReplaceAll(tc.input, "\n", ""), chars) | ||
|
||
} | ||
|
||
} |