1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2024-12-12 11:15:00 +02:00
lazygit/pkg/utils/once_writer.go

32 lines
509 B
Go
Raw Normal View History

package utils
import (
"io"
"sync"
)
// This wraps a writer and ensures that before we actually write anything we call a given function first
type OnceWriter struct {
writer io.Writer
once sync.Once
f func()
}
var _ io.Writer = &OnceWriter{}
func NewOnceWriter(writer io.Writer, f func()) *OnceWriter {
return &OnceWriter{
writer: writer,
f: f,
}
}
func (self *OnceWriter) Write(p []byte) (n int, err error) {
self.once.Do(func() {
self.f()
})
return self.writer.Write(p)
}