Skip to content

Commit

Permalink
display error for 10 secs
Browse files Browse the repository at this point in the history
Signed-off-by: Joana Hrotko <[email protected]>
  • Loading branch information
jhrotko committed Mar 13, 2024
1 parent 71b9782 commit d71a2de
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 25 deletions.
6 changes: 3 additions & 3 deletions cmd/formatter/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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") {
Expand Down
67 changes: 62 additions & 5 deletions cmd/formatter/shortcut.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand All @@ -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
}

Expand All @@ -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()
Expand Down Expand Up @@ -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)
// }
// }
// }
33 changes: 16 additions & 17 deletions pkg/compose/up.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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)
Expand Down

0 comments on commit d71a2de

Please sign in to comment.