Commit | Line | Data |
---|---|---|
2539e6af KR |
1 | //****************************************************************************** |
2 | // MAL - readline | |
3 | //****************************************************************************** | |
4 | ||
5 | import Foundation | |
6 | ||
7 | private let HISTORY_FILE = "~/.mal-history" | |
8 | ||
425305df | 9 | private func with_history_file(do_to_history_file: (UnsafePointer<Int8>) -> ()) { |
2539e6af KR |
10 | HISTORY_FILE.withCString { |
11 | (c_str) -> () in | |
12 | let abs_path = tilde_expand(UnsafeMutablePointer<Int8>(c_str)) | |
13 | if abs_path != nil { | |
14 | do_to_history_file(abs_path) | |
15 | free(abs_path) | |
16 | } | |
17 | } | |
18 | } | |
19 | ||
20 | func load_history_file() { | |
21 | using_history() | |
22 | with_history_file { | |
23 | let _ = read_history($0) | |
24 | } | |
25 | } | |
26 | ||
27 | func save_history_file() { | |
28 | // Do this? stifle_history(1000) | |
29 | with_history_file { | |
30 | let _ = write_history($0) | |
31 | } | |
32 | } | |
33 | ||
34 | func _readline(prompt: String) -> String? { | |
35 | let line = prompt.withCString { | |
36 | (c_str) -> UnsafeMutablePointer<Int8> in | |
37 | return readline(c_str) | |
38 | } | |
39 | if line != nil { | |
40 | if let result = String(UTF8String: line) { | |
41 | add_history(line) | |
42 | return result | |
43 | } | |
44 | } | |
45 | return nil | |
46 | } |