From 57e8e8ef29ef01cf591bc08dd53f3827ef1e66a4 Mon Sep 17 00:00:00 2001 From: Nicolas De Loof Date: Tue, 8 Mar 2022 12:15:59 +0100 Subject: [PATCH] use plain text progress when ansi=never is set Signed-off-by: Nicolas De Loof --- cmd/compose/compose.go | 11 ++++++++-- pkg/progress/writer.go | 49 ++++++++++++++++++++++++++++-------------- 2 files changed, 42 insertions(+), 18 deletions(-) diff --git a/cmd/compose/compose.go b/cmd/compose/compose.go index 0ee5bd0cd..07800a3da 100644 --- a/cmd/compose/compose.go +++ b/cmd/compose/compose.go @@ -29,16 +29,17 @@ import ( "github.com/compose-spec/compose-go/types" dockercli "github.com/docker/cli/cli" "github.com/docker/cli/cli-plugins/manager" - "github.com/docker/compose/v2/cmd/formatter" - "github.com/docker/compose/v2/pkg/utils" "github.com/morikuni/aec" "github.com/pkg/errors" "github.com/sirupsen/logrus" "github.com/spf13/cobra" "github.com/spf13/pflag" + "github.com/docker/compose/v2/cmd/formatter" "github.com/docker/compose/v2/pkg/api" "github.com/docker/compose/v2/pkg/compose" + "github.com/docker/compose/v2/pkg/progress" + "github.com/docker/compose/v2/pkg/utils" ) // Command defines a compose CLI command as a func with args @@ -271,6 +272,12 @@ func RootCommand(backend api.Service) *cobra.Command { logrus.SetLevel(logrus.TraceLevel) } formatter.SetANSIMode(ansi) + switch ansi { + case "never": + progress.Mode = progress.ModePlain + case "tty": + progress.Mode = progress.ModeTTY + } if opts.WorkDir != "" { if opts.ProjectDir != "" { return errors.New(`cannot specify DEPRECATED "--workdir" and "--project-directory". Please use only "--project-directory" instead`) diff --git a/pkg/progress/writer.go b/pkg/progress/writer.go index 352ecb030..2914364e4 100644 --- a/pkg/progress/writer.go +++ b/pkg/progress/writer.go @@ -90,28 +90,45 @@ func RunWithStatus(ctx context.Context, pf progressFuncWithStatus) (string, erro return result, err } +const ( + // ModeAuto detect console capabilities + ModeAuto = "auto" + // ModeTTY use terminal capability for advanced rendering + ModeTTY = "tty" + // ModePlain dump raw events to output + ModePlain = "plain" +) + +// Mode define how progress should be rendered, either as ModePlain or ModeTTY +var Mode = ModeAuto + // NewWriter returns a new multi-progress writer func NewWriter(out console.File) (Writer, error) { _, isTerminal := term.GetFdInfo(out) - - if isTerminal { - con, err := console.ConsoleFromFile(out) - if err != nil { - return nil, err - } - - return &ttyWriter{ - out: con, - eventIDs: []string{}, - events: map[string]Event{}, - repeated: false, - done: make(chan bool), - mtx: &sync.Mutex{}, - }, nil + if Mode == ModeAuto && isTerminal { + return newTTYWriter(out) + } + if Mode == ModeTTY { + return newTTYWriter(out) } - return &plainWriter{ out: out, done: make(chan bool), }, nil } + +func newTTYWriter(out console.File) (Writer, error) { + con, err := console.ConsoleFromFile(out) + if err != nil { + return nil, err + } + + return &ttyWriter{ + out: con, + eventIDs: []string{}, + events: map[string]Event{}, + repeated: false, + done: make(chan bool), + mtx: &sync.Mutex{}, + }, nil +}