Skip to content

Commit 6e55832

Browse files
ndeloofglours
authored andcommitted
add (restore) support for detach keys
Signed-off-by: Nicolas De Loof <[email protected]>
1 parent 45def51 commit 6e55832

File tree

6 files changed

+34
-19
lines changed

6 files changed

+34
-19
lines changed

cmd/formatter/shortcut.go

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"fmt"
2323
"math"
2424
"os"
25+
"strings"
2526
"syscall"
2627
"time"
2728

@@ -90,6 +91,7 @@ const (
9091
type LogKeyboard struct {
9192
kError KeyboardError
9293
Watch *KeyboardWatch
94+
Detach func()
9395
IsDockerDesktopActive bool
9496
logLevel KEYBOARD_LOG_LEVEL
9597
signalChannel chan<- os.Signal
@@ -161,29 +163,23 @@ func (lk *LogKeyboard) printNavigationMenu() {
161163
}
162164

163165
func (lk *LogKeyboard) navigationMenu() string {
164-
var openDDInfo string
166+
var items []string
165167
if lk.IsDockerDesktopActive {
166-
openDDInfo = shortcutKeyColor("v") + navColor(" View in Docker Desktop")
168+
items = append(items, shortcutKeyColor("v")+navColor(" View in Docker Desktop"))
167169
}
168170

169-
var openDDUI string
170-
if openDDInfo != "" {
171-
openDDUI = navColor(" ")
172-
}
173171
if lk.IsDockerDesktopActive {
174-
openDDUI = openDDUI + shortcutKeyColor("o") + navColor(" View Config")
172+
items = append(items, shortcutKeyColor("o")+navColor(" View Config"))
175173
}
176174

177-
var watchInfo string
178-
if openDDInfo != "" || openDDUI != "" {
179-
watchInfo = navColor(" ")
180-
}
181175
isEnabled := " Enable"
182176
if lk.Watch != nil && lk.Watch.Watching {
183177
isEnabled = " Disable"
184178
}
185-
watchInfo = watchInfo + shortcutKeyColor("w") + navColor(isEnabled+" Watch")
186-
return openDDInfo + openDDUI + watchInfo
179+
items = append(items, shortcutKeyColor("w")+navColor(isEnabled+" Watch"))
180+
items = append(items, shortcutKeyColor("d")+navColor(" Detach"))
181+
182+
return strings.Join(items, " ")
187183
}
188184

189185
func (lk *LogKeyboard) clearNavigationMenu() {
@@ -290,6 +286,9 @@ func (lk *LogKeyboard) ToggleWatch(ctx context.Context, options api.UpOptions) {
290286

291287
func (lk *LogKeyboard) HandleKeyEvents(ctx context.Context, event keyboard.KeyEvent, project *types.Project, options api.UpOptions) {
292288
switch kRune := event.Rune; kRune {
289+
case 'd':
290+
lk.clearNavigationMenu()
291+
lk.Detach()
293292
case 'v':
294293
lk.openDockerDesktop(ctx, project)
295294
case 'w':
@@ -336,6 +335,10 @@ func (lk *LogKeyboard) EnableWatch(enabled bool, watcher Feature) {
336335
}
337336
}
338337

338+
func (lk *LogKeyboard) EnableDetach(detach func()) {
339+
lk.Detach = detach
340+
}
341+
339342
func allocateSpace(lines int) {
340343
for i := 0; i < lines; i++ {
341344
clearLine()

pkg/compose/attach.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -152,11 +152,12 @@ func (s *composeService) getContainerStreams(ctx context.Context, container stri
152152
var stdout io.ReadCloser
153153
var stdin io.WriteCloser
154154
cnx, err := s.apiClient().ContainerAttach(ctx, container, containerType.AttachOptions{
155-
Stream: true,
156-
Stdin: true,
157-
Stdout: true,
158-
Stderr: true,
159-
Logs: false,
155+
Stream: true,
156+
Stdin: true,
157+
Stdout: true,
158+
Stderr: true,
159+
Logs: false,
160+
DetachKeys: s.configFile().DetachKeys,
160161
})
161162
if err == nil {
162163
stdout = ContainerStdout{HijackedResponse: cnx}

pkg/compose/attach_service.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,13 @@ func (s *composeService) Attach(ctx context.Context, projectName string, options
3131
return err
3232
}
3333

34+
detachKeys := options.DetachKeys
35+
if detachKeys == "" {
36+
detachKeys = s.configFile().DetachKeys
37+
}
38+
3439
var attach container.AttachOptions
35-
attach.DetachKeys = options.DetachKeys
40+
attach.DetachKeys = detachKeys
3641
attach.NoStdin = options.NoStdin
3742
attach.Proxy = options.Proxy
3843
return container.RunAttach(ctx, s.dockerCli, target.ID, &attach)

pkg/compose/run.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ func (s *composeService) RunOneOffContainer(ctx context.Context, project *types.
4949
OpenStdin: !opts.Detach && opts.Interactive,
5050
Attach: !opts.Detach,
5151
Containers: []string{containerID},
52+
DetachKeys: s.configFile().DetachKeys,
5253
})
5354
var stErr cli.StatusError
5455
if errors.As(err, &stErr) {

pkg/compose/up.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,10 @@ func (s *composeService) Up(ctx context.Context, project *types.Project, options
107107
globalCtx, cancel := context.WithCancel(ctx)
108108
defer cancel()
109109

110+
if navigationMenu != nil {
111+
navigationMenu.EnableDetach(cancel)
112+
}
113+
110114
var (
111115
eg errgroup.Group
112116
mu sync.Mutex

pkg/compose/watch.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -597,6 +597,7 @@ func (s *composeService) exec(ctx context.Context, project *types.Project, servi
597597
exec.Privileged = x.Privileged
598598
exec.Command = x.Command
599599
exec.Workdir = x.WorkingDir
600+
exec.DetachKeys = s.configFile().DetachKeys
600601
for _, v := range x.Environment.ToMapping().Values() {
601602
err := exec.Env.Set(v)
602603
if err != nil {

0 commit comments

Comments
 (0)