Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

print each time #236

Merged
merged 1 commit into from
Sep 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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