Class: Coolline

Inherits:
Object show all
Defined in:
lib/chitin/core_ext/coolline.rb

Instance Method Summary collapse

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

#completeObject

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?
    menu.string = ""
  elsif completions.size == 1
    menu.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
    menu.list = completions
    self.completed_word = common_beginning(completions)
  end
end

#old_word_beginning_beforeObject



34
# File 'lib/chitin/core_ext/coolline.rb', line 34

alias_method :old_word_beginning_before, :word_beginning_before

#read_charObject

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. Notice how I went back to ‘@input.getch`, since Roger Pack was saying that `@input.raw { @input.getc }` wasn’t working on Windows.



54
55
56
57
# File 'lib/chitin/core_ext/coolline.rb', line 54

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.



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
113
114
115
116
117
118
119
120
121
# File 'lib/chitin/core_ext/coolline.rb', line 72

def readline(prompt = ">> ")
  lines = prompt.split("\n")

  if lines.size == 1
    @prompt = prompt
  else
    puts lines[0..-2]
    @prompt = lines.last
  end

  @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"
    @menu.erase

    handle(char)
    return if @should_exit

    if @history_moved
      @history_moved = false
    end

    render
  end

  @menu.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