Skip to content

Commit

Permalink
修正小bug
Browse files Browse the repository at this point in the history
  • Loading branch information
rocket049 committed Apr 12, 2020
1 parent 524468b commit 93e4e37
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 3 deletions.
5 changes: 3 additions & 2 deletions conf.d/make.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"title":"Make",
"title":"make",
"command":"make",
"work_dir":""
"work_dir":"",
"help":"在运行目录中运行make命令"
}
3 changes: 2 additions & 1 deletion mainwindow.go
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,8 @@ func (a *MyApp) controlDialog(pIn io.ReadCloser, pOut io.WriteCloser, pErr io.Re
pOut.Write([]byte(input.Text() + "\n"))
})

mrd := io.MultiReader(pIn, pErr)
//mrd := io.MultiReader(pIn, pErr)
mrd := NewRandMultiReader(pIn, pErr)
brd := bufio.NewReader(mrd)
for {
line, _, err := brd.ReadLine()
Expand Down
71 changes: 71 additions & 0 deletions mymultireader.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package main

import (
"io"
"sync/atomic"
)

type RandMultiReader struct {
channel chan []byte
buf []byte
num int32
}

func (s *RandMultiReader) Read(p []byte) (n int, err error) {
lbuf := len(s.buf)
lp := len(p)
if lbuf > 0 {
if lbuf <= lp {
n = copy(p, s.buf)
s.buf = nil
} else {
n = copy(p, s.buf[:lp])
s.buf = s.buf[lp:]
}
} else {
var ok bool
s.buf, ok = <-s.channel
//log.Println("Read ", ok)
if !ok {
return 0, io.EOF
} else {
lbuf = len(s.buf)
if lbuf <= lp {
n = copy(p, s.buf)
s.buf = nil
} else {
n = copy(p, s.buf[:lp])
s.buf = s.buf[lp:]
}
}
}
//log.Printf("Read:%d\n", n)
return
}

func (s *RandMultiReader) LinkReader(r io.ReadCloser) error {
defer r.Close()
for {
p := make([]byte, 512)
n, err := r.Read(p)
if err != nil {
atomic.AddInt32(&s.num, -1)
if atomic.LoadInt32(&s.num) == 0 {
close(s.channel)
//log.Println("close channel")
}
return err
}
s.channel <- p[:n]
}

return nil
}

func NewRandMultiReader(readers ...io.ReadCloser) io.Reader {
res := &RandMultiReader{channel: make(chan []byte, 10), buf: nil, num: int32(len(readers))}
for _, v := range readers {
go res.LinkReader(v)
}
return res
}

0 comments on commit 93e4e37

Please sign in to comment.