diff options
Diffstat (limited to 'go/src/readline')
| -rw-r--r-- | go/src/readline/readline.go | 79 |
1 files changed, 42 insertions, 37 deletions
diff --git a/go/src/readline/readline.go b/go/src/readline/readline.go index 2777cb4..c3994a1 100644 --- a/go/src/readline/readline.go +++ b/go/src/readline/readline.go @@ -15,13 +15,13 @@ package readline import "C" import ( - "errors" - "unsafe" - "strings" - "io/ioutil" - "os" - "path/filepath" - "fmt" + "errors" + "fmt" + "io/ioutil" + "os" + "path/filepath" + "strings" + "unsafe" ) var HISTORY_FILE = ".mal-history" @@ -29,42 +29,47 @@ var HISTORY_FILE = ".mal-history" var rl_history_loaded = false func Readline(prompt string) (string, error) { - history_path := filepath.Join(os.Getenv("HOME"), "/", HISTORY_FILE) + history_path := filepath.Join(os.Getenv("HOME"), "/", HISTORY_FILE) - if !rl_history_loaded { - rl_history_loaded = true - content, e := ioutil.ReadFile(history_path) - if e != nil { return "", e } + if !rl_history_loaded { + rl_history_loaded = true + content, e := ioutil.ReadFile(history_path) + if e != nil { + return "", e + } - for _, add_line := range strings.Split(string(content), "\n") { - if add_line == "" { continue } - c_add_line := C.CString(add_line) - C.add_history(c_add_line) - C.free(unsafe.Pointer(c_add_line)) - } - } + for _, add_line := range strings.Split(string(content), "\n") { + if add_line == "" { + continue + } + c_add_line := C.CString(add_line) + C.add_history(c_add_line) + C.free(unsafe.Pointer(c_add_line)) + } + } + c_prompt := C.CString(prompt) + defer C.free(unsafe.Pointer(c_prompt)) - c_prompt := C.CString(prompt) - defer C.free(unsafe.Pointer(c_prompt)) + c_line := C.readline(c_prompt) + defer C.free(unsafe.Pointer(c_line)) + line := C.GoString(c_line) - c_line := C.readline(c_prompt) - defer C.free(unsafe.Pointer(c_line)) - line := C.GoString(c_line) + if c_line == nil { + return "", errors.New("C.readline call failed") + } + C.add_history(c_line) - if c_line == nil { - return "", errors.New("C.readline call failed") - } - C.add_history(c_line) + // append to file + f, e := os.OpenFile(history_path, os.O_APPEND|os.O_WRONLY, 0600) + if e == nil { + defer f.Close() - // append to file - f, e := os.OpenFile(history_path, os.O_APPEND|os.O_WRONLY, 0600) - if e == nil { - defer f.Close() + _, e = f.WriteString(line + "\n") + if e != nil { + fmt.Printf("error writing to history") + } + } - _, e = f.WriteString(line+"\n") - if e != nil { fmt.Printf("error writing to history") } - } - - return line, nil + return line, nil } |
