Skip to content

Commit

Permalink
cache wrapWriter objects in Operation
Browse files Browse the repository at this point in the history
  • Loading branch information
slingamn committed Feb 13, 2023
1 parent 9c2be2d commit 9a3031a
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/chzyer/readline

go 1.15
go 1.18

require (
github.com/chzyer/test v1.0.0
Expand Down
10 changes: 8 additions & 2 deletions operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"errors"
"io"
"sync"
"sync/atomic"
)

var (
Expand All @@ -26,6 +27,8 @@ type Operation struct {
outchan chan []rune
errchan chan error
w io.Writer
wrapOut atomic.Pointer[wrapWriter]
wrapErr atomic.Pointer[wrapWriter]

history *opHistory
*opSearch
Expand Down Expand Up @@ -366,11 +369,11 @@ func (o *Operation) ioloop() {
}

func (o *Operation) Stderr() io.Writer {
return &wrapWriter{target: o.GetConfig().Stderr, r: o, t: o.t}
return o.wrapErr.Load()
}

func (o *Operation) Stdout() io.Writer {
return &wrapWriter{target: o.GetConfig().Stdout, r: o, t: o.t}
return o.wrapOut.Load()
}

func (o *Operation) String() (string, error) {
Expand Down Expand Up @@ -471,6 +474,9 @@ func (op *Operation) SetConfig(cfg *Config) (*Config, error) {
op.buf.SetConfig(cfg)
width := op.cfg.FuncGetWidth()

op.wrapOut.Store(&wrapWriter{target: cfg.Stdout, r: op, t: op.t})
op.wrapErr.Store(&wrapWriter{target: cfg.Stderr, r: op, t: op.t})

if cfg.opHistory == nil {
op.SetHistoryPath(cfg.HistoryFile)
cfg.opHistory = op.history
Expand Down

0 comments on commit 9a3031a

Please sign in to comment.