diff options
| author | Miki Tebeka <miki.tebeka@gmail.com> | 2015-03-01 06:08:47 +0200 |
|---|---|---|
| committer | Miki Tebeka <miki.tebeka@gmail.com> | 2015-03-01 06:08:47 +0200 |
| commit | 49916f9402e30a5277146355be878b32ec30f46d (patch) | |
| tree | 17d80fdc91cdce9c59b9e551925e6e311bb14f82 /go | |
| parent | 978af8a768be2ed4b0be249a2e87744d0ba8307d (diff) | |
| download | mal-49916f9402e30a5277146355be878b32ec30f46d.tar.gz mal-49916f9402e30a5277146355be878b32ec30f46d.zip | |
readline works even when ~/.mal-history does not exist
Diffstat (limited to 'go')
| -rw-r--r-- | go/src/readline/readline.go | 40 |
1 files changed, 22 insertions, 18 deletions
diff --git a/go/src/readline/readline.go b/go/src/readline/readline.go index c3994a1..31c1fbb 100644 --- a/go/src/readline/readline.go +++ b/go/src/readline/readline.go @@ -8,6 +8,7 @@ package readline // free() #include <stdlib.h> // readline() +#include <stdio.h> // FILE * #include <readline/readline.h> // add_history() #include <readline/history.h> @@ -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)) |
