Skip to content

Commit 50169a3

Browse files
committed
display error for 10 secs
Signed-off-by: Joana Hrotko <[email protected]>
1 parent 71b9782 commit 50169a3

File tree

3 files changed

+81
-25
lines changed

3 files changed

+81
-25
lines changed

Diff for: cmd/formatter/logs.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -113,12 +113,12 @@ func (l *logConsumer) write(w io.Writer, container, message string) {
113113
// timestamp := time.Now().Format(jsonmessage.RFC3339NanoFixed)
114114
// for _, line := range strings.Split(message, "\n") {
115115
// if l.timestamp {
116-
// fmt.Fprintf(w, "\033[K%s%s%s\n", p.prefix, timestamp, line)
116+
// fmt.Fprintf(w, "%s%s%s\n", p.prefix, timestamp, line)
117117
// } else {
118-
// fmt.Fprintf(w, "\033[K%s%s\n", p.prefix, line)
118+
// fmt.Fprintf(w, "%s%s\n", p.prefix, line)
119119
// }
120120
// }
121-
KeyboardInfo.PrintKeyboardInfo(func() {
121+
KeyboardManager.PrintKeyboardInfo(func() {
122122
p := l.getPresenter(container)
123123
timestamp := time.Now().Format(jsonmessage.RFC3339NanoFixed)
124124
for _, line := range strings.Split(message, "\n") {

Diff for: cmd/formatter/shortcut.go

+62-5
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,17 @@ import (
44
"context"
55
"fmt"
66
"strings"
7+
"time"
78

89
"github.com/buger/goterm"
910
"github.com/docker/compose/v2/pkg/watch"
1011
)
1112

13+
var DISPLAY_ERROR_TIME = 3
14+
1215
type LogKeyboard struct {
1316
err error
17+
errStart time.Time
1418
started bool
1519
IsDockerDesktopActive bool
1620
Watcher watch.Notify
@@ -19,7 +23,7 @@ type LogKeyboard struct {
1923
Cancel context.CancelFunc
2024
}
2125

22-
var KeyboardInfo = LogKeyboard{Watching: true}
26+
var KeyboardManager = LogKeyboard{Watching: true}
2327
var errorColor = "\x1b[1;33m"
2428

2529
func (lk *LogKeyboard) NewContext(ctx context.Context) context.CancelFunc {
@@ -44,9 +48,11 @@ func (lk *LogKeyboard) PrintKeyboardInfo(print func()) {
4448
}
4549

4650
func (lk *LogKeyboard) SError(err string) {
51+
lk.errStart = time.Now()
4752
lk.err = fmt.Errorf(err)
4853
}
4954
func (lk *LogKeyboard) Error(err error) {
55+
lk.errStart = time.Now()
5056
lk.err = err
5157
}
5258

@@ -57,13 +63,17 @@ func (lk *LogKeyboard) createBuffer() {
5763
fmt.Print("\033[2A") // go back 3 lines
5864
}
5965

60-
func (lk *LogKeyboard) printInfo() {
61-
height := goterm.Height()
62-
fmt.Print("\0337") // save cursor position
63-
if lk.err != nil {
66+
func (lk *LogKeyboard) printError(height int) {
67+
if lk.err != nil && int(time.Since(lk.errStart).Seconds()) < DISPLAY_ERROR_TIME {
6468
fmt.Printf("\033[%d;0H", height-1) // Move to before last line
6569
fmt.Printf("\033[K" + errorColor + "[Error] " + lk.err.Error())
6670
}
71+
}
72+
73+
func (lk *LogKeyboard) printInfo() {
74+
height := goterm.Height()
75+
fmt.Print("\0337") // save cursor position
76+
lk.printError(height)
6777
fmt.Printf("\033[%d;0H", height) // Move to last line
6878
// clear line
6979
lk.infoMessage()
@@ -101,3 +111,50 @@ func (lk *LogKeyboard) PrintEnter() {
101111
lk.ClearInfo()
102112
lk.printInfo()
103113
}
114+
115+
// func HandleKeyEvents(ctx context.Context, event keyboard.KeyEvent, project types.Project, options api.UpOptions, handleTearDown func()) {
116+
// switch key := event.Key; key {
117+
// case keyboard.KeyCtrlC:
118+
// keyboard.Close()
119+
// KeyboardManager.ClearInfo()
120+
// handleTearDown()
121+
// case keyboard.KeyCtrlG:
122+
// if KeyboardManager.IsDockerDesktopActive {
123+
// link := fmt.Sprintf("docker-desktop://dashboard/apps/%s", project.Name)
124+
// err := open.Run(link)
125+
// if err != nil {
126+
// KeyboardManager.SError("Could not open Docker Desktop")
127+
// } else {
128+
// KeyboardManager.Error(nil)
129+
// }
130+
// }
131+
// case keyboard.KeyCtrlW:
132+
// if KeyboardManager.Watching {
133+
// KeyboardManager.Watching = !KeyboardManager.Watching
134+
// fmt.Println("watching shortcut", KeyboardManager.Watching)
135+
136+
// if KeyboardManager.Watching {
137+
// KeyboardManager.Cancel()
138+
// } else {
139+
// KeyboardManager.NewContext(ctx)
140+
// quit := make(chan error)
141+
// go func() {
142+
// buildOpts := *options.Create.Build
143+
// buildOpts.Quiet = true
144+
// err := s.Watch(KeyboardManager.Ctx, project, options.Start.Services, api.WatchOptions{
145+
// Build: &buildOpts,
146+
// LogTo: options.Start.Attach,
147+
// })
148+
// quit <- err
149+
// }()
150+
// KeyboardManager.Error(<-quit)
151+
// }
152+
// }
153+
// case keyboard.KeyEnter:
154+
// KeyboardManager.PrintEnter()
155+
// default:
156+
// if key != 0 { // If some key is pressed
157+
// fmt.Println("key pressed: ", key)
158+
// }
159+
// }
160+
// }

Diff for: pkg/compose/up.go

+16-17
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,8 @@ func (s *composeService) Up(ctx context.Context, project *types.Project, options
7979
panic(err)
8080
}
8181
// formatter.KeyboardInfo.IsDockerDesktopActive = s.isDesktopIntegrationActive()
82-
formatter.KeyboardInfo.IsDockerDesktopActive = true
82+
formatter.KeyboardManager.IsDockerDesktopActive = true
8383
// formatter.KeyboardInfo.Watching = s.shouldWatch(project)
84-
formatter.KeyboardInfo.Watching = true
8584
defer keyboard.Close()
8685
first := true
8786
gracefulTeardown := func() {
@@ -104,42 +103,42 @@ func (s *composeService) Up(ctx context.Context, project *types.Project, options
104103
switch key := event.Key; key {
105104
case keyboard.KeyCtrlC:
106105
keyboard.Close()
107-
formatter.KeyboardInfo.ClearInfo()
106+
formatter.KeyboardManager.ClearInfo()
108107
gracefulTeardown()
109108
case keyboard.KeyCtrlG:
110-
if formatter.KeyboardInfo.IsDockerDesktopActive {
109+
if formatter.KeyboardManager.IsDockerDesktopActive {
111110
link := fmt.Sprintf("docker-desktop://dashboard/apps/%s", project.Name)
112111
err := open.Run(link)
113112
if err != nil {
114-
formatter.KeyboardInfo.SError("Could not open Docker Desktop")
113+
formatter.KeyboardManager.SError("Could not open Docker Desktop")
115114
} else {
116-
formatter.KeyboardInfo.Error(nil)
115+
formatter.KeyboardManager.Error(nil)
117116
}
118117
}
119118
case keyboard.KeyCtrlW:
120-
if formatter.KeyboardInfo.Watching {
121-
formatter.KeyboardInfo.Watching = !formatter.KeyboardInfo.Watching
122-
fmt.Println("watching shortcut", formatter.KeyboardInfo.Watching)
119+
if formatter.KeyboardManager.Watching {
120+
formatter.KeyboardManager.Watching = !formatter.KeyboardManager.Watching
121+
fmt.Println("watching shortcut", formatter.KeyboardManager.Watching)
123122

124-
if formatter.KeyboardInfo.Watching {
125-
formatter.KeyboardInfo.Cancel()
123+
if formatter.KeyboardManager.Watching {
124+
formatter.KeyboardManager.Cancel()
126125
} else {
127-
formatter.KeyboardInfo.NewContext(ctx)
128-
quit := make(chan error)
126+
formatter.KeyboardManager.NewContext(ctx)
127+
errW := make(chan error)
129128
go func() {
130129
buildOpts := *options.Create.Build
131130
buildOpts.Quiet = true
132-
err := s.Watch(formatter.KeyboardInfo.Ctx, project, options.Start.Services, api.WatchOptions{
131+
err := s.Watch(formatter.KeyboardManager.Ctx, project, options.Start.Services, api.WatchOptions{
133132
Build: &buildOpts,
134133
LogTo: options.Start.Attach,
135134
})
136-
quit <- err
135+
errW <- err
137136
}()
138-
formatter.KeyboardInfo.Error(<-quit)
137+
formatter.KeyboardManager.Error(<-errW)
139138
}
140139
}
141140
case keyboard.KeyEnter:
142-
formatter.KeyboardInfo.PrintEnter()
141+
formatter.KeyboardManager.PrintEnter()
143142
default:
144143
if key != 0 { // If some key is pressed
145144
fmt.Println("key pressed: ", key)

0 commit comments

Comments
 (0)