Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Prompt type form string to fmt.Stringer. #156

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
8 changes: 4 additions & 4 deletions example/readline-demo/readline-demo.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func filterInput(r rune) (rune, bool) {

func main() {
l, err := readline.NewEx(&readline.Config{
Prompt: "\033[31m»\033[0m ",
Prompt: readline.StaticPrompt("\033[31m»\033[0m "),
HistoryFile: "/tmp/readline.tmp",
AutoComplete: completer,
InterruptPrompt: "^C",
Expand All @@ -88,7 +88,7 @@ func main() {

setPasswordCfg := l.GenPasswordConfig()
setPasswordCfg.SetListener(func(line []rune, pos int, key rune) (newLine []rune, newPos int, ok bool) {
l.SetPrompt(fmt.Sprintf("Enter password(%v): ", len(line)))
l.SetPrompt(readline.StaticPrompt(fmt.Sprintf("Enter password(%v): ", len(line))))
l.Refresh()
return nil, 0, false
})
Expand Down Expand Up @@ -124,7 +124,7 @@ func main() {
println("current mode: emacs")
}
case line == "login":
pswd, err := l.ReadPassword("please enter your password: ")
pswd, err := l.ReadPassword(readline.StaticPrompt("please enter your password: "))
if err != nil {
break
}
Expand All @@ -141,7 +141,7 @@ func main() {
log.Println("setprompt <prompt>")
break
}
l.SetPrompt(line[10:])
l.SetPrompt(readline.StaticPrompt(line[10:]))
case strings.HasPrefix(line, "say"):
line := strings.TrimSpace(line[3:])
if len(line) == 0 {
Expand Down
4 changes: 2 additions & 2 deletions example/readline-im/readline-im.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func main() {
}
defer rl.Close()

rl.SetPrompt("username: ")
rl.SetPrompt(readline.StaticPrompt("username: "))
username, err := rl.Readline()
if err != nil {
return
Expand All @@ -27,7 +27,7 @@ func main() {
log.SetOutput(rl.Stderr())

fmt.Fprintln(rl, "Hi,", username+"! My name is Dave.")
rl.SetPrompt(username + "> ")
rl.SetPrompt(readline.StaticPrompt(username + "> "))

done := make(chan struct{})
go func() {
Expand Down
6 changes: 3 additions & 3 deletions example/readline-multiline/readline-multiline.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (

func main() {
rl, err := readline.NewEx(&readline.Config{
Prompt: "> ",
Prompt: readline.StaticPrompt("> "),
HistoryFile: "/tmp/readline-multiline",
DisableAutoSaveHistory: true,
})
Expand All @@ -29,12 +29,12 @@ func main() {
}
cmds = append(cmds, line)
if !strings.HasSuffix(line, ";") {
rl.SetPrompt(">>> ")
rl.SetPrompt(readline.StaticPrompt(">>> "))
continue
}
cmd := strings.Join(cmds, " ")
cmds = cmds[:0]
rl.SetPrompt("> ")
rl.SetPrompt(readline.StaticPrompt("> "))
rl.SaveHistory(cmd)
println(cmd)
}
Expand Down
4 changes: 2 additions & 2 deletions example/readline-pass-strength/readline-pass-strength.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,15 +80,15 @@ func createStrengthPrompt(password []rune) string {
}

func main() {
rl, err := readline.New("")
rl, err := readline.New(readline.StaticPrompt(""))
if err != nil {
return
}
defer rl.Close()

setPasswordCfg := rl.GenPasswordConfig()
setPasswordCfg.SetListener(func(line []rune, pos int, key rune) (newLine []rune, newPos int, ok bool) {
rl.SetPrompt(createStrengthPrompt(line))
rl.SetPrompt(readline.StaticPrompt(createStrengthPrompt(line)))
rl.Refresh()
return nil, 0, false
})
Expand Down
2 changes: 1 addition & 1 deletion example/readline-remote/readline-remote-server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (

func main() {
cfg := &readline.Config{
Prompt: "readline-remote: ",
Prompt: readline.StaticPrompt("readline-remote: "),
}
handleFunc := func(rl *readline.Instance) {
for {
Expand Down
7 changes: 4 additions & 3 deletions operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package readline

import (
"errors"
"fmt"
"io"
"sync"
)
Expand Down Expand Up @@ -89,7 +90,7 @@ func NewOperation(t *Terminal, cfg *Config) *Operation {
return op
}

func (o *Operation) SetPrompt(s string) {
func (o *Operation) SetPrompt(s fmt.Stringer) {
o.buf.SetPrompt(s)
}

Expand Down Expand Up @@ -398,7 +399,7 @@ func (o *Operation) Runes() ([]rune, error) {
}
}

func (o *Operation) PasswordEx(prompt string, l Listener) ([]byte, error) {
func (o *Operation) PasswordEx(prompt fmt.Stringer, l Listener) ([]byte, error) {
cfg := o.GenPasswordConfig()
cfg.Prompt = prompt
cfg.Listener = l
Expand All @@ -417,7 +418,7 @@ func (o *Operation) PasswordWithConfig(cfg *Config) ([]byte, error) {
return o.Slice()
}

func (o *Operation) Password(prompt string) ([]byte, error) {
func (o *Operation) Password(prompt fmt.Stringer) ([]byte, error) {
return o.PasswordEx(prompt, nil)
}

Expand Down
11 changes: 11 additions & 0 deletions prompt.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package readline

type StaticPrompt string

func (s StaticPrompt) String() string {
return string(s)
}

func NewStaticPrompt(s string) StaticPrompt {
return StaticPrompt(s)
}
22 changes: 15 additions & 7 deletions readline.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Readline is a pure go implementation for GNU-Readline kind library.
//
// example:
// rl, err := readline.New("> ")
// rl, err := readline.New(readline.StaticPrompt("> "))
// if err != nil {
// panic(err)
// }
Expand All @@ -17,7 +17,10 @@
//
package readline

import "io"
import (
"fmt"
"io"
)

type Instance struct {
Config *Config
Expand All @@ -27,7 +30,7 @@ type Instance struct {

type Config struct {
// prompt supports ANSI escape sequence, so we can color some characters even in windows
Prompt string
Prompt fmt.Stringer

// readline will persist historys to file where HistoryFile specified
HistoryFile string
Expand Down Expand Up @@ -94,6 +97,7 @@ func (c *Config) Init() error {
if c.inited {
return nil
}
c.Prompt = StaticPrompt("")
c.inited = true
if c.Stdin == nil {
c.Stdin = NewCancelableStdin(Stdin)
Expand Down Expand Up @@ -175,15 +179,19 @@ func NewEx(cfg *Config) (*Instance, error) {
}, nil
}

func New(prompt string) (*Instance, error) {
type Prompter interface {
String() string
}

func New(prompt fmt.Stringer) (*Instance, error) {
return NewEx(&Config{Prompt: prompt})
}

func (i *Instance) ResetHistory() {
i.Operation.ResetHistory()
}

func (i *Instance) SetPrompt(s string) {
func (i *Instance) SetPrompt(s fmt.Stringer) {
i.Operation.SetPrompt(s)
}

Expand Down Expand Up @@ -224,11 +232,11 @@ func (i *Instance) ReadPasswordWithConfig(cfg *Config) ([]byte, error) {
return i.Operation.PasswordWithConfig(cfg)
}

func (i *Instance) ReadPasswordEx(prompt string, l Listener) ([]byte, error) {
func (i *Instance) ReadPasswordEx(prompt fmt.Stringer, l Listener) ([]byte, error) {
return i.Operation.PasswordEx(prompt, l)
}

func (i *Instance) ReadPassword(prompt string) ([]byte, error) {
func (i *Instance) ReadPassword(prompt fmt.Stringer) ([]byte, error) {
return i.Operation.Password(prompt)
}

Expand Down
6 changes: 4 additions & 2 deletions readline_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,17 @@ import (
)

func TestRace(t *testing.T) {
rl, err := NewEx(&Config{})
rl, err := NewEx(&Config{
Prompt: StaticPrompt(""),
})
if err != nil {
t.Fatal(err)
return
}

go func() {
for range time.Tick(time.Millisecond) {
rl.SetPrompt("hello")
rl.SetPrompt(StaticPrompt("hello"))
}
}()

Expand Down
19 changes: 10 additions & 9 deletions runebuf.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package readline
import (
"bufio"
"bytes"
"fmt"
"io"
"strconv"
"strings"
Expand All @@ -17,7 +18,7 @@ type runeBufferBck struct {
type RuneBuffer struct {
buf []rune
idx int
prompt []rune
prompt fmt.Stringer
w io.Writer

hadClean bool
Expand All @@ -35,7 +36,7 @@ type RuneBuffer struct {
sync.Mutex
}

func (r* RuneBuffer) pushKill(text []rune) {
func (r *RuneBuffer) pushKill(text []rune) {
r.lastKill = append([]rune{}, text...)
}

Expand All @@ -61,7 +62,7 @@ func (r *RuneBuffer) Restore() {
})
}

func NewRuneBuffer(w io.Writer, prompt string, cfg *Config, width int) *RuneBuffer {
func NewRuneBuffer(w io.Writer, prompt fmt.Stringer, cfg *Config, width int) *RuneBuffer {
rb := &RuneBuffer{
w: w,
interactive: cfg.useInteractive(),
Expand Down Expand Up @@ -99,7 +100,7 @@ func (r *RuneBuffer) PromptLen() int {
}

func (r *RuneBuffer) promptLen() int {
return runes.WidthAll(runes.ColorFilter(r.prompt))
return runes.WidthAll(runes.ColorFilter([]rune(r.prompt.String())))
}

func (r *RuneBuffer) RuneSlice(i int) []rune {
Expand Down Expand Up @@ -221,7 +222,7 @@ func (r *RuneBuffer) DeleteWord() {
}
for i := init + 1; i < len(r.buf); i++ {
if !IsWordBreak(r.buf[i]) && IsWordBreak(r.buf[i-1]) {
r.pushKill(r.buf[r.idx:i-1])
r.pushKill(r.buf[r.idx : i-1])
r.Refresh(func() {
r.buf = append(r.buf[:r.idx], r.buf[i-1:]...)
})
Expand Down Expand Up @@ -350,7 +351,7 @@ func (r *RuneBuffer) Yank() {
return
}
r.Refresh(func() {
buf := make([]rune, 0, len(r.buf) + len(r.lastKill))
buf := make([]rune, 0, len(r.buf)+len(r.lastKill))
buf = append(buf, r.buf[:r.idx]...)
buf = append(buf, r.lastKill...)
buf = append(buf, r.buf[r.idx:]...)
Expand Down Expand Up @@ -478,7 +479,7 @@ func (r *RuneBuffer) print() {

func (r *RuneBuffer) output() []byte {
buf := bytes.NewBuffer(nil)
buf.WriteString(string(r.prompt))
buf.WriteString(r.prompt.String())
if r.cfg.EnableMask && len(r.buf) > 0 {
buf.Write([]byte(strings.Repeat(string(r.cfg.MaskRune), len(r.buf)-1)))
if r.buf[len(r.buf)-1] == '\n' {
Expand Down Expand Up @@ -582,9 +583,9 @@ func (r *RuneBuffer) Set(buf []rune) {
r.SetWithIdx(len(buf), buf)
}

func (r *RuneBuffer) SetPrompt(prompt string) {
func (r *RuneBuffer) SetPrompt(prompt fmt.Stringer) {
r.Lock()
r.prompt = []rune(prompt)
r.prompt = prompt //[]rune(prompt)
r.Unlock()
}

Expand Down
5 changes: 3 additions & 2 deletions std.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package readline

import (
"fmt"
"io"
"os"
"sync"
Expand Down Expand Up @@ -54,13 +55,13 @@ func AddHistory(content string) error {
return ins.SaveHistory(content)
}

func Password(prompt string) ([]byte, error) {
func Password(prompt fmt.Stringer) ([]byte, error) {
ins := getInstance()
return ins.ReadPassword(prompt)
}

// readline with global configs
func Line(prompt string) (string, error) {
func Line(prompt fmt.Stringer) (string, error) {
ins := getInstance()
ins.SetPrompt(prompt)
return ins.Readline()
Expand Down