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(list_manager:, keystroke_processor:, testing: false, 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
49
50
51
52
53
54
55
56
57
58
# File 'lib/diakonos/readline.rb', line 10

def initialize(
  list_manager:,
  keystroke_processor:,
  testing: false,
  window:,
  start_pos:,
  options: {},
  &block
)
  @list_manager = list_manager
  @keystroke_processor = keystroke_processor
  @testing = testing
  @window = window
  @start_pos = start_pos
  @window.setpos( 0, start_pos )
  @initial_text = options[ :initial_text ] || ''
  @completion_array = options[ :completion_array ]
  @list_filename = @list_manager.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 ! @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



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

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

#complete_inputObject



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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
# File 'lib/diakonos/readline.rb', line 147

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



139
140
141
142
143
144
145
# File 'lib/diakonos/readline.rb', line 139

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)


81
82
83
# File 'lib/diakonos/readline.rb', line 81

def done?
  @done
end

#finishObject



85
86
87
# File 'lib/diakonos/readline.rb', line 85

def finish
  @done = true
end

#get_inputObject



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

def get_input
  while ! done?
    @keystroke_processor.process_keystroke(Array.new, 'input')
  end
  input
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



89
90
91
92
93
# File 'lib/diakonos/readline.rb', line 89

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

#numbered_list?Boolean

Returns:

  • (Boolean)


95
96
97
# File 'lib/diakonos/readline.rb', line 95

def numbered_list?
  @numbered_list
end

#paste(s) ⇒ Object



116
117
118
119
120
# File 'lib/diakonos/readline.rb', line 116

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

#redraw_inputObject



122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/diakonos/readline.rb', line 122

def redraw_input
  # TODO: Gracefully handle when `input` is nil
  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



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

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

#syncObject



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/diakonos/readline.rb', line 99

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