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"
|
"github.com/c-bata/go-prompt/completer"
|
||||||
)
|
)
|
||||||
|
|
||||||
func executor(in string) {
|
var filePathCompleter = completer.FilePathCompleter{
|
||||||
fmt.Println("Your input: " + in)
|
|
||||||
}
|
|
||||||
|
|
||||||
func main() {
|
|
||||||
c := completer.FilePathCompleter{
|
|
||||||
IgnoreCase: true,
|
IgnoreCase: true,
|
||||||
Filter: func(fi os.FileInfo) bool {
|
Filter: func(fi os.FileInfo) bool {
|
||||||
return fi.IsDir() || strings.HasSuffix(fi.Name(), ".go")
|
return fi.IsDir() || strings.HasSuffix(fi.Name(), ".go")
|
||||||
},
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
func executor(in string) {
|
||||||
|
fmt.Println("Your input: " + in)
|
||||||
|
}
|
||||||
|
|
||||||
|
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(
|
p := prompt.New(
|
||||||
executor,
|
executor,
|
||||||
c.Complete,
|
completerFunc,
|
||||||
prompt.OptionPrefix(">>> "),
|
prompt.OptionPrefix(">>> "),
|
||||||
prompt.OptionCompletionWordSeparator(completer.FilePathCompletionSeparator),
|
prompt.OptionCompletionWordSeparator(completer.FilePathCompletionSeparator),
|
||||||
)
|
)
|
||||||
|
49
_tools/sigwinch2/main.go
Normal file
49
_tools/sigwinch2/main.go
Normal file
@ -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,
|
Key: ControlL,
|
||||||
Fn: func(buf *Buffer) {
|
Fn: func(buf *Buffer) {
|
||||||
out := NewStandardOutputWriter()
|
consoleWriter.EraseScreen()
|
||||||
out.EraseScreen()
|
consoleWriter.CursorGoTo(0, 0)
|
||||||
out.CursorGoTo(0, 0)
|
consoleWriter.Flush()
|
||||||
out.Flush()
|
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
@ -15,6 +15,7 @@ func OptionParser(x ConsoleParser) Option {
|
|||||||
// OptionWriter to set a custom ConsoleWriter object. An argument should implement ConsoleWriter interface.
|
// OptionWriter to set a custom ConsoleWriter object. An argument should implement ConsoleWriter interface.
|
||||||
func OptionWriter(x ConsoleWriter) Option {
|
func OptionWriter(x ConsoleWriter) Option {
|
||||||
return func(p *Prompt) error {
|
return func(p *Prompt) error {
|
||||||
|
RegisterConsoleWriter(x)
|
||||||
p.renderer.out = x
|
p.renderer.out = x
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@ -227,11 +228,14 @@ func OptionAddASCIICodeBind(b ...ASCIICodeBind) Option {
|
|||||||
|
|
||||||
// New returns a Prompt with powerful auto-completion.
|
// New returns a Prompt with powerful auto-completion.
|
||||||
func New(executor Executor, completer Completer, opts ...Option) *Prompt {
|
func New(executor Executor, completer Completer, opts ...Option) *Prompt {
|
||||||
|
defaultWriter := NewStandardOutputWriter()
|
||||||
|
RegisterConsoleWriter(defaultWriter)
|
||||||
|
|
||||||
pt := &Prompt{
|
pt := &Prompt{
|
||||||
in: NewStandardInputParser(),
|
in: NewStandardInputParser(),
|
||||||
renderer: &Render{
|
renderer: &Render{
|
||||||
prefix: "> ",
|
prefix: "> ",
|
||||||
out: NewStandardOutputWriter(),
|
out: defaultWriter,
|
||||||
livePrefixCallback: func() (string, bool) { return "", false },
|
livePrefixCallback: func() (string, bool) { return "", false },
|
||||||
prefixTextColor: Blue,
|
prefixTextColor: Blue,
|
||||||
prefixBGColor: DefaultColor,
|
prefixBGColor: DefaultColor,
|
||||||
|
13
output.go
13
output.go
@ -1,5 +1,18 @@
|
|||||||
package prompt
|
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.
|
// DisplayAttribute represents display attributes like Blinking, Bold, Italic and so on.
|
||||||
type DisplayAttribute int
|
type DisplayAttribute int
|
||||||
|
|
||||||
|
@ -45,7 +45,7 @@ var _ ConsoleWriter = &PosixWriter{}
|
|||||||
// NewStandardOutputWriter returns ConsoleWriter object to write to stdout.
|
// NewStandardOutputWriter returns ConsoleWriter object to write to stdout.
|
||||||
// This generates VT100 escape sequences because almost terminal emulators
|
// This generates VT100 escape sequences because almost terminal emulators
|
||||||
// in POSIX OS built on top of a VT100 specification.
|
// in POSIX OS built on top of a VT100 specification.
|
||||||
func NewStandardOutputWriter() *PosixWriter {
|
func NewStandardOutputWriter() ConsoleWriter {
|
||||||
return &PosixWriter{
|
return &PosixWriter{
|
||||||
fd: syscall.Stdout,
|
fd: syscall.Stdout,
|
||||||
}
|
}
|
||||||
|
@ -29,7 +29,7 @@ var _ ConsoleWriter = &WindowsWriter{}
|
|||||||
|
|
||||||
// NewStandardOutputWriter returns ConsoleWriter object to write to stdout.
|
// NewStandardOutputWriter returns ConsoleWriter object to write to stdout.
|
||||||
// This generates win32 control sequences.
|
// This generates win32 control sequences.
|
||||||
func NewStandardOutputWriter() *WindowsWriter {
|
func NewStandardOutputWriter() ConsoleWriter {
|
||||||
return &WindowsWriter{
|
return &WindowsWriter{
|
||||||
out: colorable.NewColorableStdout(),
|
out: colorable.NewColorableStdout(),
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user