Fix a bug when using OptionWriter
This commit is contained in:
parent
15a5ff0630
commit
433f2c9b36
|
@ -9,20 +9,33 @@ import (
|
|||
"github.com/c-bata/go-prompt/completer"
|
||||
)
|
||||
|
||||
var filePathCompleter = completer.FilePathCompleter{
|
||||
IgnoreCase: true,
|
||||
Filter: func(fi os.FileInfo) bool {
|
||||
return fi.IsDir() || strings.HasSuffix(fi.Name(), ".go")
|
||||
},
|
||||
}
|
||||
|
||||
func executor(in string) {
|
||||
fmt.Println("Your input: " + in)
|
||||
}
|
||||
|
||||
func main() {
|
||||
c := completer.FilePathCompleter{
|
||||
IgnoreCase: true,
|
||||
Filter: func(fi os.FileInfo) bool {
|
||||
return fi.IsDir() || strings.HasSuffix(fi.Name(), ".go")
|
||||
},
|
||||
func completerFunc(d prompt.Document) []prompt.Suggest {
|
||||
t := d.GetWordBeforeCursor()
|
||||
if strings.HasPrefix(t, "--") {
|
||||
return []prompt.Suggest{
|
||||
{"--foo", ""},
|
||||
{"--bar", ""},
|
||||
{"--baz", ""},
|
||||
}
|
||||
}
|
||||
return filePathCompleter.Complete(d)
|
||||
}
|
||||
|
||||
func main() {
|
||||
p := prompt.New(
|
||||
executor,
|
||||
c.Complete,
|
||||
completerFunc,
|
||||
prompt.OptionPrefix(">>> "),
|
||||
prompt.OptionCompletionWordSeparator(completer.FilePathCompletionSeparator),
|
||||
)
|
||||
|
|
|
@ -0,0 +1,49 @@
|
|||
// +build windows
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"syscall"
|
||||
"unsafe"
|
||||
|
||||
"github.com/mattn/go-tty"
|
||||
)
|
||||
|
||||
const maxReadByteLen = 1024
|
||||
|
||||
var kernel32 = syscall.NewLazyDLL("kernel32.dll")
|
||||
|
||||
var procGetNumberOfConsoleInputEvents = kernel32.NewProc("GetNumberOfConsoleInputEvents")
|
||||
|
||||
func main() {
|
||||
t, err := tty.Open()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
sigwinch := t.SIGWINCH()
|
||||
|
||||
go func() {
|
||||
for {
|
||||
select {
|
||||
default:
|
||||
var ev uint32
|
||||
r0, _, err := procGetNumberOfConsoleInputEvents.Call(t.Input().Fd(), uintptr(unsafe.Pointer(&ev)))
|
||||
if r0 == 0 {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
if ev == 0 {
|
||||
fmt.Println("EAGAIN")
|
||||
}
|
||||
t.ReadRune()
|
||||
}
|
||||
t.ReadRune()
|
||||
}
|
||||
}()
|
||||
|
||||
for {
|
||||
ws := <-sigwinch
|
||||
fmt.Printf("Row %d : Col %d\n", ws.H, ws.W)
|
||||
}
|
||||
}
|
7
emacs.go
7
emacs.go
|
@ -110,10 +110,9 @@ var emacsKeyBindings = []KeyBind{
|
|||
{
|
||||
Key: ControlL,
|
||||
Fn: func(buf *Buffer) {
|
||||
out := NewStandardOutputWriter()
|
||||
out.EraseScreen()
|
||||
out.CursorGoTo(0, 0)
|
||||
out.Flush()
|
||||
consoleWriter.EraseScreen()
|
||||
consoleWriter.CursorGoTo(0, 0)
|
||||
consoleWriter.Flush()
|
||||
},
|
||||
},
|
||||
}
|
||||
|
|
|
@ -15,6 +15,7 @@ func OptionParser(x ConsoleParser) Option {
|
|||
// OptionWriter to set a custom ConsoleWriter object. An argument should implement ConsoleWriter interface.
|
||||
func OptionWriter(x ConsoleWriter) Option {
|
||||
return func(p *Prompt) error {
|
||||
RegisterConsoleWriter(x)
|
||||
p.renderer.out = x
|
||||
return nil
|
||||
}
|
||||
|
@ -227,11 +228,14 @@ func OptionAddASCIICodeBind(b ...ASCIICodeBind) Option {
|
|||
|
||||
// New returns a Prompt with powerful auto-completion.
|
||||
func New(executor Executor, completer Completer, opts ...Option) *Prompt {
|
||||
defaultWriter := NewStandardOutputWriter()
|
||||
RegisterConsoleWriter(defaultWriter)
|
||||
|
||||
pt := &Prompt{
|
||||
in: NewStandardInputParser(),
|
||||
renderer: &Render{
|
||||
prefix: "> ",
|
||||
out: NewStandardOutputWriter(),
|
||||
out: defaultWriter,
|
||||
livePrefixCallback: func() (string, bool) { return "", false },
|
||||
prefixTextColor: Blue,
|
||||
prefixBGColor: DefaultColor,
|
||||
|
|
13
output.go
13
output.go
|
@ -1,5 +1,18 @@
|
|||
package prompt
|
||||
|
||||
import "sync"
|
||||
|
||||
var (
|
||||
consoleWriterMu sync.Mutex
|
||||
consoleWriter ConsoleWriter
|
||||
)
|
||||
|
||||
func RegisterConsoleWriter(f ConsoleWriter) {
|
||||
consoleWriterMu.Lock()
|
||||
defer consoleWriterMu.Unlock()
|
||||
consoleWriter = f
|
||||
}
|
||||
|
||||
// DisplayAttribute represents display attributes like Blinking, Bold, Italic and so on.
|
||||
type DisplayAttribute int
|
||||
|
||||
|
|
|
@ -45,7 +45,7 @@ var _ ConsoleWriter = &PosixWriter{}
|
|||
// NewStandardOutputWriter returns ConsoleWriter object to write to stdout.
|
||||
// This generates VT100 escape sequences because almost terminal emulators
|
||||
// in POSIX OS built on top of a VT100 specification.
|
||||
func NewStandardOutputWriter() *PosixWriter {
|
||||
func NewStandardOutputWriter() ConsoleWriter {
|
||||
return &PosixWriter{
|
||||
fd: syscall.Stdout,
|
||||
}
|
||||
|
|
|
@ -29,7 +29,7 @@ var _ ConsoleWriter = &WindowsWriter{}
|
|||
|
||||
// NewStandardOutputWriter returns ConsoleWriter object to write to stdout.
|
||||
// This generates win32 control sequences.
|
||||
func NewStandardOutputWriter() *WindowsWriter {
|
||||
func NewStandardOutputWriter() ConsoleWriter {
|
||||
return &WindowsWriter{
|
||||
out: colorable.NewColorableStdout(),
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue