Skip to content

Commit 011e97d

Browse files
author
gentele
committed
trim loading text for small terminals
1 parent ee4e376 commit 011e97d

3 files changed

Lines changed: 55 additions & 28 deletions

File tree

pkg/devspace/kubectl/client.go

Lines changed: 2 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package kubectl
22

33
import (
4+
"github.com/covexo/devspace/pkg/util/terminal"
45
"bytes"
56
"errors"
67
"fmt"
@@ -16,7 +17,6 @@ import (
1617
"github.com/covexo/devspace/pkg/devspace/config/configutil"
1718
"github.com/covexo/devspace/pkg/devspace/config/v1"
1819
"github.com/covexo/devspace/pkg/util/log"
19-
dockerterm "github.com/docker/docker/pkg/term"
2020
k8sv1 "k8s.io/api/core/v1"
2121
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2222
"k8s.io/client-go/kubernetes"
@@ -413,7 +413,7 @@ func Exec(kubectlClient *kubernetes.Clientset, pod *k8sv1.Pod, container string,
413413
SubResource("exec")
414414

415415
if tty {
416-
t = setupTTY()
416+
t = terminal.SetupTTY()
417417
}
418418

419419
execRequest.VersionedParams(&k8sapi.PodExecOptions{
@@ -475,30 +475,6 @@ func Exec(kubectlClient *kubernetes.Clientset, pod *k8sv1.Pod, container string,
475475
return stdinWriter, stdoutReader, stderrReader, nil
476476
}
477477

478-
func setupTTY() term.TTY {
479-
t := term.TTY{
480-
Out: os.Stdout,
481-
In: os.Stdin,
482-
}
483-
484-
if !t.IsTerminalIn() {
485-
log.Info("Unable to use a TTY - input is not a terminal or the right kind of file")
486-
487-
return t
488-
}
489-
490-
// if we get to here, the user wants to attach stdin, wants a TTY, and In is a terminal, so we
491-
// can safely set t.Raw to true
492-
t.Raw = true
493-
494-
stdin, stdout, _ := dockerterm.StdStreams()
495-
496-
t.In = stdin
497-
t.Out = stdout
498-
499-
return t
500-
}
501-
502478
//ExecBuffered executes a command for kubernetes and returns the output and error buffers
503479
func ExecBuffered(kubectlClient *kubernetes.Clientset, pod *k8sv1.Pod, container string, command []string) ([]byte, []byte, error) {
504480
_, stdout, stderr, execErr := Exec(kubectlClient, pod, container, command, false, nil)

pkg/util/log/loading_text.go

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,15 @@ import (
55
"io"
66
"time"
77

8+
"github.com/covexo/devspace/pkg/util/terminal"
9+
810
"github.com/daviddengcn/go-colortext"
911
)
1012

1113
const waitInterval = time.Millisecond * 150
1214

15+
var tty = terminal.SetupTTY()
16+
1317
type loadingText struct {
1418
Stream io.Writer
1519
Message string
@@ -71,13 +75,30 @@ func (l *loadingText) render() {
7175
} else {
7276
l.Stream.Write([]byte("\r"))
7377
}
78+
messagePrefix := []byte("[WAIT] ")
7479

7580
ct.Foreground(ct.Red, false)
76-
l.Stream.Write([]byte("[WAIT] "))
81+
l.Stream.Write(messagePrefix)
7782
ct.ResetColor()
7883

7984
timeElapsed := fmt.Sprintf("%d", (time.Now().UnixNano()-l.startTimestamp)/int64(time.Second))
80-
l.Stream.Write([]byte(l.getLoadingChar() + " " + l.Message + " (" + timeElapsed + "s)"))
85+
message := []byte(l.getLoadingChar() + " " + l.Message)
86+
messageSuffix := " (" + timeElapsed + "s)"
87+
terminalSize := tty.GetSize()
88+
prefixLength := len(messagePrefix)
89+
suffixLength := len(messageSuffix)
90+
91+
if uint16(prefixLength+len(message)+suffixLength) > terminalSize.Width {
92+
dots := []byte("...")
93+
maxMessageLength := terminalSize.Width - uint16(prefixLength+suffixLength+len(dots))
94+
95+
if maxMessageLength > 0 {
96+
message = append(message[:maxMessageLength], dots...)
97+
}
98+
}
99+
message = append(message, messageSuffix...)
100+
101+
l.Stream.Write(message)
81102
}
82103

83104
func (l *loadingText) Stop() {

pkg/util/terminal/tty.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package terminal
2+
3+
import (
4+
"os"
5+
6+
dockerterm "github.com/docker/docker/pkg/term"
7+
"k8s.io/kubernetes/pkg/kubectl/util/term"
8+
)
9+
10+
func SetupTTY() term.TTY {
11+
t := term.TTY{
12+
Out: os.Stdout,
13+
In: os.Stdin,
14+
}
15+
16+
if !t.IsTerminalIn() {
17+
return t
18+
}
19+
20+
// if we get to here, the user wants to attach stdin, wants a TTY, and In is a terminal, so we
21+
// can safely set t.Raw to true
22+
t.Raw = true
23+
24+
stdin, stdout, _ := dockerterm.StdStreams()
25+
26+
t.In = stdin
27+
t.Out = stdout
28+
29+
return t
30+
}

0 commit comments

Comments
 (0)