From 42d46784276dd82eef9a9ec32097420086d2f720 Mon Sep 17 00:00:00 2001 From: Joana Hrotko Date: Mon, 11 Mar 2024 11:31:11 +0000 Subject: [PATCH] Fix error message Signed-off-by: Joana Hrotko --- cmd/formatter/logs.go | 2 +- cmd/formatter/shortcut.go | 34 ++++++++++++++++++++++++++-------- pkg/compose/up.go | 24 ++++++++---------------- 3 files changed, 35 insertions(+), 25 deletions(-) diff --git a/cmd/formatter/logs.go b/cmd/formatter/logs.go index 8a2927fc930..435d28832d6 100644 --- a/cmd/formatter/logs.go +++ b/cmd/formatter/logs.go @@ -127,7 +127,7 @@ func (l *logConsumer) write(w io.Writer, container, message string) { fmt.Fprintf(w, "\033[K%s%s\n", p.prefix, line) } } - }, fmt.Errorf("OH NO")) + }) // // save cursor position // fmt.Print("\0337") diff --git a/cmd/formatter/shortcut.go b/cmd/formatter/shortcut.go index 048aa431b1c..6bc70d094bc 100644 --- a/cmd/formatter/shortcut.go +++ b/cmd/formatter/shortcut.go @@ -9,24 +9,43 @@ import ( type LogKeyboard struct { // mutex sync.Mutex // message string - err error + err error + started bool } var KeyboardInfo = LogKeyboard{} var errorColor = "\x1b[1;33m" -func (lk *LogKeyboard) PrintKeyboardInfo(print func(), err error) { +func (lk *LogKeyboard) PrintKeyboardInfo(print func()) { fmt.Print("\033[?25l") // hide cursor defer fmt.Printf("\033[?25h") // show cursor - lk.err = err - lk.ClearInfo() + if lk.started { + lk.ClearInfo() + } else { + lk.started = true + } print() + lk.createBuffer() lk.printInfo() } + +func (lk *LogKeyboard) SError(err string) { + lk.err = fmt.Errorf(err) +} +func (lk *LogKeyboard) Error(err error) { + lk.err = err +} + +// This avoids incorrect printing at the end of the terminal +func (lk *LogKeyboard) createBuffer() { + fmt.Print("\012") // new line + fmt.Print("\012") + 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 { fmt.Printf("\033[%d;0H", height-1) // Move to before last line @@ -40,14 +59,13 @@ func (lk *LogKeyboard) printInfo() { func (lk *LogKeyboard) ClearInfo() { height := goterm.Height() - fmt.Print("\0337") // save cursor position if lk.err != nil { fmt.Printf("\033[%d;0H", height-1) - fmt.Print("\033[0K") // clear line + fmt.Print("\033[2K") // clear line } fmt.Printf("\033[%d;0H", height) // Move to last line - fmt.Print("\033[0K") // clear line + fmt.Print("\033[2K") // clear line fmt.Print("\0338") // restore cursor position } diff --git a/pkg/compose/up.go b/pkg/compose/up.go index ae3796da9cf..93d76c2d2f0 100644 --- a/pkg/compose/up.go +++ b/pkg/compose/up.go @@ -31,6 +31,7 @@ import ( "github.com/docker/compose/v2/pkg/progress" "github.com/eiannone/keyboard" "github.com/hashicorp/go-multierror" + "github.com/skratchdot/open-golang/open" ) func (s *composeService) Up(ctx context.Context, project *types.Project, options api.UpOptions) error { //nolint:gocyclo @@ -103,25 +104,16 @@ func (s *composeService) Up(ctx context.Context, project *types.Project, options gracefulTeardown() case keyboard.KeyCtrlG: link := fmt.Sprintf("docker-desktop://dashboard/apps/%s", project.Name) - // err := fmt.Errorf("OH NO!\n") - // if err != nil { - // fmt.Print("\0337") // save cursor position - // fmt.Println("\033[0;0H") // Move to top - // fmt.Printf("\033[0;34m") //change color - // fmt.Printf("\033[%d;0H", goterm.Height()-1) // Move to last line - // fmt.Printf("\033[K%s", err.Error()) - // // fmt.Println("\033[0m") // restore color - // fmt.Println("\033[u") //restore - // } - // err := open.Run(link) - fmt.Println("link: ", link) - // if err != nil { - // fmt.Fprintln(s.stdinfo(), "Could not open Docker Desktop") - // } + err := open.Run(link) + if err != nil { + formatter.KeyboardInfo.SError("Could not open Docker Desktop") + } else { + formatter.KeyboardInfo.Error(nil) + } case keyboard.KeyEnter: formatter.KeyboardInfo.PrintEnter() default: - if key != 0 { + if key != 0 { // If some key is pressed fmt.Println("key pressed: ", key) } }