Skip to content

Commit

Permalink
fix: use reader.ReadString() to read buffer
Browse files Browse the repository at this point in the history
Instead of creating a scanner and dealing with those scanner sh*t, let's use reader.ReadString().

- The bufio package has a limit called MaxScanTokenSize = 64 * 1024, and isn't enough in most situations (upstream annie has a huge output to stdout).
- bufio.reader.ReadString() is a more advanced func and just won't have that problem.
- For further information, see https://www.wuyifei.cc/sanner-token-too-long/

Closes #2

Signed-off-by: 135e2 <[email protected]>
  • Loading branch information
135e2 committed Dec 26, 2021
1 parent 03feb91 commit b3aac52
Showing 1 changed file with 10 additions and 22 deletions.
32 changes: 10 additions & 22 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ var (

type outputBuffer struct {
reader *bufio.Reader
scanner *bufio.Scanner
textview *gtk.TextView
}

Expand Down Expand Up @@ -90,7 +89,6 @@ func main() {
// TODO: Download progress
output := &outputBuffer{
reader: nil,
scanner: nil,
textview: textview,
}
savedStdout := os.Stdout
Expand Down Expand Up @@ -132,12 +130,17 @@ func main() {
progbar.SetText(fmt.Sprintf("Downloaded %.2f MiB/%.2f MiB", float64(savedSize)/(1024*1024), float64(Size)/(1024*1024)))
time.Sleep(500 * time.Millisecond)
}
_, err = output.readLineAndUpdate()
if err != nil {
line, err := output.reader.ReadString('\n')
if err == nil || err == io.EOF {
if line != "" {
// AddText(textview, line)
}
if err == io.EOF {
break
}
AddText(textview, "I/O error encountered: "+err.Error())
} else {
AddText(textview, err.Error())
break
}
// fmt.Fprint(savedStdout, line)
}
Expand Down Expand Up @@ -181,26 +184,11 @@ func main() {
// Modified from fanaticscripter/annie-mingui

func (b *outputBuffer) attachReader(r io.Reader, textview *gtk.TextView) {
b.reader = bufio.NewReaderSize(r, bufio.MaxScanTokenSize)
b.scanner = bufio.NewScanner(b.reader)
// b.reader = bufio.NewReaderSize(r, bufio.MaxScanTokenSize)
b.reader = bufio.NewReader(r)
b.textview = textview
}

func (b *outputBuffer) readLineAndUpdate() (fullLine string, err error) {
if !b.scanner.Scan() {
err = b.scanner.Err()
if err != nil {
return "", err
}
err = io.EOF
}
fullLine = b.scanner.Text()
if len(fullLine) > 0 {
//AddText(b.textview, fullLine)
}
return
}

func isWindow(obj glib.IObject) (*gtk.Window, error) {
// Make type assertion (as per gtk.go).
if win, ok := obj.(*gtk.Window); ok {
Expand Down

0 comments on commit b3aac52

Please sign in to comment.