diff --git a/cmd/compose/up.go b/cmd/compose/up.go index d9c3541ef9..c6a2a66a7d 100644 --- a/cmd/compose/up.go +++ b/cmd/compose/up.go @@ -32,6 +32,7 @@ import ( "github.com/spf13/cobra" "github.com/docker/compose/v2/pkg/api" + ui "github.com/docker/compose/v2/pkg/progress" "github.com/docker/compose/v2/pkg/utils" ) @@ -301,7 +302,7 @@ func runUp( WaitTimeout: timeout, Watch: upOptions.watch, Services: services, - NavigationMenu: upOptions.navigationMenu, + NavigationMenu: upOptions.navigationMenu && ui.Mode != "plain", }, }) } diff --git a/cmd/formatter/logs.go b/cmd/formatter/logs.go index 0b41611398..0342c6320d 100644 --- a/cmd/formatter/logs.go +++ b/cmd/formatter/logs.go @@ -25,6 +25,7 @@ import ( "sync" "time" + "github.com/buger/goterm" "github.com/docker/compose/v2/pkg/api" "github.com/docker/docker/pkg/jsonmessage" ) @@ -106,30 +107,28 @@ func (l *logConsumer) write(w io.Writer, container, message string) { if l.ctx.Err() != nil { return } - printFn := func() { - p := l.getPresenter(container) - timestamp := time.Now().Format(jsonmessage.RFC3339NanoFixed) - for _, line := range strings.Split(message, "\n") { - if KeyboardManager != nil { - ClearLine() - } - if l.timestamp { - fmt.Fprintf(w, "%s%s%s\n", p.prefix, timestamp, line) - } else { - fmt.Fprintf(w, "%s%s\n", p.prefix, line) - } + if KeyboardManager != nil { + KeyboardManager.ClearKeyboardInfo() + } + + p := l.getPresenter(container) + timestamp := time.Now().Format(jsonmessage.RFC3339NanoFixed) + for _, line := range strings.Split(message, "\n") { + if l.timestamp { + fmt.Fprintf(w, "%s%s%s\n", p.prefix, timestamp, line) + } else { + fmt.Fprintf(w, "%s%s\n", p.prefix, line) } } + if KeyboardManager != nil { - KeyboardManager.PrintKeyboardInfo(printFn) - } else { - printFn() + KeyboardManager.PrintKeyboardInfo() } } func (l *logConsumer) Status(container, msg string) { p := l.getPresenter(container) - s := p.colors(fmt.Sprintf("%s %s\n", container, msg)) + s := p.colors(fmt.Sprintf("%s%s %s\n", goterm.RESET_LINE, container, msg)) l.stdout.Write([]byte(s)) //nolint:errcheck } diff --git a/cmd/formatter/shortcut.go b/cmd/formatter/shortcut.go index 5b633076d0..fea7a1d47f 100644 --- a/cmd/formatter/shortcut.go +++ b/cmd/formatter/shortcut.go @@ -50,7 +50,7 @@ func (ke *KeyboardError) printError(height int, info string) { if ke.shouldDisplay() { errMessage := ke.err.Error() - MoveCursor(height-linesOffset(info)-linesOffset(errMessage)-1, 0) + MoveCursor(height-1-extraLines(info)-extraLines(errMessage), 0) ClearLine() fmt.Print(errMessage) @@ -132,13 +132,13 @@ func NewKeyboardManager(ctx context.Context, isDockerDesktopActive, isWatchConfi km.signalChannel = sc KeyboardManager = &km - - HideCursor() } -func (lk *LogKeyboard) PrintKeyboardInfo(printFn func()) { - printFn() +func (lk *LogKeyboard) ClearKeyboardInfo() { + lk.clearNavigationMenu() +} +func (lk *LogKeyboard) PrintKeyboardInfo() { if lk.logLevel == INFO { lk.printNavigationMenu() } @@ -146,27 +146,28 @@ func (lk *LogKeyboard) PrintKeyboardInfo(printFn func()) { // Creates space to print error and menu string func (lk *LogKeyboard) createBuffer(lines int) { - allocateSpace(lines) - if lk.kError.shouldDisplay() { - extraLines := linesOffset(lk.kError.error()) + 1 - allocateSpace(extraLines) + extraLines := extraLines(lk.kError.error()) + 1 lines += extraLines } + // get the string infoMessage := lk.navigationMenu() - extraLines := linesOffset(infoMessage) + 1 - allocateSpace(extraLines) + // calculate how many lines we need to display the menu info + // might be needed a line break + extraLines := extraLines(infoMessage) + 1 lines += extraLines if lines > 0 { + allocateSpace(lines) MoveCursorUp(lines) } } func (lk *LogKeyboard) printNavigationMenu() { + offset := 1 lk.clearNavigationMenu() - lk.createBuffer(0) + lk.createBuffer(offset) if lk.logLevel == INFO { height := goterm.Height() @@ -177,7 +178,7 @@ func (lk *LogKeyboard) printNavigationMenu() { lk.kError.printError(height, menu) - MoveCursor(height-linesOffset(menu), 0) + MoveCursor(height-extraLines(menu), 0) ClearLine() fmt.Print(menu) @@ -207,6 +208,8 @@ func (lk *LogKeyboard) clearNavigationMenu() { height := goterm.Height() MoveCursorX(0) SaveCursor() + + // ClearLine() for i := 0; i < height; i++ { MoveCursorDown(1) ClearLine() @@ -308,7 +311,7 @@ func allocateSpace(lines int) { } } -func linesOffset(s string) int { +func extraLines(s string) int { return int(math.Floor(float64(lenAnsi(s)) / float64(goterm.Width()))) } diff --git a/pkg/compose/up.go b/pkg/compose/up.go index a0ebdcbc24..be1fe4c04d 100644 --- a/pkg/compose/up.go +++ b/pkg/compose/up.go @@ -77,7 +77,6 @@ func (s *composeService) Up(ctx context.Context, project *types.Project, options first := true gracefulTeardown := func() { printer.Cancel() - formatter.ClearLine() fmt.Fprintln(s.stdinfo(), "Gracefully stopping... (press Ctrl+C again to force)") eg.Go(func() error { err := s.Stop(context.WithoutCancel(ctx), project.Name, api.StopOptions{