From fe472377671106d712b7d808c509e66c69db4696 Mon Sep 17 00:00:00 2001 From: Djordje Lukic Date: Wed, 17 Jun 2020 17:24:50 +0200 Subject: [PATCH] Add functions to add the writer in the context --- progress/writer.go | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/progress/writer.go b/progress/writer.go index 9803f382..9f386d19 100644 --- a/progress/writer.go +++ b/progress/writer.go @@ -59,6 +59,19 @@ type writer struct { mtx *sync.RWMutex } +type writerKey struct{} + +// WithContextWriter adds the writer to the context +func WithContextWriter(ctx context.Context, writer Writer) context.Context { + return context.WithValue(ctx, writerKey{}, writer) +} + +// ContextWriter returns the writer from the context +func ContextWriter(ctx context.Context) Writer { + s, _ := ctx.Value(writerKey{}).(Writer) + return s +} + // NewWriter returns a new multi-progress writer func NewWriter(out io.Writer) Writer { return &writer{