aboutsummaryrefslogtreecommitdiffstats
path: root/commands/execenv
diff options
context:
space:
mode:
Diffstat (limited to 'commands/execenv')
-rw-r--r--commands/execenv/env.go13
-rw-r--r--commands/execenv/env_testing.go12
2 files changed, 21 insertions, 4 deletions
diff --git a/commands/execenv/env.go b/commands/execenv/env.go
index 46de8401..e693807e 100644
--- a/commands/execenv/env.go
+++ b/commands/execenv/env.go
@@ -7,6 +7,7 @@ import (
"os"
"github.com/mattn/go-isatty"
+ "golang.org/x/term"
"github.com/MichaelMure/git-bug/cache"
"github.com/MichaelMure/git-bug/repository"
@@ -57,6 +58,8 @@ type Out interface {
// IsTerminal tells if the output is a user terminal (rather than a buffer,
// a pipe ...), which tells if we can use colors and other interactive features.
IsTerminal() bool
+ // Width return the width of the attached terminal, or a good enough value.
+ Width() int
// Raw return the underlying io.Writer, or itself if not.
// This is useful if something need to access the raw file descriptor.
@@ -123,6 +126,16 @@ func (o out) IsTerminal() bool {
return false
}
+func (o out) Width() int {
+ if f, ok := o.Raw().(*os.File); ok {
+ width, _, err := term.GetSize(int(f.Fd()))
+ if err == nil {
+ return width
+ }
+ }
+ return 80
+}
+
func (o out) Raw() io.Writer {
return o.Writer
}
diff --git a/commands/execenv/env_testing.go b/commands/execenv/env_testing.go
index 03fe0430..15d7b646 100644
--- a/commands/execenv/env_testing.go
+++ b/commands/execenv/env_testing.go
@@ -20,14 +20,14 @@ type TestIn struct {
forceIsTerminal bool
}
-func (t *TestIn) ForceIsTerminal(value bool) {
- t.forceIsTerminal = value
-}
-
func (t *TestIn) IsTerminal() bool {
return t.forceIsTerminal
}
+func (t *TestIn) ForceIsTerminal(value bool) {
+ t.forceIsTerminal = value
+}
+
var _ Out = &TestOut{}
type TestOut struct {
@@ -60,6 +60,10 @@ func (te *TestOut) IsTerminal() bool {
return te.forceIsTerminal
}
+func (te *TestOut) Width() int {
+ return 80
+}
+
func (te *TestOut) Raw() io.Writer {
return te.Buffer
}