Skip to content

Commit 9c0b922

Browse files
authored
fix: overlapping logs and menu navigation (docker#11765)
1 parent c26d2fa commit 9c0b922

File tree

4 files changed

+34
-32
lines changed

4 files changed

+34
-32
lines changed

cmd/compose/up.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import (
3232
"github.com/spf13/cobra"
3333

3434
"github.com/docker/compose/v2/pkg/api"
35+
ui "github.com/docker/compose/v2/pkg/progress"
3536
"github.com/docker/compose/v2/pkg/utils"
3637
)
3738

@@ -301,7 +302,7 @@ func runUp(
301302
WaitTimeout: timeout,
302303
Watch: upOptions.watch,
303304
Services: services,
304-
NavigationMenu: upOptions.navigationMenu,
305+
NavigationMenu: upOptions.navigationMenu && ui.Mode != "plain",
305306
},
306307
})
307308
}

cmd/formatter/logs.go

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"sync"
2626
"time"
2727

28+
"github.com/buger/goterm"
2829
"github.com/docker/compose/v2/pkg/api"
2930
"github.com/docker/docker/pkg/jsonmessage"
3031
)
@@ -106,30 +107,28 @@ func (l *logConsumer) write(w io.Writer, container, message string) {
106107
if l.ctx.Err() != nil {
107108
return
108109
}
109-
printFn := func() {
110-
p := l.getPresenter(container)
111-
timestamp := time.Now().Format(jsonmessage.RFC3339NanoFixed)
112-
for _, line := range strings.Split(message, "\n") {
113-
if KeyboardManager != nil {
114-
ClearLine()
115-
}
116-
if l.timestamp {
117-
fmt.Fprintf(w, "%s%s%s\n", p.prefix, timestamp, line)
118-
} else {
119-
fmt.Fprintf(w, "%s%s\n", p.prefix, line)
120-
}
110+
if KeyboardManager != nil {
111+
KeyboardManager.ClearKeyboardInfo()
112+
}
113+
114+
p := l.getPresenter(container)
115+
timestamp := time.Now().Format(jsonmessage.RFC3339NanoFixed)
116+
for _, line := range strings.Split(message, "\n") {
117+
if l.timestamp {
118+
fmt.Fprintf(w, "%s%s%s\n", p.prefix, timestamp, line)
119+
} else {
120+
fmt.Fprintf(w, "%s%s\n", p.prefix, line)
121121
}
122122
}
123+
123124
if KeyboardManager != nil {
124-
KeyboardManager.PrintKeyboardInfo(printFn)
125-
} else {
126-
printFn()
125+
KeyboardManager.PrintKeyboardInfo()
127126
}
128127
}
129128

130129
func (l *logConsumer) Status(container, msg string) {
131130
p := l.getPresenter(container)
132-
s := p.colors(fmt.Sprintf("%s %s\n", container, msg))
131+
s := p.colors(fmt.Sprintf("%s%s %s\n", goterm.RESET_LINE, container, msg))
133132
l.stdout.Write([]byte(s)) //nolint:errcheck
134133
}
135134

cmd/formatter/shortcut.go

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ func (ke *KeyboardError) printError(height int, info string) {
5050
if ke.shouldDisplay() {
5151
errMessage := ke.err.Error()
5252

53-
MoveCursor(height-linesOffset(info)-linesOffset(errMessage)-1, 0)
53+
MoveCursor(height-1-extraLines(info)-extraLines(errMessage), 0)
5454
ClearLine()
5555

5656
fmt.Print(errMessage)
@@ -132,41 +132,42 @@ func NewKeyboardManager(ctx context.Context, isDockerDesktopActive, isWatchConfi
132132
km.signalChannel = sc
133133

134134
KeyboardManager = &km
135-
136-
HideCursor()
137135
}
138136

139-
func (lk *LogKeyboard) PrintKeyboardInfo(printFn func()) {
140-
printFn()
137+
func (lk *LogKeyboard) ClearKeyboardInfo() {
138+
lk.clearNavigationMenu()
139+
}
141140

141+
func (lk *LogKeyboard) PrintKeyboardInfo() {
142142
if lk.logLevel == INFO {
143143
lk.printNavigationMenu()
144144
}
145145
}
146146

147147
// Creates space to print error and menu string
148148
func (lk *LogKeyboard) createBuffer(lines int) {
149-
allocateSpace(lines)
150-
151149
if lk.kError.shouldDisplay() {
152-
extraLines := linesOffset(lk.kError.error()) + 1
153-
allocateSpace(extraLines)
150+
extraLines := extraLines(lk.kError.error()) + 1
154151
lines += extraLines
155152
}
156153

154+
// get the string
157155
infoMessage := lk.navigationMenu()
158-
extraLines := linesOffset(infoMessage) + 1
159-
allocateSpace(extraLines)
156+
// calculate how many lines we need to display the menu info
157+
// might be needed a line break
158+
extraLines := extraLines(infoMessage) + 1
160159
lines += extraLines
161160

162161
if lines > 0 {
162+
allocateSpace(lines)
163163
MoveCursorUp(lines)
164164
}
165165
}
166166

167167
func (lk *LogKeyboard) printNavigationMenu() {
168+
offset := 1
168169
lk.clearNavigationMenu()
169-
lk.createBuffer(0)
170+
lk.createBuffer(offset)
170171

171172
if lk.logLevel == INFO {
172173
height := goterm.Height()
@@ -177,7 +178,7 @@ func (lk *LogKeyboard) printNavigationMenu() {
177178

178179
lk.kError.printError(height, menu)
179180

180-
MoveCursor(height-linesOffset(menu), 0)
181+
MoveCursor(height-extraLines(menu), 0)
181182
ClearLine()
182183
fmt.Print(menu)
183184

@@ -207,6 +208,8 @@ func (lk *LogKeyboard) clearNavigationMenu() {
207208
height := goterm.Height()
208209
MoveCursorX(0)
209210
SaveCursor()
211+
212+
// ClearLine()
210213
for i := 0; i < height; i++ {
211214
MoveCursorDown(1)
212215
ClearLine()
@@ -308,7 +311,7 @@ func allocateSpace(lines int) {
308311
}
309312
}
310313

311-
func linesOffset(s string) int {
314+
func extraLines(s string) int {
312315
return int(math.Floor(float64(lenAnsi(s)) / float64(goterm.Width())))
313316
}
314317

pkg/compose/up.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,6 @@ func (s *composeService) Up(ctx context.Context, project *types.Project, options
7777
first := true
7878
gracefulTeardown := func() {
7979
printer.Cancel()
80-
formatter.ClearLine()
8180
fmt.Fprintln(s.stdinfo(), "Gracefully stopping... (press Ctrl+C again to force)")
8281
eg.Go(func() error {
8382
err := s.Stop(context.WithoutCancel(ctx), project.Name, api.StopOptions{

0 commit comments

Comments
 (0)