diff --git a/cmd/formatter/logs.go b/cmd/formatter/logs.go index dad44e78d12..6fda379c9d7 100644 --- a/cmd/formatter/logs.go +++ b/cmd/formatter/logs.go @@ -113,12 +113,12 @@ func (l *logConsumer) write(w io.Writer, container, message string) { // timestamp := time.Now().Format(jsonmessage.RFC3339NanoFixed) // for _, line := range strings.Split(message, "\n") { // if l.timestamp { - // fmt.Fprintf(w, "\033[K%s%s%s\n", p.prefix, timestamp, line) + // fmt.Fprintf(w, "%s%s%s\n", p.prefix, timestamp, line) // } else { - // fmt.Fprintf(w, "\033[K%s%s\n", p.prefix, line) + // fmt.Fprintf(w, "%s%s\n", p.prefix, line) // } // } - KeyboardInfo.PrintKeyboardInfo(func() { + KeyboardManager.PrintKeyboardInfo(func() { p := l.getPresenter(container) timestamp := time.Now().Format(jsonmessage.RFC3339NanoFixed) for _, line := range strings.Split(message, "\n") { diff --git a/cmd/formatter/shortcut.go b/cmd/formatter/shortcut.go index fd636b94ef2..35a3807d557 100644 --- a/cmd/formatter/shortcut.go +++ b/cmd/formatter/shortcut.go @@ -4,13 +4,17 @@ import ( "context" "fmt" "strings" + "time" "github.com/buger/goterm" "github.com/docker/compose/v2/pkg/watch" ) +var DISPLAY_ERROR_TIME = 10 + type LogKeyboard struct { err error + errStart time.Time started bool IsDockerDesktopActive bool Watcher watch.Notify @@ -19,7 +23,7 @@ type LogKeyboard struct { Cancel context.CancelFunc } -var KeyboardInfo = LogKeyboard{Watching: true} +var KeyboardManager = LogKeyboard{Watching: true} var errorColor = "\x1b[1;33m" func (lk *LogKeyboard) NewContext(ctx context.Context) context.CancelFunc { @@ -44,9 +48,11 @@ func (lk *LogKeyboard) PrintKeyboardInfo(print func()) { } func (lk *LogKeyboard) SError(err string) { + lk.errStart = time.Now() lk.err = fmt.Errorf(err) } func (lk *LogKeyboard) Error(err error) { + lk.errStart = time.Now() lk.err = err } @@ -57,13 +63,17 @@ func (lk *LogKeyboard) createBuffer() { fmt.Print("\033[2A") // go back 3 lines } -func (lk *LogKeyboard) printInfo() { - height := goterm.Height() - fmt.Print("\0337") // save cursor position - if lk.err != nil { +func (lk *LogKeyboard) printError(height int) { + if lk.err != nil && int(time.Since(lk.errStart).Seconds()) < DISPLAY_ERROR_TIME { fmt.Printf("\033[%d;0H", height-1) // Move to before last line fmt.Printf("\033[K" + errorColor + "[Error] " + lk.err.Error()) } +} + +func (lk *LogKeyboard) printInfo() { + height := goterm.Height() + fmt.Print("\0337") // save cursor position + lk.printError(height) fmt.Printf("\033[%d;0H", height) // Move to last line // clear line lk.infoMessage() @@ -101,3 +111,50 @@ func (lk *LogKeyboard) PrintEnter() { lk.ClearInfo() lk.printInfo() } + +// func HandleKeyEvents(ctx context.Context, event keyboard.KeyEvent, project types.Project, options api.UpOptions, handleTearDown func()) { +// switch key := event.Key; key { +// case keyboard.KeyCtrlC: +// keyboard.Close() +// KeyboardManager.ClearInfo() +// handleTearDown() +// case keyboard.KeyCtrlG: +// if KeyboardManager.IsDockerDesktopActive { +// link := fmt.Sprintf("docker-desktop://dashboard/apps/%s", project.Name) +// err := open.Run(link) +// if err != nil { +// KeyboardManager.SError("Could not open Docker Desktop") +// } else { +// KeyboardManager.Error(nil) +// } +// } +// case keyboard.KeyCtrlW: +// if KeyboardManager.Watching { +// KeyboardManager.Watching = !KeyboardManager.Watching +// fmt.Println("watching shortcut", KeyboardManager.Watching) + +// if KeyboardManager.Watching { +// KeyboardManager.Cancel() +// } else { +// KeyboardManager.NewContext(ctx) +// quit := make(chan error) +// go func() { +// buildOpts := *options.Create.Build +// buildOpts.Quiet = true +// err := s.Watch(KeyboardManager.Ctx, project, options.Start.Services, api.WatchOptions{ +// Build: &buildOpts, +// LogTo: options.Start.Attach, +// }) +// quit <- err +// }() +// KeyboardManager.Error(<-quit) +// } +// } +// case keyboard.KeyEnter: +// KeyboardManager.PrintEnter() +// default: +// if key != 0 { // If some key is pressed +// fmt.Println("key pressed: ", key) +// } +// } +// } diff --git a/pkg/compose/up.go b/pkg/compose/up.go index b91ca871d85..9ef2e103e18 100644 --- a/pkg/compose/up.go +++ b/pkg/compose/up.go @@ -79,9 +79,8 @@ func (s *composeService) Up(ctx context.Context, project *types.Project, options panic(err) } // formatter.KeyboardInfo.IsDockerDesktopActive = s.isDesktopIntegrationActive() - formatter.KeyboardInfo.IsDockerDesktopActive = true + formatter.KeyboardManager.IsDockerDesktopActive = true // formatter.KeyboardInfo.Watching = s.shouldWatch(project) - formatter.KeyboardInfo.Watching = true defer keyboard.Close() first := true gracefulTeardown := func() { @@ -104,42 +103,42 @@ func (s *composeService) Up(ctx context.Context, project *types.Project, options switch key := event.Key; key { case keyboard.KeyCtrlC: keyboard.Close() - formatter.KeyboardInfo.ClearInfo() + formatter.KeyboardManager.ClearInfo() gracefulTeardown() case keyboard.KeyCtrlG: - if formatter.KeyboardInfo.IsDockerDesktopActive { + if formatter.KeyboardManager.IsDockerDesktopActive { link := fmt.Sprintf("docker-desktop://dashboard/apps/%s", project.Name) err := open.Run(link) if err != nil { - formatter.KeyboardInfo.SError("Could not open Docker Desktop") + formatter.KeyboardManager.SError("Could not open Docker Desktop") } else { - formatter.KeyboardInfo.Error(nil) + formatter.KeyboardManager.Error(nil) } } case keyboard.KeyCtrlW: - if formatter.KeyboardInfo.Watching { - formatter.KeyboardInfo.Watching = !formatter.KeyboardInfo.Watching - fmt.Println("watching shortcut", formatter.KeyboardInfo.Watching) + if formatter.KeyboardManager.Watching { + formatter.KeyboardManager.Watching = !formatter.KeyboardManager.Watching + fmt.Println("watching shortcut", formatter.KeyboardManager.Watching) - if formatter.KeyboardInfo.Watching { - formatter.KeyboardInfo.Cancel() + if formatter.KeyboardManager.Watching { + formatter.KeyboardManager.Cancel() } else { - formatter.KeyboardInfo.NewContext(ctx) - quit := make(chan error) + formatter.KeyboardManager.NewContext(ctx) + errW := make(chan error) go func() { buildOpts := *options.Create.Build buildOpts.Quiet = true - err := s.Watch(formatter.KeyboardInfo.Ctx, project, options.Start.Services, api.WatchOptions{ + err := s.Watch(formatter.KeyboardManager.Ctx, project, options.Start.Services, api.WatchOptions{ Build: &buildOpts, LogTo: options.Start.Attach, }) - quit <- err + errW <- err }() - formatter.KeyboardInfo.Error(<-quit) + formatter.KeyboardManager.Error(<-errW) } } case keyboard.KeyEnter: - formatter.KeyboardInfo.PrintEnter() + formatter.KeyboardManager.PrintEnter() default: if key != 0 { // If some key is pressed fmt.Println("key pressed: ", key)