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

#readline(prompt = ">> ") ⇒ Object

This is to comment out ‘reset_line`. Now why would we do that, you ask? Because otherwise it overwrites the entire line when printing the prompt. And what’s wrong with that? This is undesirable behavior if the previous program run prints output that does NOT have a trailing newline. This was the source of a “bug” for a few days now.

NB: This preserves any input on the line for ONLY the initial printing of the prompt. After that, the render call in the #getch loop will overwrite it. This needs some work, obviously.



60
61
62
63
64
65
66
67
68
69
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
# File 'lib/chitin/core_ext/coolline.rb', line 60

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 = @input.getch) == "\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