-
Notifications
You must be signed in to change notification settings - Fork 0
/
readline.go
55 lines (44 loc) · 1014 Bytes
/
readline.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
//see http://cheesesun.blogspot.com/2009/12/basic-cgo.html for basic explanation
// Wrapper around the GNU readline(3) library
package main
/*
#cgo darwin CFLAGS: -I/usr/local/opt/readline/include
#cgo darwin LDFLAGS: -L/usr/local/opt/readline/lib
#cgo LDFLAGS: -lreadline
#include <stdio.h>
#include <stdlib.h>
#include <readline/readline.h>
#include <readline/history.h>
*/
import "C"
import "unsafe"
func init() {
// Can't handle console resize yet
// So we disable all signal handle in readline
C.rl_catch_signals = 0
C.rl_catch_sigwinch = 0
C.rl_clear_signals()
C.rl_set_signals()
}
func ReadLine(prompt *string) *string {
var p *C.char
//readline allows an empty prompt(NULL)
if prompt != nil {
p = C.CString(*prompt)
}
ret := C.readline(p)
if p != nil {
C.free(unsafe.Pointer(p))
}
if ret == nil {
return nil
} //EOF
s := C.GoString(ret)
C.free(unsafe.Pointer(ret))
return &s
}
func AddHistory(s string) {
p := C.CString(s)
C.add_history(p)
C.free(unsafe.Pointer(p))
}