Skip to content

Commit

Permalink
Fix menu print when logs/menu number of lines change
Browse files Browse the repository at this point in the history
Signed-off-by: Joana Hrotko <joana.hrotko@docker.com>
  • Loading branch information
jhrotko committed Apr 24, 2024
1 parent b53b8b2 commit 0cba9fe
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 31 deletions.
31 changes: 15 additions & 16 deletions cmd/formatter/logs.go
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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 0cba9fe

Please sign in to comment.