Skip to content

Commit c8ac872

Browse files
authored
Merge pull request moby#4220 from jsternberg/io-writer-display
progressui: modify NewDisplay to accept io.Writer instead of console.File
2 parents 01aa7cb + f53bc88 commit c8ac872

File tree

1 file changed

+15
-4
lines changed

1 file changed

+15
-4
lines changed

util/progress/progressui/display.go

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -137,14 +137,16 @@ const (
137137
RawJSONMode DisplayMode = "rawjson"
138138
)
139139

140-
// NewDisplay constructs a Display that outputs to the given console.File with the given DisplayMode.
140+
// NewDisplay constructs a Display that outputs to the given io.Writer with the given DisplayMode.
141141
//
142-
// This method will return an error when the DisplayMode is invalid or if TtyMode is used but the console.File
142+
// This method will return an error when the DisplayMode is invalid or if TtyMode is used but the io.Writer
143143
// does not refer to a tty. AutoMode will choose TtyMode or PlainMode depending on if the output is a tty or not.
144-
func NewDisplay(out console.File, mode DisplayMode, opts ...DisplayOpt) (Display, error) {
144+
//
145+
// For TtyMode to work, the io.Writer should also implement console.File.
146+
func NewDisplay(out io.Writer, mode DisplayMode, opts ...DisplayOpt) (Display, error) {
145147
switch mode {
146148
case AutoMode, TtyMode, DefaultMode:
147-
if c, err := console.ConsoleFromFile(out); err == nil {
149+
if c, err := consoleFromWriter(out); err == nil {
148150
return newConsoleDisplay(c, opts...), nil
149151
} else if mode == "tty" {
150152
return Display{}, errors.Wrap(err, "failed to get console")
@@ -161,6 +163,15 @@ func NewDisplay(out console.File, mode DisplayMode, opts ...DisplayOpt) (Display
161163
}
162164
}
163165

166+
// consoleFromWriter retrieves a console.Console from an io.Writer.
167+
func consoleFromWriter(out io.Writer) (console.Console, error) {
168+
f, ok := out.(console.File)
169+
if !ok {
170+
return nil, errors.New("output is not a file")
171+
}
172+
return console.ConsoleFromFile(f)
173+
}
174+
164175
type discardDisplay struct{}
165176

166177
func newDiscardDisplay() Display {

0 commit comments

Comments
 (0)