Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix menu print when logs/menu number of lines change #11765

Merged
merged 1 commit into from Apr 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion cmd/compose/up.go
Expand Up @@ -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"
)

Expand Down Expand Up @@ -301,7 +302,7 @@ func runUp(
WaitTimeout: timeout,
Watch: upOptions.watch,
Services: services,
NavigationMenu: upOptions.navigationMenu,
NavigationMenu: upOptions.navigationMenu && ui.Mode != "plain",
},
})
}
Expand Down
31 changes: 15 additions & 16 deletions cmd/formatter/logs.go
Expand Up @@ -25,6 +25,7 @@ import (
"sync"
"time"

"github.com/buger/goterm"
"github.com/docker/compose/v2/pkg/api"
"github.com/docker/docker/pkg/jsonmessage"
)
Expand Down Expand Up @@ -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
}

Expand Down
31 changes: 17 additions & 14 deletions cmd/formatter/shortcut.go
Expand Up @@ -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)
Expand Down Expand Up @@ -132,41 +132,42 @@ 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()
}
}

// 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()
Expand All @@ -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)

Expand Down Expand Up @@ -207,6 +208,8 @@ func (lk *LogKeyboard) clearNavigationMenu() {
height := goterm.Height()
MoveCursorX(0)
SaveCursor()

// ClearLine()
for i := 0; i < height; i++ {
MoveCursorDown(1)
ClearLine()
Expand Down Expand Up @@ -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())))
}

Expand Down
1 change: 0 additions & 1 deletion pkg/compose/up.go
Expand Up @@ -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{
Expand Down