-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 9a5cbe5
Showing
9 changed files
with
268 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
root = true | ||
|
||
[*.cr] | ||
charset = utf-8 | ||
end_of_line = lf | ||
insert_final_newline = true | ||
indent_style = space | ||
indent_size = 2 | ||
trim_trailing_whitespace = true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
/docs/ | ||
/lib/ | ||
/bin/ | ||
/.shards/ | ||
*.dwarf | ||
|
||
# Libraries don't need dependency lock | ||
# Dependencies will be locked in applications that use them | ||
/shard.lock |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
language: crystal | ||
|
||
crystal: | ||
- latest | ||
- nightly | ||
|
||
script: | ||
- crystal spec | ||
- crystal tool format --check |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
The MIT License (MIT) | ||
|
||
Copyright (c) 2019 Brian J. Cardiff | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in | ||
all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
# readline | ||
|
||
Crystal bindings to [GNU Readline Library](https://www.gnu.org/software/readline). | ||
|
||
## Installation | ||
|
||
1. Add the dependency to your `shard.yml`: | ||
|
||
```yaml | ||
dependencies: | ||
readline: | ||
github: crystal-lang/crystal-readline | ||
``` | ||
2. Run `shards install` | ||
|
||
## Usage | ||
|
||
```crystal | ||
require "readline" | ||
user_input = Readline.readline("> ") | ||
``` | ||
|
||
## Contributing | ||
|
||
1. Fork it (<https://github.com/crystal-lang/crystal-readline/fork>) | ||
2. Create your feature branch (`git checkout -b my-new-feature`) | ||
3. Commit your changes (`git commit -am 'Add some feature'`) | ||
4. Push to the branch (`git push origin my-new-feature`) | ||
5. Create a new Pull Request | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
name: readline | ||
version: 0.1.0 | ||
|
||
libraries: | ||
readline: "*" | ||
|
||
authors: | ||
- Brian J. Cardiff <[email protected]> | ||
- Franciscello <[email protected]> | ||
|
||
crystal: 0.31.1 | ||
|
||
license: MIT |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
require "./spec_helper" | ||
|
||
describe Readline do | ||
typeof(Readline.readline) | ||
typeof(Readline.readline("Hello", true)) | ||
typeof(Readline.readline(prompt: "Hello")) | ||
typeof(Readline.readline(add_history: false)) | ||
typeof(Readline.line_buffer) | ||
typeof(Readline.point) | ||
typeof(Readline.autocomplete { |s| %w(foo bar) }) | ||
|
||
it "gets prefix in bytesize between two strings" do | ||
Readline.common_prefix_bytesize("", "foo").should eq(0) | ||
Readline.common_prefix_bytesize("foo", "").should eq(0) | ||
Readline.common_prefix_bytesize("a", "a").should eq(1) | ||
Readline.common_prefix_bytesize("open", "operate").should eq(3) | ||
Readline.common_prefix_bytesize("operate", "open").should eq(3) | ||
Readline.common_prefix_bytesize(["operate", "open", "optional"]).should eq(2) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
require "spec" | ||
require "../src/readline" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,153 @@ | ||
@[Link("readline")] | ||
{% if flag?(:openbsd) %} | ||
@[Link("termcap")] | ||
{% end %} | ||
lib LibReadline | ||
alias Int = LibC::Int | ||
|
||
fun readline(prompt : UInt8*) : UInt8* | ||
fun add_history(line : UInt8*) | ||
fun rl_bind_key(key : Int, f : Int, Int -> Int) : Int | ||
fun rl_unbind_key(key : Int) : Int | ||
|
||
alias CPP = (UInt8*, Int, Int) -> UInt8** | ||
|
||
$rl_attempted_completion_function : CPP | ||
$rl_line_buffer : UInt8* | ||
$rl_point : Int | ||
$rl_done : Int | ||
end | ||
|
||
private def malloc_match(match) | ||
match_ptr = LibC.malloc(match.bytesize + 1).as(UInt8*) | ||
match_ptr.copy_from(match.to_unsafe, match.bytesize) | ||
match_ptr[match.bytesize] = 0_u8 | ||
match_ptr | ||
end | ||
|
||
module Readline | ||
VERSION = "0.1.0" | ||
|
||
extend self | ||
|
||
alias CompletionProc = String -> Array(String)? | ||
|
||
alias KeyBindingProc = Int32, Int32 -> Int32 | ||
KeyBindingHandler = ->(count : LibReadline::Int, key : LibReadline::Int) do | ||
if (handlers = @@key_bind_handlers) && handlers[key.to_i32]? | ||
res = handlers[key].call(count.to_i32, key.to_i32) | ||
LibReadline::Int.new(res) | ||
else | ||
LibReadline::Int.new(1) | ||
end | ||
end | ||
|
||
def readline(prompt = "", add_history = false) | ||
line = LibReadline.readline(prompt) | ||
if line | ||
LibReadline.add_history(line) if add_history | ||
String.new(line).tap { LibC.free(line.as(Void*)) } | ||
else | ||
nil | ||
end | ||
end | ||
|
||
def autocomplete(&@@completion_proc : CompletionProc) | ||
end | ||
|
||
def line_buffer | ||
line = LibReadline.rl_line_buffer | ||
return nil unless line | ||
|
||
String.new(line) | ||
end | ||
|
||
def point | ||
LibReadline.rl_point | ||
end | ||
|
||
def bind_key(c : Char, &f : KeyBindingProc) | ||
raise ArgumentError.new "Not a valid ASCII character: #{c.inspect}" unless c.ascii? | ||
|
||
handlers = (@@key_bind_handlers ||= {} of LibReadline::Int => KeyBindingProc) | ||
handlers[c.ord] = f | ||
|
||
res = LibReadline.rl_bind_key(c.ord, KeyBindingHandler).to_i32 | ||
raise ArgumentError.new "Invalid key: #{c.inspect}" unless res == 0 | ||
end | ||
|
||
def unbind_key(c : Char) | ||
if (handlers = @@key_bind_handlers) && handlers[c.ord]? | ||
handlers.delete(c.ord) | ||
res = LibReadline.rl_unbind_key(c.ord).to_i32 | ||
raise Exception.new "Error unbinding key: #{c.inspect}" unless res == 0 | ||
else | ||
raise KeyError.new "Key not bound: #{c.inspect}" | ||
end | ||
end | ||
|
||
def done | ||
LibReadline.rl_done != 0 | ||
end | ||
|
||
def done=(val : Bool) | ||
LibReadline.rl_done = val.hash | ||
end | ||
|
||
# :nodoc: | ||
def common_prefix_bytesize(str1 : String, str2 : String) | ||
r1 = Char::Reader.new str1 | ||
r2 = Char::Reader.new str2 | ||
|
||
while r1.has_next? && r2.has_next? | ||
break if r1.current_char != r2.current_char | ||
|
||
r1.next_char | ||
r2.next_char | ||
end | ||
|
||
r1.pos | ||
end | ||
|
||
# :nodoc: | ||
def common_prefix_bytesize(strings : Array) | ||
str1 = strings[0] | ||
low = str1.bytesize | ||
1.upto(strings.size - 1).each do |i| | ||
str2 = strings[i] | ||
low2 = common_prefix_bytesize(str1, str2) | ||
low = low2 if low2 < low | ||
end | ||
low | ||
end | ||
|
||
LibReadline.rl_attempted_completion_function = ->(text_ptr, start, finish) { | ||
completion_proc = @@completion_proc | ||
return Pointer(UInt8*).null unless completion_proc | ||
|
||
text = String.new(text_ptr) | ||
matches = completion_proc.call(text) | ||
|
||
return Pointer(UInt8*).null unless matches | ||
return Pointer(UInt8*).null if matches.empty? | ||
|
||
# We *must* to create the results using malloc (readline later frees that). | ||
# We create an extra result for the first element. | ||
result = LibC.malloc(sizeof(UInt8*) * (matches.size + 2)).as(UInt8**) | ||
matches.each_with_index do |match, i| | ||
result[i + 1] = malloc_match(match) | ||
end | ||
result[matches.size + 1] = Pointer(UInt8).null | ||
|
||
# The first element is the completion if it's oe | ||
if matches.size == 1 | ||
result[0] = malloc_match(matches[0]) | ||
else | ||
# Otherwise, we compute the common prefix of all matches | ||
low = Readline.common_prefix_bytesize(matches) | ||
sub = matches[0].byte_slice(0, low) | ||
result[0] = malloc_match(sub) | ||
end | ||
result | ||
} | ||
end |