Class: ImportJS::CommandLineEditor

Inherits:
Object
  • Object
show all
Defined in:
lib/import_js/command_line_editor.rb

Instance Method Summary collapse

Constructor Details

#initialize(lines, opts) ⇒ CommandLineEditor

Returns a new instance of CommandLineEditor.



3
4
5
6
7
8
9
10
# File 'lib/import_js/command_line_editor.rb', line 3

def initialize(lines, opts)
  @lines = lines
  @messages = []
  @ask_for_selections = []
  @selections = opts[:selections] unless opts[:selections].empty?
  @word = opts[:word]
  @filename = opts[:filename]
end

Instance Method Details

#append_line(line_number, str) ⇒ Object

Append a line right after the specified line.

Lines are one-indexed, but you need to support appending to line 0 (add content at top of file).

Parameters:

  • line_number (Number)


89
90
91
# File 'lib/import_js/command_line_editor.rb', line 89

def append_line(line_number, str)
  @lines.insert(line_number, str)
end

#ask_for_selection(word, alternatives) ⇒ Number?

Ask the user to select something from a list of alternatives.

Parameters:

  • word (String)

    The word/variable to import

  • alternatives (Array<String>)

    A list of alternatives

Returns:

  • (Number, nil)

    the index of the selected alternative, or nil if nothing was selected.



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

def ask_for_selection(word, alternatives)
  if @selections
    # this is a re-run, where selections have already been made
    @selections[word]
  else
    @ask_for_selections << {
      word: word,
      alternatives: alternatives
    }
    nil
  end
end

#ask_for_selectionsArray

Returns:

  • (Array)


38
39
40
# File 'lib/import_js/command_line_editor.rb', line 38

def ask_for_selections
  @ask_for_selections
end

#count_linesNumber

Count the number of lines in the file.

Returns:

  • (Number)

    the number of lines in the file



96
97
98
# File 'lib/import_js/command_line_editor.rb', line 96

def count_lines
  @lines.length
end

#current_file_contentString

Returns:

  • (String)


43
44
45
# File 'lib/import_js/command_line_editor.rb', line 43

def current_file_content
  @lines.join("\n")
end

#current_wordString

Returns:

  • (String)


13
14
15
# File 'lib/import_js/command_line_editor.rb', line 13

def current_word
  @word
end

#cursorArray(Number, Number)

Get the cursor position.

Returns:

  • (Array(Number, Number))


63
64
65
66
# File 'lib/import_js/command_line_editor.rb', line 63

def cursor
  # not relevant for this editor
  [0, 0]
end

#cursor=(_position_tuple) ⇒ Object

Place the cursor at a specified position.

Parameters:

  • _position_tuple (Array(Number, Number))

    the row and column to place the cursor at.



72
73
74
# File 'lib/import_js/command_line_editor.rb', line 72

def cursor=(_position_tuple)
  # no-op
end

#delete_line(line_number) ⇒ Object

Delete a line.

Parameters:

  • line_number (Number)

    One-indexed line number. 1 is the first line in the file.



80
81
82
# File 'lib/import_js/command_line_editor.rb', line 80

def delete_line(line_number)
  @lines.delete_at(line_number - 1)
end

#gotoString

Returns:

  • (String)


28
29
30
# File 'lib/import_js/command_line_editor.rb', line 28

def goto
  @goto
end

#max_line_lengthNumber?

Get the preferred max length of a line.

Returns:

  • (Number?)


121
122
123
# File 'lib/import_js/command_line_editor.rb', line 121

def max_line_length
  80
end

#message(str) ⇒ Object

Parameters:

  • str (String)


33
34
35
# File 'lib/import_js/command_line_editor.rb', line 33

def message(str)
  @messages << str
end

#messagesString

Returns:

  • (String)


48
49
50
# File 'lib/import_js/command_line_editor.rb', line 48

def messages
  @messages.join("\n")
end

#open_file(file_path) ⇒ Object

Parameters:

  • file_path (String)


23
24
25
# File 'lib/import_js/command_line_editor.rb', line 23

def open_file(file_path)
  @goto = file_path
end

#path_to_current_fileString?

Returns:

  • (String?)


18
19
20
# File 'lib/import_js/command_line_editor.rb', line 18

def path_to_current_file
  @filename
end

#read_line(line_number) ⇒ String

Reads a line from the file.

Lines are one-indexed, so 1 means the first line in the file.

Returns:

  • (String)


56
57
58
# File 'lib/import_js/command_line_editor.rb', line 56

def read_line(line_number)
  @lines[line_number - 1]
end

#tabString

Returns shiftwidth number of spaces if expandtab is not set, otherwise ‘t`.

Returns:

  • (String)

    shiftwidth number of spaces if expandtab is not set, otherwise ‘t`.



127
128
129
# File 'lib/import_js/command_line_editor.rb', line 127

def tab
  '  '
end