-
Notifications
You must be signed in to change notification settings - Fork 2
/
linenoise.go
98 lines (82 loc) · 1.7 KB
/
linenoise.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package linenoise
/*
#include <stdlib.h>
#include "linenoise.h"
extern void goCompletionEntry(char * buf, void * lc);
*/
import "C"
import "unsafe"
var cc CompletionCallback
type CompletionCallback func(buf string, lc unsafe.Pointer)
//export goCompletionEntry
func goCompletionEntry(buf * C.char, lc unsafe.Pointer) {
if cc != nil {
cc(C.GoString(buf), lc)
return
}
return
}
func SetCompletionCallback(fn CompletionCallback) {
cc = fn
return
}
func AddCompletion(lc unsafe.Pointer, completion string) {
c := C.CString(completion)
defer C.free(unsafe.Pointer(c))
C.linenoiseAddCompletion(lc, c)
}
func Scan(prompt string) (line string, end bool) {
pt := C.CString(prompt)
defer C.free(unsafe.Pointer(pt))
l := C.linenoise(pt)
defer C.free(unsafe.Pointer(l))
if l == nil {
end = true
} else {
end = false
}
return C.GoString(l), end
}
func HistoryAdd(line string) (done bool) {
l := C.CString(line)
defer C.free(unsafe.Pointer(l))
i := C.linenoiseHistoryAdd(l)
if i == 1 {
done = true
} else {
done = false
}
return done
}
func HistorySetMaxLen(length int) {
C.linenoiseHistorySetMaxLen(C.int(length))
return
}
func HistorySave(filename string) {
f := C.CString(filename)
defer C.free(unsafe.Pointer(f))
C.linenoiseHistorySave(f)
return
}
func HistoryLoad(filename string) {
f := C.CString(filename)
defer C.free(unsafe.Pointer(f))
C.linenoiseHistoryLoad(f)
return
}
func ClearScreen() {
C.linenoiseClearScreen()
return
}
func SetMultiLine(yes bool) {
if yes {
C.linenoiseSetMultiLine(C.int(1))
} else {
C.linenoiseSetMultiLine(C.int(0))
}
return
}
func PrintKeyCodes() {
C.linenoisePrintKeyCodes()
return
}