Skip to content

Commit

Permalink
TTP-332 add modified source code
Browse files Browse the repository at this point in the history
  • Loading branch information
chriszzt committed Dec 3, 2019
1 parent 8fa33db commit 1495d30
Show file tree
Hide file tree
Showing 10 changed files with 135 additions and 88 deletions.
20 changes: 0 additions & 20 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,6 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

## Unreleased

## [0.6.0] - 2019-11-29

### Added

- Support configurable stdin

### Fixed

- Correct the dep on go-i18n

## [0.5.0] - 2019-11-29

### Added

- Now building and testing on go 1.11, go 1.12, and go 1.13

### Removed

- Removed support for Go versions that don't include modules.

## [0.4.0] - 2019-02-19

### Added
Expand Down
12 changes: 12 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
HAS_GO_MOD=$(shell go help mod; echo $$?)
LINTERS=$(shell grep "// lint" tools.go | awk '{gsub(/\"/, "", $$1); print $$1}' | awk -F / '{print $$NF}') \
gofmt \
vet
Expand All @@ -24,12 +25,23 @@ $(foreach cmd_pkg,$(CMD_PKGS),$(eval $(call VENDOR_BIN_TMPL,$(cmd_pkg))))
$(patsubst %,%-bin,$(filter-out gofmt vet,$(LINTERS))): %-bin: vendor/bin/%
gofmt-bin vet-bin:

ifeq ($(HAS_GO_MOD),0)
bootstrap:

vendor: go.sum
GO111MODULE=on go mod vendor
else
bootstrap:
which dep || go get -u github.com/golang/dep/cmd/dep

vendor: Gopkg.lock
dep ensure -vendor-only
endif

mod-update:
GO111MODULE=on go get -u -m
GO111MODULE=on go mod tidy
dep ensure -update

mod-tidy:
GO111MODULE=on go mod tidy
Expand Down
2 changes: 2 additions & 0 deletions codes.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ var ResetCode = fmt.Sprintf("%s%dm", esc, reset)
const (
hideCursor = esc + "?25l"
showCursor = esc + "?25h"
noLineWrap = esc + "\x1b[?7l"
doLineWrap = esc + "\x1b[?7h"
clearLine = esc + "2K"
)

Expand Down
15 changes: 13 additions & 2 deletions cursor.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,16 @@ import "fmt"
type Pointer func(to []rune) []rune

func defaultCursor(ignored []rune) []rune {
return []rune("\u2588")
return []rune("\u2590")
}

func blockCursor(input []rune) []rune {
return []rune(fmt.Sprintf("\\e[7m%s\\e[0m", string(input)))
}

