Skip to content

Commit

Permalink
Windows support (#112)
Browse files Browse the repository at this point in the history
  • Loading branch information
ktr0731 committed Oct 26, 2018
1 parent c7eaf07 commit d9c7c9a
Show file tree
Hide file tree
Showing 10 changed files with 68 additions and 38 deletions.
1 change: 1 addition & 0 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -132,3 +132,7 @@
[prune]
go-tests = true
unused-packages = true

[[constraint]]
name = "github.com/mattn/go-colorable"
version = "0.0.9"
2 changes: 1 addition & 1 deletion adapter/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,5 +74,5 @@ func Run(cfg *config.Config, ui cui.UI, file, call string) error {

// IsCLIMode returns whether Evans was launched as CLI mode or not.
func IsCLIMode(file string) bool {
return !isatty.IsTerminal(os.Stdin.Fd()) || file != ""
return (!isatty.IsTerminal(os.Stdin.Fd()) && !isatty.IsCygwinTerminal(os.Stdin.Fd())) || file != ""
}
49 changes: 28 additions & 21 deletions adapter/cui/cui.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@ import (
"os"

"github.com/fatih/color"
colorable "github.com/mattn/go-colorable"
)

// UI provides formatted I/O interfaces.
// It is used from Evans's standard I/O and CLI mode I/O.
type UI interface {
Println(a interface{})
InfoPrintln(a interface{})
Expand All @@ -17,69 +20,73 @@ type UI interface {
ErrWriter() io.Writer
}

type BaseUI struct {
type basicUI struct {
reader io.Reader
writer, errWriter io.Writer
}

func NewUI(r io.Reader, w, ew io.Writer) UI {
return &BaseUI{
// New creates a new UI with passed io.Reader, io.Writers.
// In normal case, you can use NewBasic instead of New.
func New(r io.Reader, w, ew io.Writer) UI {
return &basicUI{
reader: r,
writer: w,
errWriter: ew,
}
}

func NewBasicUI() *BaseUI {
return &BaseUI{
// NewBasic creates a new UI with stdin, stdout, stderr.
func NewBasic() UI {
return &basicUI{
reader: os.Stdin,
writer: os.Stdout,
errWriter: os.Stderr,
writer: colorable.NewColorableStdout(),
errWriter: colorable.NewColorableStderr(),
}
}

func (u *BaseUI) fprintln(w io.Writer, a interface{}) {
func (u *basicUI) fprintln(w io.Writer, a interface{}) {
if i, ok := a.(io.Reader); ok {
io.Copy(u.writer, i)
} else {
fmt.Fprintln(w, a)
}
}

func (u *BaseUI) Println(a interface{}) {
func (u *basicUI) Println(a interface{}) {
u.fprintln(u.writer, a)
}

func (u *BaseUI) InfoPrintln(a interface{}) {
func (u *basicUI) InfoPrintln(a interface{}) {
u.fprintln(u.writer, a)
}

func (u *BaseUI) ErrPrintln(a interface{}) {
func (u *basicUI) ErrPrintln(a interface{}) {
u.fprintln(u.errWriter, a)
}

func (u *BaseUI) Writer() io.Writer {
func (u *basicUI) Writer() io.Writer {
return u.writer
}

func (u *BaseUI) ErrWriter() io.Writer {
func (u *basicUI) ErrWriter() io.Writer {
return u.errWriter
}

type ColoredUI struct {
type coloredUI struct {
UI
}

// NewColored wraps provided `ui` with ColoredUI.
// If `ui` is *ColoredUI, NewColored returns it as it is.
// NewColored wraps provided `ui` with coloredUI.
// If `ui` is *coloredUI, NewColored returns it as it is.
// Colored output works fine in Windows environment.
func NewColored(ui UI) UI {
if ui, ok := ui.(*ColoredUI); ok {
if ui, ok := ui.(*coloredUI); ok {
return ui
}
return &ColoredUI{ui}
return &coloredUI{ui}
}

func (u *ColoredUI) printWithColor(
func (u *coloredUI) printWithColor(
w func(a interface{}),
color func(format string, a ...interface{}) string,
a interface{},
Expand All @@ -94,10 +101,10 @@ func (u *ColoredUI) printWithColor(
}
}

func (u *ColoredUI) InfoPrintln(a interface{}) {
func (u *coloredUI) InfoPrintln(a interface{}) {
u.printWithColor(u.UI.InfoPrintln, color.BlueString, a)
}

func (u *ColoredUI) ErrPrintln(a interface{}) {
func (u *coloredUI) ErrPrintln(a interface{}) {
u.printWithColor(u.UI.ErrPrintln, color.RedString, a)
}
12 changes: 0 additions & 12 deletions adapter/repl/repl.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,18 +203,6 @@ func (r *repl) getPrompt() string {
return p
}

const defaultSplashText = `
______
| ____|
| |__ __ __ __ _ _ __ ___
| __| \ \ / / / _. | | '_ \ / __|
| |____ \ V / | (_| | | | | | \__ \
|______| \_/ \__,_| |_| |_| |___/
more expressive universal gRPC client
`

func (r *repl) printSplash(p string) {
if p == "" {
r.ui.Println(defaultSplashText)
Expand Down
15 changes: 15 additions & 0 deletions adapter/repl/splash_others.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// +build !windows

package repl

const defaultSplashText = `
______
| ____|
| |__ __ __ __ _ _ __ ___
| __| \ \ / / / _. | | '_ \ / __|
| |____ \ V / | (_| | | | | | \__ \
|______| \_/ \__,_| |_| |_| |___/
more expressive universal gRPC client
`
15 changes: 15 additions & 0 deletions adapter/repl/splash_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// +build windows

package repl

const defaultSplashText = `
______
| ____|
| |__ __ __ __ _ _ __ ___
| __| ' ' / / / _. | | '_ ' / __|
| |____ ' V / | (_| | | | | | '__ ,
|______| '_/ '__,_| |_| |_| |___/
more expressive universal gRPC client
`
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ func main() {
os.Exit(cmd.New(
meta.AppName,
meta.Version.String(),
cui.NewBasicUI(),
cui.NewBasic(),
).Run(os.Args[1:]))
}
4 changes: 2 additions & 2 deletions tests/e2e/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func TestCLI(t *testing.T) {

out := new(bytes.Buffer)
errOut := new(bytes.Buffer)
ui := cui.NewUI(in, out, errOut)
ui := cui.New(in, out, errOut)

args := strings.Split(c.args, " ")
args = append([]string{"--cli"}, args...)
Expand Down Expand Up @@ -114,7 +114,7 @@ func TestCLI(t *testing.T) {
cli.DefaultReader = in

out := new(bytes.Buffer)
ui := cui.NewUI(in, out, ioutil.Discard)
ui := cui.New(in, out, ioutil.Discard)

code := newCommand(ui).Run(strings.Split(c.args, " "))
require.Equal(t, c.code, code)
Expand Down
2 changes: 1 addition & 1 deletion tests/e2e/repl_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func (h *replHelper) run(args []string) int {
h.ew = os.Stderr
}

ui := cui.NewUI(h.r, h.w, h.ew)
ui := cui.New(h.r, h.w, h.ew)
cli := newCommand(ui)

h.iq = append(h.iq, repl.Exit()...)
Expand Down

0 comments on commit d9c7c9a

Please sign in to comment.