fix: overlapping logs and menu navigation (#11765)

This commit is contained in:
Joana H 2024-04-24 19:59:42 +01:00 committed by GitHub
parent c26d2fa7cf
commit 9c0b922597
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 34 additions and 32 deletions

View File

@ -32,6 +32,7 @@ import (
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/docker/compose/v2/pkg/api" "github.com/docker/compose/v2/pkg/api"
ui "github.com/docker/compose/v2/pkg/progress"
"github.com/docker/compose/v2/pkg/utils" "github.com/docker/compose/v2/pkg/utils"
) )
@ -301,7 +302,7 @@ func runUp(
WaitTimeout: timeout, WaitTimeout: timeout,
Watch: upOptions.watch, Watch: upOptions.watch,
Services: services, Services: services,
NavigationMenu: upOptions.navigationMenu, NavigationMenu: upOptions.navigationMenu && ui.Mode != "plain",
}, },
}) })
} }

View File

@ -25,6 +25,7 @@ import (
"sync" "sync"
"time" "time"
"github.com/buger/goterm"
"github.com/docker/compose/v2/pkg/api" "github.com/docker/compose/v2/pkg/api"
"github.com/docker/docker/pkg/jsonmessage" "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 { if l.ctx.Err() != nil {
return return
} }
printFn := func() { if KeyboardManager != nil {
p := l.getPresenter(container) KeyboardManager.ClearKeyboardInfo()
timestamp := time.Now().Format(jsonmessage.RFC3339NanoFixed) }
for _, line := range strings.Split(message, "\n") {
if KeyboardManager != nil { p := l.getPresenter(container)
ClearLine() timestamp := time.Now().Format(jsonmessage.RFC3339NanoFixed)
} for _, line := range strings.Split(message, "\n") {
if l.timestamp { if l.timestamp {
fmt.Fprintf(w, "%s%s%s\n", p.prefix, timestamp, line) fmt.Fprintf(w, "%s%s%s\n", p.prefix, timestamp, line)
} else { } else {
fmt.Fprintf(w, "%s%s\n", p.prefix, line) fmt.Fprintf(w, "%s%s\n", p.prefix, line)
}
} }
} }
if KeyboardManager != nil { if KeyboardManager != nil {
KeyboardManager.PrintKeyboardInfo(printFn) KeyboardManager.PrintKeyboardInfo()
} else {
printFn()
} }
} }
func (l *logConsumer) Status(container, msg string) { func (l *logConsumer) Status(container, msg string) {
p := l.getPresenter(container) 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 l.stdout.Write([]byte(s)) //nolint:errcheck
} }

View File

@ -50,7 +50,7 @@ func (ke *KeyboardError) printError(height int, info string) {
if ke.shouldDisplay() { if ke.shouldDisplay() {
errMessage := ke.err.Error() errMessage := ke.err.Error()
MoveCursor(height-linesOffset(info)-linesOffset(errMessage)-1, 0) MoveCursor(height-1-extraLines(info)-extraLines(errMessage), 0)
ClearLine() ClearLine()
fmt.Print(errMessage) fmt.Print(errMessage)
@ -132,13 +132,13 @@ func NewKeyboardManager(ctx context.Context, isDockerDesktopActive, isWatchConfi
km.signalChannel = sc km.signalChannel = sc
KeyboardManager = &km KeyboardManager = &km
HideCursor()
} }
func (lk *LogKeyboard) PrintKeyboardInfo(printFn func()) { func (lk *LogKeyboard) ClearKeyboardInfo() {
printFn() lk.clearNavigationMenu()
}
func (lk *LogKeyboard) PrintKeyboardInfo() {
if lk.logLevel == INFO { if lk.logLevel == INFO {
lk.printNavigationMenu() lk.printNavigationMenu()
} }
@ -146,27 +146,28 @@ func (lk *LogKeyboard) PrintKeyboardInfo(printFn func()) {
// Creates space to print error and menu string // Creates space to print error and menu string
func (lk *LogKeyboard) createBuffer(lines int) { func (lk *LogKeyboard) createBuffer(lines int) {
allocateSpace(lines)
if lk.kError.shouldDisplay() { if lk.kError.shouldDisplay() {
extraLines := linesOffset(lk.kError.error()) + 1 extraLines := extraLines(lk.kError.error()) + 1
allocateSpace(extraLines)
lines += extraLines lines += extraLines
} }
// get the string
infoMessage := lk.navigationMenu() infoMessage := lk.navigationMenu()
extraLines := linesOffset(infoMessage) + 1 // calculate how many lines we need to display the menu info
allocateSpace(extraLines) // might be needed a line break
extraLines := extraLines(infoMessage) + 1
lines += extraLines lines += extraLines
if lines > 0 { if lines > 0 {
allocateSpace(lines)
MoveCursorUp(lines) MoveCursorUp(lines)
} }
} }
func (lk *LogKeyboard) printNavigationMenu() { func (lk *LogKeyboard) printNavigationMenu() {
offset := 1
lk.clearNavigationMenu() lk.clearNavigationMenu()
lk.createBuffer(0) lk.createBuffer(offset)
if lk.logLevel == INFO { if lk.logLevel == INFO {
height := goterm.Height() height := goterm.Height()
@ -177,7 +178,7 @@ func (lk *LogKeyboard) printNavigationMenu() {
lk.kError.printError(height, menu) lk.kError.printError(height, menu)
MoveCursor(height-linesOffset(menu), 0) MoveCursor(height-extraLines(menu), 0)
ClearLine() ClearLine()
fmt.Print(menu) fmt.Print(menu)
@ -207,6 +208,8 @@ func (lk *LogKeyboard) clearNavigationMenu() {
height := goterm.Height() height := goterm.Height()
MoveCursorX(0) MoveCursorX(0)
SaveCursor() SaveCursor()
// ClearLine()
for i := 0; i < height; i++ { for i := 0; i < height; i++ {
MoveCursorDown(1) MoveCursorDown(1)
ClearLine() 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()))) return int(math.Floor(float64(lenAnsi(s)) / float64(goterm.Width())))
} }

View File

@ -77,7 +77,6 @@ func (s *composeService) Up(ctx context.Context, project *types.Project, options
first := true first := true
gracefulTeardown := func() { gracefulTeardown := func() {
printer.Cancel() printer.Cancel()
formatter.ClearLine()
fmt.Fprintln(s.stdinfo(), "Gracefully stopping... (press Ctrl+C again to force)") fmt.Fprintln(s.stdinfo(), "Gracefully stopping... (press Ctrl+C again to force)")
eg.Go(func() error { eg.Go(func() error {
err := s.Stop(context.WithoutCancel(ctx), project.Name, api.StopOptions{ err := s.Stop(context.WithoutCancel(ctx), project.Name, api.StopOptions{