Skip to content

Commit

Permalink
fix: overlapping logs and menu navigation (#11765)
Browse files Browse the repository at this point in the history
  • Loading branch information
jhrotko committed Apr 24, 2024
1 parent c26d2fa commit 9c0b922
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 32 deletions.
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

0 comments on commit 9c0b922

Please sign in to comment.