Skip to content
This repository was archived by the owner on Apr 19, 2024. It is now read-only.
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions password.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"strings"

"github.com/AlecAivazis/survey/v2/core"
"github.com/AlecAivazis/survey/v2/terminal"
)

/*
Expand Down Expand Up @@ -48,7 +47,7 @@ func (p *Password) Prompt(config *PromptConfig) (interface{}, error) {
return "", err
}

if _, err := fmt.Fprint(terminal.NewAnsiStdout(p.Stdio().Out), userOut); err != nil {
if _, err := fmt.Fprint(p.Stdio().Out, userOut); err != nil {
return "", err
}

Expand Down
5 changes: 3 additions & 2 deletions renderer.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package survey
import (
"bytes"
"fmt"

"github.com/AlecAivazis/survey/v2/core"
"github.com/AlecAivazis/survey/v2/terminal"
"golang.org/x/term"
Expand Down Expand Up @@ -59,7 +60,7 @@ func (r *Renderer) Error(config *PromptConfig, invalid error) error {
}

// send the message to the user
if _, err := fmt.Fprint(terminal.NewAnsiStdout(r.stdio.Out), userOut); err != nil {
if _, err := fmt.Fprint(r.stdio.Out, userOut); err != nil {
return err
}

Expand Down Expand Up @@ -90,7 +91,7 @@ func (r *Renderer) Render(tmpl string, data interface{}) error {
}

// print the summary
if _, err := fmt.Fprint(terminal.NewAnsiStdout(r.stdio.Out), userOut); err != nil {
if _, err := fmt.Fprint(r.stdio.Out, userOut); err != nil {
return err
}

Expand Down
50 changes: 44 additions & 6 deletions survey.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,12 @@ import (

"github.com/AlecAivazis/survey/v2/core"
"github.com/AlecAivazis/survey/v2/terminal"
"github.com/mattn/go-colorable"
)

// DefaultAskOptions is the default options on ask, using the OS stdio.
func defaultAskOptions() *AskOptions {
return &AskOptions{
Stdio: terminal.Stdio{
In: os.Stdin,
Out: os.Stdout,
Err: os.Stderr,
},
PromptConfig: PromptConfig{
PageSize: 7,
HelpInput: "?",
Expand Down Expand Up @@ -154,7 +150,7 @@ type AskOptions struct {
func WithStdio(in terminal.FileReader, out terminal.FileWriter, err io.Writer) AskOpt {
return func(options *AskOptions) error {
options.Stdio.In = in
options.Stdio.Out = out
options.Stdio.Out = colorableOut(out)
options.Stdio.Err = err
return nil
}
Expand Down Expand Up @@ -323,6 +319,17 @@ func Ask(qs []*Question, response interface{}, opts ...AskOpt) error {
}
}

// fallback values in case [WithStdio] was not used
if options.Stdio.In == nil {
options.Stdio.In = os.Stdin
}
if options.Stdio.Out == nil {
options.Stdio.Out = colorableOut(os.Stdout)
}
if options.Stdio.Err == nil {
options.Stdio.Err = os.Stderr
}

// if we weren't passed a place to record the answers
if response == nil {
// we can't go any further
Expand Down Expand Up @@ -472,3 +479,34 @@ func computeCursorOffset(tmpl string, data IterableOpts, opts []core.OptionAnswe

return offset
}

// colorableOut transforms a file writer into one where it is safe to write ANSI escape codes to.
func colorableOut(w terminal.FileWriter) terminal.FileWriter {
if f, ok := w.(*os.File); ok {
out := colorable.NewColorable(f)
if f, ok := out.(*os.File); ok {
// Most cases will end up here: the writer is either
// 1. the original os.Stdout; or
// 2. the original os.Stdout with virtual terminal processing enabled on Windows.
return f
}
// If we have reached this point, the resulting writer is a Windows-specific writer that
// converts ANSI escape codes to Console API calls, and we need to wrap it in an extra
// type to preserve the original file descriptor.
return &writerWithFd{
Writer: out,
orig: f,
}
}
return w
}

// writerWithFd implements a [terminal.FileWriter]
type writerWithFd struct {
io.Writer
orig *os.File
}

func (w writerWithFd) Fd() uintptr {
return w.orig.Fd()
}
3 changes: 0 additions & 3 deletions terminal/cursor.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
//go:build !windows
// +build !windows

package terminal

import (
Expand Down
164 changes: 0 additions & 164 deletions terminal/cursor_windows.go

This file was deleted.

10 changes: 10 additions & 0 deletions terminal/display.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
package terminal

import (
"fmt"
"io"
)

type EraseLineMode int

const (
ERASE_LINE_END EraseLineMode = iota
ERASE_LINE_START
ERASE_LINE_ALL
)

func EraseLine(out io.Writer, mode EraseLineMode) error {
_, err := fmt.Fprintf(out, "\x1b[%dK", mode)
return err
}
13 changes: 0 additions & 13 deletions terminal/display_posix.go

This file was deleted.

31 changes: 0 additions & 31 deletions terminal/display_windows.go

This file was deleted.

10 changes: 6 additions & 4 deletions terminal/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,16 @@ import (
"io"
)

// NewAnsiStdout returns special stdout, which converts escape sequences to Windows API calls
// on Windows environment.
// NewAnsiStdout returns a writer connected to standard output that interprets ANSI escape codes to in a platform-agnostic way.
//
// Deprecated: use the mattn/go-colorable module instead of this method.
func NewAnsiStdout(out FileWriter) io.Writer {
return out
}

// NewAnsiStderr returns special stderr, which converts escape sequences to Windows API calls
// on Windows environment.
// NewAnsiStdout returns a writer connected to standard error that interprets ANSI escape codes to in a platform-agnostic way.
//
// Deprecated: use the mattn/go-colorable module instead of this method.
func NewAnsiStderr(out FileWriter) io.Writer {
return out
}
Loading