瀏覽代碼

Add functions to add the writer in the context

Djordje Lukic 5 年之前
父節點
當前提交
fe47237767
共有 1 個文件被更改,包括 13 次插入0 次删除
  1. 13 0
      progress/writer.go

+ 13 - 0
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{