func pipeCursor(input []rune) []rune {
marker := []rune("|")
// change from "|" to be right half block(U+2590) to make it more explicit
marker := []rune("\u2590")
out := []rune{}
out = append(out, marker...)
out = append(out, input...)
Expand Down Expand Up @@ -128,6 +129,15 @@ func (c *Cursor) FormatMask(mask rune) string {
return format(r, c)
}

// FormatMask replaces all input runes with the mask rune.
func (c *Cursor) FormatMaskNoCursor(mask rune) string {
r := make([]rune, len(c.input))
for i := range r {
r[i] = mask
}
return string(r)
}

// Update inserts newinput into the input []rune in the appropriate place.
// The cursor is moved to the end of the inputed sequence.
func (c *Cursor) Update(newinput string) {
Expand Down Expand Up @@ -194,6 +204,7 @@ func (c *Cursor) Listen(line []rune, pos int, key rune) ([]rune, int, bool) {
switch key {
case 0: // empty
case KeyEnter:
// keyEnter means this session ends, we should return false and let outside to check user input
return []rune(c.Get()), c.Position, false
case KeyBackspace:
if c.erase {
Expand Down
23 changes: 12 additions & 11 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,31 +1,32 @@
module github.com/manifoldco/promptui

go 1.11

// Do not change the versions of kingpin or go-i18n;
// we will remove them when we stop using gometalinter

require (
github.com/BurntSushi/toml v0.3.1 // indirect
github.com/alecthomas/gometalinter v3.0.0+incompatible
github.com/alecthomas/units v0.0.0-20190924025748-f65c72e2690d // indirect
github.com/alecthomas/gometalinter v2.0.11+incompatible
github.com/chzyer/logex v1.1.10 // indirect
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e
github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1 // indirect
github.com/client9/misspell v0.3.4
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/golang/lint v0.0.0-20181026193005-c67002cb31c3
github.com/google/shlex v0.0.0-20181106134648-c34317bd91bf // indirect
github.com/gordonklaus/ineffassign v0.0.0-20180909121442-1003c8bd00dc
github.com/juju/ansiterm v0.0.0-20180109212912-720a0952cc2a
github.com/kr/pretty v0.1.0 // indirect
github.com/lunixbochs/vtclean v0.0.0-20180621232353-2d01aacdc34a // indirect
github.com/mattn/go-colorable v0.0.9 // indirect
github.com/mattn/go-isatty v0.0.4 // indirect
github.com/nicksnyder/go-i18n v1.10.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/stretchr/testify v1.2.2 // indirect
github.com/tsenart/deadcode v0.0.0-20160724212837-210d2dc333e9
golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3
golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3 // indirect
golang.org/x/sys v0.0.0-20181122145206-62eef0e2fa9b // indirect
golang.org/x/tools v0.0.0-20181122213734-04b5d21e00f1 // indirect
gopkg.in/alecthomas/kingpin.v3-unstable v3.0.0-20171010053543-63abe20a23e2 // indirect
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 // indirect
)

// This version of kingpin is incompatible with the released version of
// gometalinter until the next release of gometalinter, and possibly until it
// has go module support, we'll need this exclude, and perhaps some more.
//
// After that point, we should be able to remove it.
exclude gopkg.in/alecthomas/kingpin.v3-unstable v3.0.0-20180810215634-df19058c872c
27 changes: 6 additions & 21 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/alecthomas/gometalinter v3.0.0+incompatible h1:e9Zfvfytsw/e6Kd/PYd75wggK+/kX5Xn8IYDUKyc5fU=
github.com/alecthomas/gometalinter v3.0.0+incompatible/go.mod h1:qfIpQGGz3d+NmgyPBqv+LSh50emm1pt72EtcX2vKYQk=
github.com/alecthomas/units v0.0.0-20190924025748-f65c72e2690d h1:UQZhZ2O0vMHr2cI+DC1Mbh0TJxzA3RcLoMsFw+aXw7E=
github.com/alecthomas/units v0.0.0-20190924025748-f65c72e2690d/go.mod h1:rBZYJk541a8SKzHPHnH3zbiI+7dagKZ0cgpgrD7Fyho=
github.com/alecthomas/gometalinter v2.0.11+incompatible h1:toROE7pXPU/pUB4lh6ICqUKwpDtmkRCyJIr1nYqmKp0=
github.com/alecthomas/gometalinter v2.0.11+incompatible/go.mod h1:qfIpQGGz3d+NmgyPBqv+LSh50emm1pt72EtcX2vKYQk=
github.com/chzyer/logex v1.1.10 h1:Swpa1K6QvQznwJRcfTfQJmTE72DqScAa40E+fbHEXEE=
github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI=
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e h1:fY5BOSpyZCqRo5OhCuC+XN+r/bBCmeuuJtjz+bCNIf8=
Expand All @@ -12,10 +8,10 @@ github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1 h1:q763qf9huN11kDQavWs
github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMnBNeIyt5eFwwo7qiLfzFZmjNmxjkiQlU=
github.com/client9/misspell v0.3.4 h1:ta993UF76GwbvJcIo3Y68y/M3WxlpEHPWIGDkJYwzJI=
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/golang/lint v0.0.0-20181026193005-c67002cb31c3 h1:I4BOK3PBMjhWfQM2zPJKK7lOBGsrsvOB7kBELP33hiE=
github.com/golang/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:tluoj9z5200jBnyusfRPU2LqT6J+DAorxEvtC7LHB+E=
github.com/google/shlex v0.0.0-20181106134648-c34317bd91bf h1:7+FW5aGwISbqUtkfmIpZJGRgNFg2ioYPvFaUxdqpDsg=
github.com/google/shlex v0.0.0-20181106134648-c34317bd91bf/go.mod h1:RpwtwJQFrIEPstU94h88MWPXP2ektJZ8cZ0YntAmXiE=
github.com/gordonklaus/ineffassign v0.0.0-20180909121442-1003c8bd00dc h1:cJlkeAx1QYgO5N80aF5xRGstVsRQwgLR7uA2FnP1ZjY=
Expand All @@ -33,15 +29,10 @@ github.com/mattn/go-colorable v0.0.9 h1:UVL0vNpWh04HeJXV0KLcaT7r06gOH2l4OW6ddYRU
github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU=
github.com/mattn/go-isatty v0.0.4 h1:bnP0vzxcAdeI1zdubAl5PjU6zsERjGZb7raWodagDYs=
github.com/mattn/go-isatty v0.0.4/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4=
github.com/nicksnyder/go-i18n v1.10.1 h1:isfg77E/aCD7+0lD/D00ebR2MV5vgeQ276WYyDaCRQc=
github.com/nicksnyder/go-i18n v1.10.1/go.mod h1:e4Di5xjP9oTVrC6y3C7C0HoSYXjSbhh/dU0eUV32nB4=
github.com/pelletier/go-toml v1.2.0 h1:T5zMGML61Wp+FlcbWjRDT7yAxhJNAiPPLOFECq181zc=
github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/tsenart/deadcode v0.0.0-20160724212837-210d2dc333e9 h1:vY5WqiEon0ZSTGM3ayVVi+twaHKHDFUVloaQ/wug9/c=
github.com/tsenart/deadcode v0.0.0-20160724212837-210d2dc333e9/go.mod h1:q+QjxYvZ+fpjMXqs+XEriussHjSYqeXVnAdSV1tkMYk=
golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3 h1:x/bBzNauLQAlE3fLku/xy92Y8QwKX5HZymrMz2IiKFc=
Expand All @@ -50,11 +41,5 @@ golang.org/x/sys v0.0.0-20181122145206-62eef0e2fa9b h1:MQE+LT/ABUuuvEZ+YQAMSXind
golang.org/x/sys v0.0.0-20181122145206-62eef0e2fa9b/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/tools v0.0.0-20181122213734-04b5d21e00f1 h1:bsEj/LXbv3BCtkp/rBj9Wi/0Nde4OMaraIZpndHAhdI=
golang.org/x/tools v0.0.0-20181122213734-04b5d21e00f1/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
gopkg.in/alecthomas/kingpin.v3-unstable v3.0.0-20171010053543-63abe20a23e2 h1:5zOHKFi4LqGWG+3d+isqpbPrN/2yhDJnlO+BhRiuR6U=
gopkg.in/alecthomas/kingpin.v3-unstable v3.0.0-20171010053543-63abe20a23e2/go.mod h1:3HH7i1SgMqlzxCcBmUHW657sD4Kvv9sC3HpL3YukzwA=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
49 changes: 32 additions & 17 deletions prompt.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,13 @@ import (
"text/template"

"github.com/chzyer/readline"
"github.com/manifoldco/promptui/screenbuf"

"tigergraph.com/thirdparty/promptui/screenbuf"
)

const default_prompt_placehold_size = 4
const default_error_prompt_size = 3

// Prompt represents a single line text field input with options for validation and input masks.
type Prompt struct {
// Label is the value displayed on the command line prompt.
Expand Down Expand Up @@ -111,7 +115,12 @@ type PromptTemplates struct {
// value. It will return the value and an error if any occurred during the prompt's execution.
func (p *Prompt) Run() (string, error) {
var err error

//label should be only string
labelStr, ok := p.Label.(string)
if !ok {
return "", ErrAbort
}
promptLen := default_prompt_placehold_size + len(labelStr)
err = p.prepareTemplates()
if err != nil {
return "", err
Expand All @@ -138,6 +147,7 @@ func (p *Prompt) Run() (string, error) {
}
// we're taking over the cursor, so stop showing it.
rl.Write([]byte(hideCursor))

sb := screenbuf.New(rl)

validFn := func(x string) error {
Expand All @@ -154,9 +164,13 @@ func (p *Prompt) Run() (string, error) {
}
eraseDefault := input != "" && !p.AllowEdit
cur := NewCursor(input, p.Pointer, eraseDefault)

cursorLen := len(cur.Cursor([]rune{}))
listen := func(input []rune, pos int, key rune) ([]rune, int, bool) {
_, _, keepOn := cur.Listen(input, pos, key)
// if keepOn is false, we should return immediately
if !keepOn {
return nil, 0, keepOn
}
err := validFn(cur.Get())
var prompt []byte

Expand All @@ -173,16 +187,20 @@ func (p *Prompt) Run() (string, error) {
if p.Mask != 0 {
echo = cur.FormatMask(p.Mask)
}

prompt = append(prompt, []byte(echo)...)

sb.Reset()
sb.Write(prompt)
// some cursor might have extra rune in the end of line
sb.WriteLineWrap(prompt, len([]rune(cur.input))+promptLen+cursorLen)

if inputErr != nil {
len := default_error_prompt_size + len(inputErr.Error())
validation := render(p.Templates.validation, inputErr)
sb.Write(validation)

sb.WriteLineWrap(validation, len)
inputErr = nil
}
sb.Flush()
sb.FlushLineWrap()
return nil, 0, keepOn
}

Expand All @@ -191,15 +209,14 @@ func (p *Prompt) Run() (string, error) {
for {
_, err = rl.Readline()
inputErr = validFn(cur.Get())

if inputErr == nil {
break
}

if err != nil {
break
}
}

if err != nil {
switch err {
case readline.ErrInterrupt:
Expand All @@ -211,16 +228,16 @@ func (p *Prompt) Run() (string, error) {
err = ErrInterrupt
}
sb.Reset()
sb.WriteString("")
sb.Flush()
rl.Write([]byte(showCursor))
rl.Close()

return "", err
}

echo := cur.Format()
// this is when input value is correct, we don't show cursor
echo := string(cur.input)
if p.Mask != 0 {
echo = cur.FormatMask(p.Mask)
echo = cur.FormatMaskNoCursor(p.Mask)
}

prompt := render(p.Templates.success, p.Label)
Expand All @@ -233,13 +250,11 @@ func (p *Prompt) Run() (string, error) {
err = ErrAbort
}
}

sb.Reset()
sb.Write(prompt)
sb.Flush()
sb.WriteLineWrap(prompt, len([]rune(cur.input))+promptLen)
sb.FlushLineWrap()
rl.Write([]byte(showCursor))
rl.Close()

return cur.Get(), err
}

Expand Down
Loading

0 comments on commit 1495d30

Please sign in to comment.