-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreplxx.go
61 lines (52 loc) · 1.17 KB
/
replxx.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// +build rline_replxx
package rline
/*
#cgo LDFLAGS: -L${SRCDIR}/replxx -lreplxx -lstdc++ -lc
#include <stdlib.h>
#include "replxx/replxx.h"
*/
import "C"
import "unsafe"
func init() {
prompts[Replxx].init = NewReplxxPrompt
}
// ReplxxPrompt is a readline prompt.
type ReplxxPrompt struct {
replxx *C.Replxx
}
// NewReplxxPrompt creates a new replxx line reader.
func NewReplxxPrompt(app string, opts ...Option) (Prompter, error) {
ok, err := Replxx.Initialized()
switch {
case err != nil:
return nil, err
case ok:
return nil, &ErrPromptAlreadyInitialized{Replxx.String()}
}
p := &ReplxxPrompt{}
for _, o := range opts {
if err := o(p); err != nil {
return nil, err
}
}
p.replxx = C.replxx_init()
return p, nil
}
// Prompt prompts the user.
func (p *ReplxxPrompt) Prompt(s string) (string, error) {
buf := C.CString(s)
res, err := C.replxx_input(p.replxx, buf)
if err != nil {
return "", err
}
C.free(unsafe.Pointer(buf))
return C.GoString(res), nil
}
// Password prompts a password.
func (p *ReplxxPrompt) Password(string) (string, error) {
return "", nil
}
// Finalize finalizes the prompt.
func (p *ReplxxPrompt) Finalize() {
C.replxx_end(p.replxx)
}