-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathgoreadline.go
61 lines (52 loc) · 1.26 KB
/
goreadline.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 linux darwin
// sniped from https://github.com/rocaltair/goreadline
package golisp
/*
#include <stdio.h>
#include <stdlib.h>
#include <readline/readline.h>
#include <readline/history.h>
#cgo LDFLAGS: -lreadline
*/
import "C"
import "unsafe"
func ReadLine(prompt *string) *string {
var cPrompt *C.char
if prompt != nil {
cPrompt = C.CString(*prompt)
}
cLine := C.readline(cPrompt)
if cPrompt != nil {
C.free(unsafe.Pointer(cPrompt))
}
if cLine == nil {
return nil
}
line := C.GoString(cLine)
C.free(unsafe.Pointer(cLine))
return &line
}
func AddHistory(line string) {
cLine := C.CString(line)
C.add_history(cLine)
C.free(unsafe.Pointer(cLine))
}
func ClearHistory() {
C.clear_history()
}
func WriteHistoryToFile(fileName string) {
cFileName := C.CString(fileName)
C.write_history(cFileName)
C.free(unsafe.Pointer(cFileName))
}
func LoadHistoryFromFile(fileName string) {
cFileName := C.CString(fileName)
C.read_history(cFileName)
C.free(unsafe.Pointer(cFileName))
}
func TruncateHistoryFile(fileName string, left int) {
cFileName := C.CString(fileName)
cLeft := C.int(left)
C.history_truncate_file(cFileName, cLeft)
C.free(unsafe.Pointer(cFileName))
}