Skip to content

Commit

Permalink
print each time (#236)
Browse files Browse the repository at this point in the history
  • Loading branch information
ddddddO committed Sep 24, 2023
1 parent 8cd5f14 commit 9c5e3a8
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 28 deletions.
17 changes: 4 additions & 13 deletions pipeline_tree_spreader.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,23 +44,19 @@ func (ds *defaultSpreaderPipeline) spread(ctx context.Context, w io.Writer, root
close(errc)
}()

bw := bufio.NewWriter(w)
ds.w = w
wg := &sync.WaitGroup{}
for i := 0; i < workerSpreadNum; i++ {
wg.Add(1)
go ds.worker(ctx, wg, bw, roots, errc)
go ds.worker(ctx, wg, roots, errc)
}
wg.Wait()

if err := bw.Flush(); err != nil {
errc <- err
}
}()

return errc
}

func (ds *defaultSpreaderPipeline) worker(ctx context.Context, wg *sync.WaitGroup, bw *bufio.Writer, roots <-chan *Node, errc chan<- error) {
func (ds *defaultSpreaderPipeline) worker(ctx context.Context, wg *sync.WaitGroup, roots <-chan *Node, _ chan<- error) {
defer wg.Done()
for {
select {
Expand All @@ -70,15 +66,10 @@ func (ds *defaultSpreaderPipeline) worker(ctx context.Context, wg *sync.WaitGrou
if !ok {
return
}
ret := ds.spreadBranch(root)

ds.Lock()
_, err := bw.WriteString(ret)
ds.spreadBranch(root)
ds.Unlock()

if err != nil {
errc <- err
}
}
}
}
Expand Down
32 changes: 17 additions & 15 deletions simple_tree_spreader.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,34 +35,28 @@ const (
encodeTOML
)

type defaultSpreaderSimple struct{}
type defaultSpreaderSimple struct {
w io.Writer
}

func (ds *defaultSpreaderSimple) spread(w io.Writer, roots []*Node) error {
branches := ""
ds.w = w
for _, root := range roots {
branches += ds.spreadBranch(root)
ds.spreadBranch(root)
}
return ds.write(w, branches)
return nil
}

func (*defaultSpreaderSimple) spreadBranch(current *Node) string {
func (ds *defaultSpreaderSimple) spreadBranch(current *Node) {
ret := current.name + "\n"
if !current.isRoot() {
ret = current.branch() + " " + current.name + "\n"
}
fmt.Fprint(ds.w, ret)

for _, child := range current.children {
ret += (*defaultSpreaderSimple)(nil).spreadBranch(child)
ds.spreadBranch(child)
}
return ret
}

func (*defaultSpreaderSimple) write(w io.Writer, in string) error {
buf := bufio.NewWriter(w)
if _, err := buf.WriteString(in); err != nil {
return err
}
return buf.Flush()
}

type formattedSpreaderSimple[T sitter] struct {
Expand Down Expand Up @@ -219,6 +213,14 @@ func (cs *colorizeSpreaderSimple) spreadBranch(current *Node) string {
return ret
}

func (*colorizeSpreaderSimple) write(w io.Writer, in string) error {
buf := bufio.NewWriter(w)
if _, err := buf.WriteString(in); err != nil {
return err
}
return buf.Flush()
}

func (cs *colorizeSpreaderSimple) colorize(current *Node) string {
if cs.fileConsiderer.isFile(current) {
_ = cs.fileCounter.next()
Expand Down

0 comments on commit 9c5e3a8

Please sign in to comment.