Class: Diakonos::Readline

Inherits:
Object show all
Defined in:
lib/diakonos/readline.rb,
lib/diakonos/readline/functions.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(diakonos, window, start_pos, options = {}, &block) ⇒ Readline

completion_array is the array of strings that tab completion can use The block returns true if a refresh is needed?

Parameters:

  • options (defaults to: {})

    :initial_text, :completion_array, :history, :do_complete, :on_dirs


10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/diakonos/readline.rb', line 10

def initialize( diakonos, window, start_pos, options = {}, &block )
  @diakonos = diakonos
  @window = window
  @start_pos = start_pos
  @window.setpos( 0, start_pos )
  @initial_text = options[ :initial_text ] || ''
  @completion_array = options[ :completion_array ]
  @list_filename = @diakonos.list_filename

  @history = options[ :history ] || []
  @history << @initial_text
  @history_index = [ @history.length - 1, 0 ].max

  @do_complete = options[ :do_complete ] || ::Diakonos::DONT_COMPLETE
  @on_dirs = options[ :on_dirs ] || :go_into_dirs
  @numbered_list = options[ :numbered_list ]

  @block = block

  # ---

  @input = @initial_text.dup
  if ! @input.empty?
    call_block
  end

  @icurx = @window.curx
  @icury = @window.cury
  @view_y = 0
  if ! @diakonos.testing
    @window.addstr @initial_text
  end
  @input_cursor = @initial_text.length
  @opened_list_file = false

  if @do_complete
    complete_input
  end
end

Instance Attribute Details

#inputObject (readonly)

Returns the value of attribute input


5
6
7
# File 'lib/diakonos/readline.rb', line 5

def input
  @input
end

Instance Method Details

#abortObject


5
6
7
8
# File 'lib/diakonos/readline/functions.rb', line 5

def abort
  @input = nil
  @done = true
end

#accept(item) ⇒ Object


10
11
12
13
14
15
16
# File 'lib/diakonos/readline/functions.rb', line 10

def accept( item )
  if item && @on_dirs == :go_into_dirs && File.directory?( item )
    complete_input
  else
    @done = true
  end
end

#backspaceObject


18
19
20
21
22
# File 'lib/diakonos/readline/functions.rb', line 18

def backspace
  if cursor_left
    delete
  end
end

#call_blockObject


50
51
52
53
54
55
# File 'lib/diakonos/readline.rb', line 50

def call_block
  if @block
    @block.call( @input )
    @window.refresh
  end
end

#complete_inputObject


129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'lib/diakonos/readline.rb', line 129

def complete_input
  if @completion_array && @input.length > 0
    len = @input.length
    matches = @completion_array.find_all { |el| el[ 0...len ] == @input && len <= el.length }
  else
    path = File.expand_path( @input || '.' )
    if FileTest.directory? path
      path << '/'
    end
    matches = Dir.glob( ( path + "*" ).gsub( /\*\*/, "*" ) )
    if @on_dirs == :accept_dirs
      matches = matches.select { |m| File.directory? m }
    end
  end
  matches.sort!

  if matches.length == 1
    @input = matches[ 0 ]
    cursor_write_input
    File.open( @list_filename, "w" ) do |f|
      if @completion_array.nil?
        f.puts "(unique)"
      else
        f.puts @input
      end
    end
    if @completion_array.nil? && FileTest.directory?( @input )
      @input << "/"
      cursor_write_input
      if @on_dirs != :accept_dirs
        complete_input
      end
    end
  elsif matches.length > 1
    common = matches[ 0 ]
    File.open( @list_filename, "w" ) do |f|
      i = nil
      matches.each do |match|
        f.print match
        if FileTest.directory?( match )
          f.print '/'
        end
        f.puts

        if match[ 0 ] != common[ 0 ]
          common = nil
          break
        end

        up_to = [ common.length - 1, match.length - 1 ].min
        i = 1
        while ( i <= up_to ) && ( match[ 0..i ] == common[ 0..i ] )
          i += 1
        end
        common = common[ 0...i ]
      end
    end
    if common.nil?
      File.open( @list_filename, "w" ) do |f|
        f.puts "(no matches)"
      end
    else
      @input = common
      cursor_write_input
    end
  else
    File.open( @list_filename, "w" ) do |f|
      f.puts "(no matches)"
    end
  end
  @diakonos.open_list_buffer
  @window.setpos( @window.cury, @window.curx )
end

#cursor_bolObject


24
25
26
27
# File 'lib/diakonos/readline/functions.rb', line 24

