Commit | Line | Data |
---|---|---|
85c1b0c0 | 1 | # Note: |
2 | # Crystal already has "readline" library. | |
3 | # I implemented a subset of it again for practice. | |
4 | ||
5 | @[Link("readline")] | |
6 | lib LibReadline | |
7 | fun readline(prompt : UInt8*) : UInt8* | |
8 | fun add_history(line : UInt8*) | |
9 | end | |
10 | ||
fdf28b76 | 11 | def my_readline(prompt = "") |
12 | line = LibReadline.readline(prompt) | |
13 | if line | |
14 | LibReadline.add_history(line) | |
15 | String.new(line) | |
16 | else | |
17 | nil | |
85c1b0c0 | 18 | end |
fdf28b76 | 19 | ensure |
20 | LibC.free(line as Void*) if line | |
2c436e30 | 21 | end |