aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/cli/internal/ui/term
diff options
context:
space:
mode:
Diffstat (limited to 'cli/internal/ui/term')
-rw-r--r--cli/internal/ui/term/cursor.go73
-rw-r--r--cli/internal/ui/term/cursor_test.go43
2 files changed, 116 insertions, 0 deletions
diff --git a/cli/internal/ui/term/cursor.go b/cli/internal/ui/term/cursor.go
new file mode 100644
index 0000000..253f043
--- /dev/null
+++ b/cli/internal/ui/term/cursor.go
@@ -0,0 +1,73 @@
+// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
+// SPDX-License-Identifier: Apache-2.0
+
+// Package cursor provides functionality to interact with the terminal cursor.
+package cursor
+
+import (
+ "io"
+ "os"
+
+ "github.com/AlecAivazis/survey/v2/terminal"
+)
+
+type cursor interface {
+ Up(n int) error
+ Down(n int) error
+ Hide() error
+ Show() error
+}
+
+// fakeFileWriter is a terminal.FileWriter.
+// If the underlying writer w does not implement Fd() then a dummy value is returned.
+type fakeFileWriter struct {
+ w io.Writer
+}
+
+// Write delegates to the internal writer.
+func (w *fakeFileWriter) Write(p []byte) (int, error) {
+ return w.w.Write(p)
+}
+
+// Fd is required to be implemented to satisfy the terminal.FileWriter interface.
+// If the underlying writer is a file, like os.Stdout, then invoke it. Otherwise, this method allows us to create
+// a Cursor that can write to any io.Writer like a bytes.Buffer by returning a dummy value.
+func (w *fakeFileWriter) Fd() uintptr {
+ if v, ok := w.w.(terminal.FileWriter); ok {
+ return v.Fd()
+ }
+ return 0
+}
+
+// Cursor represents the terminal's cursor.
+type Cursor struct {
+ c cursor
+}
+
+// New creates a new cursor that writes to stderr.
+func New() *Cursor {
+ return &Cursor{
+ c: &terminal.Cursor{
+ Out: os.Stderr,
+ },
+ }
+}
+
+// EraseLine erases a line from a FileWriter.
+func EraseLine(fw terminal.FileWriter) {
+ terminal.EraseLine(fw, terminal.ERASE_LINE_ALL)
+}
+
+// EraseLinesAbove erases a line and moves the cursor up from fw, repeated n times.
+func EraseLinesAbove(fw terminal.FileWriter, n int) {
+ c := Cursor{
+ c: &terminal.Cursor{
+ Out: fw,
+ },
+ }
+ for i := 0; i < n; i += 1 {
+ EraseLine(fw)
+ c.c.Up(1)
+ }
+ EraseLine(fw) // Erase the nth line as well.
+}
diff --git a/cli/internal/ui/term/cursor_test.go b/cli/internal/ui/term/cursor_test.go
new file mode 100644
index 0000000..270ebe8
--- /dev/null
+++ b/cli/internal/ui/term/cursor_test.go
@@ -0,0 +1,43 @@
+//go:build !windows
+// +build !windows
+
+// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
+// SPDX-License-Identifier: Apache-2.0
+package cursor
+
+import (
+ "io"
+ "strings"
+ "testing"
+
+ "github.com/AlecAivazis/survey/v2/terminal"
+ "github.com/stretchr/testify/require"
+)
+
+func TestEraseLine(t *testing.T) {
+ testCases := map[string]struct {
+ inWriter func(writer io.Writer) terminal.FileWriter
+ shouldErase bool
+ }{
+ "should erase a line if the writer is a file": {
+ inWriter: func(writer io.Writer) terminal.FileWriter {
+ return &fakeFileWriter{w: writer}
+ },
+ shouldErase: true,
+ },
+ }
+
+ for name, tc := range testCases {
+ t.Run(name, func(t *testing.T) {
+ // GIVEN
+ buf := new(strings.Builder)
+
+ // WHEN
+ EraseLine(tc.inWriter(buf))
+
+ // THEN
+ isErased := buf.String() != ""
+ require.Equal(t, tc.shouldErase, isErased)
+ })
+ }
+}