Class: Coolline
Instance Method Summary collapse
- #bind(key, &action) ⇒ Object
-
#complete ⇒ Object
This is to deal wtih adding closing quotes on completions.
- #old_word_beginning_before ⇒ Object
-
#read_char ⇒ Object
In tests, this reads the first character of buffered input.
-
#readline(prompt = ">> ") ⇒ Object
This is here because the line: until (char = @input.getch) == “r” was replaced with: until (char = read_char) == “r” because I’m trying and failing to fix it so that Coolline will read buffered characters from STDIN.
-
#word_beginning_before(pos) ⇒ Object
This is to make word completion complete entire unquoted strings.
Instance Method Details
#bind(key, &action) ⇒ Object
4 5 6 |
# File 'lib/chitin/core_ext/coolline.rb', line 4 def bind(key, &action) @handlers.unshift Handler.new(key, &action) end |
#complete ⇒ Object
This is to deal wtih adding closing quotes on completions. It assumes that the completion proc will automatically place a leading quote on the line.
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/chitin/core_ext/coolline.rb', line 11 def complete return if word_boundary? line[pos - 1] completions = @completion_proc.call(self) if completions.empty? .string = "" elsif completions.size == 1 .string = "" word = completions.first # don't close quotes on directories if word[-1, 1] != '/' word += word[0, 1] # because there's a quote from when we expand the options end self.completed_word = word else .list = completions self.completed_word = common_beginning(completions) end end |
#old_word_beginning_before ⇒ Object
34 |
# File 'lib/chitin/core_ext/coolline.rb', line 34 alias_method :old_word_beginning_before, :word_beginning_before |
#read_char ⇒ Object
In tests, this reads the first character of buffered input. In practice, this does not do anything differently than ‘@input.getch` C’est la vie.
52 53 54 55 |
# File 'lib/chitin/core_ext/coolline.rb', line 52 def read_char #@input.raw { @input.getc } @input.getch end |
#readline(prompt = ">> ") ⇒ Object
This is here because the line:
until (char = @input.getch) == "\r"
was replaced with:
until (char = read_char) == "\r"
because I’m trying and failing to fix it so that Coolline will read buffered characters from STDIN. For example:
def test; sleep 2; STDIN.getch; end
if you run that and type a bunch of characters, it will only return the first character you typed AFTER STDIN.getch got a chance to run. Ideally, with ‘read_char` it will read the first character that you typed regardless.
Also, @history_moved appears to be a useless variable.
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/chitin/core_ext/coolline.rb', line 70 def readline(prompt = ">> ") @prompt = prompt @history.delete_empty @line = "" @pos = 0 @accumulator = nil @history_moved = false @should_exit = false reset_line print @prompt @history.index = @history.size - 1 @history << @line until (char = read_char) == "\r" .erase handle(char) return if @should_exit if @history_moved @history_moved = false end render end .erase print "\n" @history[-1] = @line if @history.size != 0 @history.index = @history.size @history.save_line @line end |
#word_beginning_before(pos) ⇒ Object
This is to make word completion complete entire unquoted strings. Very useful since Ruby doesn’t have space escapes like in Bash.
38 39 40 41 42 43 44 45 46 47 |
# File 'lib/chitin/core_ext/coolline.rb', line 38 def word_beginning_before(pos) # if the line is an uncompleted string, make it the whole string # else, do the normal shit if incomplete_string line[0..pos] point = line[0..pos].reverse.index incomplete_string(line[0..pos]) line[0..pos].size - point - 1 else old_word_beginning_before(pos) end end |