Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions cmd/formatter/ansi.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,73 +28,73 @@ func ansi(code string) string {
return fmt.Sprintf("\033%s", code)
}

func SaveCursor() {
func saveCursor() {
if disableAnsi {
return
}
fmt.Print(ansi("7"))
}

func RestoreCursor() {
func restoreCursor() {
if disableAnsi {
return
}
fmt.Print(ansi("8"))
}

func HideCursor() {
func hideCursor() {
if disableAnsi {
return
}
fmt.Print(ansi("[?25l"))
}

func ShowCursor() {
func showCursor() {
if disableAnsi {
return
}
fmt.Print(ansi("[?25h"))
}

func MoveCursor(y, x int) {
func moveCursor(y, x int) {
if disableAnsi {
return
}
fmt.Print(ansi(fmt.Sprintf("[%d;%dH", y, x)))
}

func MoveCursorX(pos int) {
func carriageReturn() {
if disableAnsi {
return
}
fmt.Print(ansi(fmt.Sprintf("[%dG", pos)))
fmt.Print(ansi(fmt.Sprintf("[%dG", 0)))
}

func ClearLine() {
func clearLine() {
if disableAnsi {
return
}
// Does not move cursor from its current position
fmt.Print(ansi("[2K"))
}

func MoveCursorUp(lines int) {
func moveCursorUp(lines int) {
if disableAnsi {
return
}
// Does not add new lines
fmt.Print(ansi(fmt.Sprintf("[%dA", lines)))
}

func MoveCursorDown(lines int) {
func moveCursorDown(lines int) {
if disableAnsi {
return
}
// Does not add new lines
fmt.Print(ansi(fmt.Sprintf("[%dB", lines)))
}

func NewLine() {
func newLine() {
// Like \n
fmt.Print("\012")
}
Expand Down
7 changes: 5 additions & 2 deletions cmd/formatter/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,12 @@ func (l *logConsumer) register(name string) *presenter {
} else {
cf := monochrome
if l.color {
if name == api.WatchLogger {
switch name {
case "":
cf = monochrome
case api.WatchLogger:
cf = makeColorFunc("92")
} else {
default:
cf = nextColor()
}
}
Expand Down
40 changes: 20 additions & 20 deletions cmd/formatter/shortcut.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ func (ke *KeyboardError) printError(height int, info string) {
if ke.shouldDisplay() {
errMessage := ke.err.Error()

MoveCursor(height-1-extraLines(info)-extraLines(errMessage), 0)
ClearLine()
moveCursor(height-1-extraLines(info)-extraLines(errMessage), 0)
clearLine()

fmt.Print(errMessage)
}
Expand Down Expand Up @@ -133,7 +133,7 @@ func (lk *LogKeyboard) createBuffer(lines int) {

if lines > 0 {
allocateSpace(lines)
MoveCursorUp(lines)
moveCursorUp(lines)
}
}

Expand All @@ -146,17 +146,17 @@ func (lk *LogKeyboard) printNavigationMenu() {
height := goterm.Height()
menu := lk.navigationMenu()

MoveCursorX(0)
SaveCursor()
carriageReturn()
saveCursor()

lk.kError.printError(height, menu)

MoveCursor(height-extraLines(menu), 0)
ClearLine()
moveCursor(height-extraLines(menu), 0)
clearLine()
fmt.Print(menu)

MoveCursorX(0)
RestoreCursor()
carriageReturn()
restoreCursor()
}
}

Expand Down Expand Up @@ -188,15 +188,15 @@ func (lk *LogKeyboard) navigationMenu() string {

func (lk *LogKeyboard) clearNavigationMenu() {
height := goterm.Height()
MoveCursorX(0)
SaveCursor()
carriageReturn()
saveCursor()

// ClearLine()
// clearLine()
for i := 0; i < height; i++ {
MoveCursorDown(1)
ClearLine()
moveCursorDown(1)
clearLine()
}
RestoreCursor()
restoreCursor()
}

func (lk *LogKeyboard) openDockerDesktop(ctx context.Context, project *types.Project) {
Expand Down Expand Up @@ -316,13 +316,13 @@ func (lk *LogKeyboard) HandleKeyEvents(ctx context.Context, event keyboard.KeyEv
case keyboard.KeyCtrlC:
_ = keyboard.Close()
lk.clearNavigationMenu()
ShowCursor()
showCursor()

lk.logLevel = NONE
// will notify main thread to kill and will handle gracefully
lk.signalChannel <- syscall.SIGINT
case keyboard.KeyEnter:
NewLine()
newLine()
lk.printNavigationMenu()
}
}
Expand All @@ -336,9 +336,9 @@ func (lk *LogKeyboard) EnableWatch(enabled bool, watcher Feature) {

func allocateSpace(lines int) {
for i := 0; i < lines; i++ {
ClearLine()
NewLine()
MoveCursorX(0)
clearLine()
newLine()
carriageReturn()
}
}

Expand Down
119 changes: 119 additions & 0 deletions cmd/formatter/stopping.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
/*
Copyright 2024 Docker Compose CLI authors

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package formatter

import (
"fmt"
"strings"
"time"

"github.com/buger/goterm"
"github.com/docker/compose/v2/pkg/api"
"github.com/docker/compose/v2/pkg/progress"
)

type Stopping struct {
api.LogConsumer
enabled bool
spinner *progress.Spinner
ticker *time.Ticker
startedAt time.Time
}

func NewStopping(l api.LogConsumer) *Stopping {
s := &Stopping{}
s.LogConsumer = logDecorator{
decorated: l,
Before: s.clear,
After: s.print,
}
return s
}

func (s *Stopping) ApplicationTermination() {
if progress.Mode != progress.ModeAuto {
// User explicitly opted for output format
return
}
if disableAnsi {
return
}
s.enabled = true
s.spinner = progress.NewSpinner()
hideCursor()
s.startedAt = time.Now()
s.ticker = time.NewTicker(100 * time.Millisecond)
go func() {
for {
<-s.ticker.C
s.print()
}
}()
}

func (s *Stopping) Close() {
showCursor()
if s.ticker != nil {
s.ticker.Stop()
}
s.clear()
}

func (s *Stopping) clear() {
if !s.enabled {
return
}

height := goterm.Height()
carriageReturn()
saveCursor()

// clearLine()
for i := 0; i < height; i++ {
moveCursorDown(1)
clearLine()
}
restoreCursor()
}

const stoppingBanner = "Gracefully Stopping... (press Ctrl+C again to force)"

func (s *Stopping) print() {
if !s.enabled {
return
}

height := goterm.Height()
width := goterm.Width()
carriageReturn()
saveCursor()

moveCursor(height, 0)
clearLine()
elapsed := time.Since(s.startedAt).Seconds()
timer := fmt.Sprintf("%.1fs ", elapsed)
pad := width - len(timer) - len(stoppingBanner) - 5
fmt.Printf("%s %s %s %s",
progress.CountColor(s.spinner.String()),
stoppingBanner,
strings.Repeat(" ", pad),
progress.TimerColor(timer),
)

carriageReturn()
restoreCursor()
}
2 changes: 1 addition & 1 deletion pkg/compose/convergence.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ func (c *convergence) stopDependentContainers(ctx context.Context, project *type
err := c.service.stop(ctx, project.Name, api.StopOptions{
Services: dependents,
Project: project,
})
}, nil)
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/compose/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -1428,7 +1428,7 @@ func (s *composeService) removeDivergedNetwork(ctx context.Context, project *typ
err := s.stop(ctx, project.Name, api.StopOptions{
Services: services,
Project: project,
})
}, nil)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -1599,7 +1599,7 @@ func (s *composeService) removeDivergedVolume(ctx context.Context, name string,
err := s.stop(ctx, project.Name, api.StopOptions{
Services: services,
Project: project,
})
}, nil)
if err != nil {
return err
}
Expand Down
18 changes: 13 additions & 5 deletions pkg/compose/down.go
Original file line number Diff line number Diff line change
Expand Up @@ -298,13 +298,17 @@ func (s *composeService) removeVolume(ctx context.Context, id string, w progress
return err
}

func (s *composeService) stopContainer(ctx context.Context, w progress.Writer, service *types.ServiceConfig, ctr containerType.Summary, timeout *time.Duration) error {
func (s *composeService) stopContainer(
ctx context.Context, w progress.Writer,
service *types.ServiceConfig, ctr containerType.Summary,
timeout *time.Duration, listener api.ContainerEventListener,
) error {
eventName := getContainerProgressName(ctr)
w.Event(progress.StoppingEvent(eventName))

if service != nil {
for _, hook := range service.PreStop {
err := s.runHook(ctx, ctr, *service, hook, nil)
err := s.runHook(ctx, ctr, *service, hook, listener)
if err != nil {
// Ignore errors indicating that some containers were already stopped or removed.
if cerrdefs.IsNotFound(err) || cerrdefs.IsConflict(err) {
Expand All @@ -325,11 +329,15 @@ func (s *composeService) stopContainer(ctx context.Context, w progress.Writer, s
return nil
}

func (s *composeService) stopContainers(ctx context.Context, w progress.Writer, serv *types.ServiceConfig, containers []containerType.Summary, timeout *time.Duration) error {
func (s *composeService) stopContainers(
ctx context.Context, w progress.Writer,
serv *types.ServiceConfig, containers []containerType.Summary,
timeout *time.Duration, listener api.ContainerEventListener,
) error {
eg, ctx := errgroup.WithContext(ctx)
for _, ctr := range containers {
eg.Go(func() error {
return s.stopContainer(ctx, w, serv, ctr, timeout)
return s.stopContainer(ctx, w, serv, ctr, timeout, listener)
})
}
return eg.Wait()
Expand All @@ -348,7 +356,7 @@ func (s *composeService) removeContainers(ctx context.Context, containers []cont
func (s *composeService) stopAndRemoveContainer(ctx context.Context, ctr containerType.Summary, service *types.ServiceConfig, timeout *time.Duration, volumes bool) error {
w := progress.ContextWriter(ctx)
eventName := getContainerProgressName(ctr)
err := s.stopContainer(ctx, w, service, ctr, timeout)
err := s.stopContainer(ctx, w, service, ctr, timeout, nil)
if cerrdefs.IsNotFound(err) {
w.Event(progress.RemovedEvent(eventName))
return nil
Expand Down
4 changes: 1 addition & 3 deletions pkg/compose/printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,9 +149,7 @@ func (p *printer) Run(cascade api.Cascade, exitCodeFrom string, stopFn func() er
return exitCode, nil
}
case api.ContainerEventLog, api.HookEventLog:
if !aborting {
p.consumer.Log(container, event.Line)
}
p.consumer.Log(container, event.Line)
case api.ContainerEventErr:
if !aborting {
p.consumer.Err(container, event.Line)
Expand Down
Loading