1
1
mirror of https://github.com/kanaka/mal.git synced 2024-11-11 00:52:44 +03:00
mal/crystal/readline.cr

22 lines
416 B
Crystal
Raw Normal View History

2015-05-01 20:09:52 +03:00
# Note:
# Crystal already has "readline" library.
# I implemented a subset of it again for practice.
@[Link("readline")]
lib LibReadline
fun readline(prompt : UInt8*) : UInt8*
fun add_history(line : UInt8*)
end
2015-05-04 06:12:24 +03:00
def my_readline(prompt = "")
line = LibReadline.readline(prompt)
if line
LibReadline.add_history(line)
String.new(line)
else
nil
2015-05-01 20:09:52 +03:00
end
2015-05-04 06:12:24 +03:00
ensure
LibC.free(line as Void*) if line
2015-05-02 09:44:10 +03:00
end