summaryrefslogtreecommitdiff
path: root/pkg/cli
diff options
context:
space:
mode:
Diffstat (limited to 'pkg/cli')
-rw-r--r--pkg/cli/command.go1
-rw-r--r--pkg/cli/interface.go11
-rw-r--r--pkg/cli/query.go12
3 files changed, 19 insertions, 5 deletions
diff --git a/pkg/cli/command.go b/pkg/cli/command.go
index d07a120..a9938d1 100644
--- a/pkg/cli/command.go
+++ b/pkg/cli/command.go
@@ -54,6 +54,7 @@ func (e commandError) Error() string {
//suffices for all regular operation.
type CommandRunner func(c Command, stdin io.Reader, stdout, stderr io.Writer) error
+//DefaultCommandRunner is a CommandRunner that actually executes the command.
func DefaultCommandRunner(c Command, stdin io.Reader, stdout, stderr io.Writer) error {
cmd := exec.Command(c.Program[0], c.Program[1:]...)
cmd.Stdin = stdin
diff --git a/pkg/cli/interface.go b/pkg/cli/interface.go
index 0f1ba74..4d0e196 100644
--- a/pkg/cli/interface.go
+++ b/pkg/cli/interface.go
@@ -57,7 +57,6 @@ func SetupInterface(stdin io.Reader, stdout, stderr io.Writer, commandRunner Com
//Implementation wraps access to the CLI, including input, output and subprocesses.
type Implementation struct {
- //TODO: flag isStdinTerminal that disables color output and swaps out the TUI instance
stdin io.Reader
stdout io.Writer
stderr io.Writer
@@ -83,6 +82,10 @@ type TUI interface {
//Query displays a question and a set of answers and allows the user to select
//one of the answers. Returns the Return attribute of the selected Choice.
Query(prompt string, choices ...Choice) (string, error)
+ //Print writes the given string (potentially including ANSI escape codes) to
+ //the given writer. At this point, it can be decided whether to strip out the
+ //ANSI escape codes.
+ Print(w io.Writer, msg string)
}
func (i *Implementation) safeStdout() io.Writer {
@@ -145,17 +148,17 @@ func (i *Implementation) ShowResultsSorted(strs []string) {
//ShowProgress displays a progress message on stderr.
func (i *Implementation) ShowProgress(str string) {
- fmt.Fprintf(i.stderr, "\x1B[0;1;36m>>\x1B[0;36m %s\x1B[0m", strings.TrimSpace(str))
+ i.tui.Print(i.stderr, fmt.Sprintf("\x1B[0;1;36m>>\x1B[0;36m %s\x1B[0m", strings.TrimSpace(str)))
}
//ShowWarning displays a warning message on stderr.
func (i *Implementation) ShowWarning(str string) {
- fmt.Fprintf(i.stderr, "\x1B[0;1;33m!!\x1B[0;36m %s\x1B[0m", strings.TrimSpace(str))
+ i.tui.Print(i.stderr, fmt.Sprintf("\x1B[0;1;33m!!\x1B[0;36m %s\x1B[0m", strings.TrimSpace(str)))
}
//ShowError displays an error message on stderr.
func (i *Implementation) ShowError(str string) {
- fmt.Fprintf(i.stderr, "\x1B[0;1;31m!!\x1B[0;36m %s\x1B[0m", strings.TrimSpace(str))
+ i.tui.Print(i.stderr, fmt.Sprintf("\x1B[0;1;31m!!\x1B[0;36m %s\x1B[0m", strings.TrimSpace(str)))
}
//ShowUsage displays a usage synopsis on stderr.
diff --git a/pkg/cli/query.go b/pkg/cli/query.go
index d9aa9a2..aa8fa50 100644
--- a/pkg/cli/query.go
+++ b/pkg/cli/query.go
@@ -44,6 +44,10 @@ type terminalTUI struct {
i *Implementation
}
+func (t terminalTUI) Print(w io.Writer, msg string) {
+ w.Write([]byte(msg))
+}
+
func (t terminalTUI) ReadLine(prompt string) (string, error) {
if prompt != "" {
t.i.safeStdout().Write([]byte(strings.TrimSpace(prompt) + " "))
@@ -225,12 +229,18 @@ func (b *buffer) getNextInput() []byte {
}
////////////////////////////////////////////////////////////////////////////////
-// TUI implementation for when stdin is a pipe
+// TUI implementation for when stdin is a pipe (also used in unit tests)
type pipeTUI struct {
i *Implementation
}
+var ansiColorCodeRx = regexp.MustCompile("\x1B" + `\[[0-9;]*m`)
+
+func (t *pipeTUI) Print(w io.Writer, msg string) {
+ w.Write([]byte(ansiColorCodeRx.ReplaceAllString(msg, "")))
+}
+
func (t *pipeTUI) ReadLine(prompt string) (string, error) {
str, err := t.i.stdinBuf.ReadString('\n')
str = strings.TrimSpace(str)