From: Miki Tebeka Date: Sun, 1 Mar 2015 04:08:47 +0000 (+0200) Subject: readline works even when ~/.mal-history does not exist X-Git-Url: https://git.hcoop.net/jackhill/mal.git/commitdiff_plain/49916f9402e30a5277146355be878b32ec30f46d readline works even when ~/.mal-history does not exist --- diff --git a/go/src/readline/readline.go b/go/src/readline/readline.go index c3994a1d..31c1fbb3 100644 --- a/go/src/readline/readline.go +++ b/go/src/readline/readline.go @@ -8,6 +8,7 @@ package readline // free() #include // readline() +#include // FILE * #include // add_history() #include @@ -25,29 +26,32 @@ import ( ) var HISTORY_FILE = ".mal-history" +var history_path string -var rl_history_loaded = false - -func Readline(prompt string) (string, error) { - 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 - } +func loadHistory(filename string) error { + content, err := ioutil.ReadFile(history_path) + if err != nil { + return err + } - 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)) } + return nil +} + +func init() { + history_path = filepath.Join(os.Getenv("HOME"), HISTORY_FILE) + loadHistory(history_path) +} + +func Readline(prompt string) (string, error) { c_prompt := C.CString(prompt) defer C.free(unsafe.Pointer(c_prompt))