def cursor_bol
  @input_cursor = 0
  sync
end

#cursor_eolObject


29
30
31
32
# File 'lib/diakonos/readline/functions.rb', line 29

def cursor_eol
  @input_cursor = @input.length
  sync
end

#cursor_leftObject


34
35
36
37
38
39
# File 'lib/diakonos/readline/functions.rb', line 34

def cursor_left
  return false  if @input_cursor < 1
  @input_cursor -= 1
  sync
  true
end

#cursor_rightObject


41
42
43
44
45
# File 'lib/diakonos/readline/functions.rb', line 41

def cursor_right
  return  if @input_cursor >= @input.length
  @input_cursor += 1
  sync
end

#cursor_write_inputObject

Redisplays the input text starting at the start of the user input area, positioning the cursor at the end of the text.


121
122
123
124
125
126
127
# File 'lib/diakonos/readline.rb', line 121

def cursor_write_input
  if @input
    @input_cursor = @input.length
    @window.setpos( @window.cury, @icurx + @input.length )
    redraw_input
  end
end

#deleteObject


47
48
49
50
51
# File 'lib/diakonos/readline/functions.rb', line 47

def delete
  return  if @input_cursor >= @input.length
  @input = @input[ 0...@input_cursor ] + @input[ (@input_cursor + 1)..-1 ]
  sync
end

#delete_lineObject


53
54
55
56
# File 'lib/diakonos/readline/functions.rb', line 53

def delete_line
  @input = ""
  sync
end

#delete_wordObject


58
59
60
61
62
63
64
# File 'lib/diakonos/readline/functions.rb', line 58

def delete_word
  head = @input[ 0...@input_cursor ]
  chopped = head.sub( /\w+\W*$/, '' )
  @input = chopped + @input[ @input_cursor..-1 ]
  @input_cursor -= head.length - chopped.length
  sync
end

#done?Boolean

Returns:

  • (Boolean)

64
65
66
# File 'lib/diakonos/readline.rb', line 64

def done?
  @done
end

#finishObject


68
69
70
# File 'lib/diakonos/readline.rb', line 68

def finish
  @done = true
end

#history_downObject


73
74
75
76
77
78
# File 'lib/diakonos/readline/functions.rb', line 73

def history_down
  return  if @history_index > @history.length - 2
  @history[ @history_index ] = @input
  @history_index += 1
  @input = @history[ @history_index ]
end

#history_upObject


66
67
68
69
70
71
# File 'lib/diakonos/readline/functions.rb', line 66

def history_up
  return  if @history_index < 1
  @history[ @history_index ] = @input
  @history_index -= 1
  @input = @history[ @history_index ]
end

#list_sync(line) ⇒ Object


72
73
74
75
76
# File 'lib/diakonos/readline.rb', line 72

def list_sync( line )
  return  if line.nil?
  set_input line
  cursor_write_input
end

#numbered_list?Boolean

Returns:

  • (Boolean)

78
79
80
# File 'lib/diakonos/readline.rb', line 78

def numbered_list?
  @numbered_list
end

#paste(s) ⇒ Object


99
100
101
102
103
# File 'lib/diakonos/readline.rb', line 99

def paste( s )
  @input = @input[ 0...@input_cursor ] + s + @input[ @input_cursor..-1 ]
  @input_cursor += s.length
  sync
end

#redraw_inputObject


105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/diakonos/readline.rb', line 105

def redraw_input
  input = @input[ @view_y...(@view_y + Curses::cols) ]

  curx = @window.curx
  cury = @window.cury
  @window.setpos( @icury, @icurx )
  @window.addstr "%-#{ Curses::cols - curx }s%s" % [
    input,
    " " * [ ( Curses::cols - input.length ), 0 ].max
  ]
  @window.setpos( cury, curx )
  @window.refresh
end

#set_input(input = '') ⇒ Object


57
58
59
60
61
62
# File 'lib/diakonos/readline.rb', line 57

def set_input( input = '' )
  if numbered_list? && input =~ /^\w  /
    input = input[ 3..-1 ]
  end
  @input = input
end

#syncObject


82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/diakonos/readline.rb', line 82

def sync
  if @input_cursor > @input.length
    @input_cursor = @input.length
  elsif @input_cursor < @view_y
    @view_y = @input_cursor
  end

  diff = ( @input_cursor - @view_y ) + 1 - ( Curses::cols - @start_pos )
  if diff > 0
    @view_y += diff
  end

  @window.setpos( @window.cury, @start_pos + @input_cursor - @view_y )
  redraw_input
  call_block
end