Class: ImportJS::CommandLineEditor
- Inherits:
-
Object
- Object
- ImportJS::CommandLineEditor
- Defined in:
- lib/import_js/command_line_editor.rb
Instance Method Summary collapse
-
#append_line(line_number, str) ⇒ Object
Append a line right after the specified line.
-
#ask_for_selection(word, alternatives) ⇒ Number?
Ask the user to select something from a list of alternatives.
- #ask_for_selections ⇒ Array
-
#count_lines ⇒ Number
Count the number of lines in the file.
- #current_file_content ⇒ String
- #current_word ⇒ String
-
#cursor ⇒ Array(Number, Number)
Get the cursor position.
-
#cursor=(_position_tuple) ⇒ Object
Place the cursor at a specified position.
-
#delete_line(line_number) ⇒ Object
Delete a line.
- #goto ⇒ String
-
#initialize(lines, opts) ⇒ CommandLineEditor
constructor
A new instance of CommandLineEditor.
-
#max_line_length ⇒ Number?
Get the preferred max length of a line.
- #message(str) ⇒ Object
- #messages ⇒ String
- #open_file(file_path) ⇒ Object
- #path_to_current_file ⇒ String?
-
#read_line(line_number) ⇒ String
Reads a line from the file.
-
#tab ⇒ String
Shiftwidth number of spaces if expandtab is not set, otherwise ‘t`.
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).
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.
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_selections ⇒ Array
38 39 40 |
# File 'lib/import_js/command_line_editor.rb', line 38 def ask_for_selections @ask_for_selections end |
#count_lines ⇒ Number
Count 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_content ⇒ String
43 44 45 |
# File 'lib/import_js/command_line_editor.rb', line 43 def current_file_content @lines.join("\n") end |
#current_word ⇒ String
13 14 15 |
# File 'lib/import_js/command_line_editor.rb', line 13 def current_word @word end |
#cursor ⇒ Array(Number, Number)
Get the cursor position.
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.
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.
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 |
#goto ⇒ String
28 29 30 |
# File 'lib/import_js/command_line_editor.rb', line 28 def goto @goto end |
#max_line_length ⇒ Number?
Get the preferred max length of a line.
121 122 123 |
# File 'lib/import_js/command_line_editor.rb', line 121 def max_line_length 80 end |
#message(str) ⇒ Object
33 34 35 |
# File 'lib/import_js/command_line_editor.rb', line 33 def (str) @messages << str end |
#messages ⇒ String
48 49 50 |
# File 'lib/import_js/command_line_editor.rb', line 48 def @messages.join("\n") end |
#open_file(file_path) ⇒ Object
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_file ⇒ 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.
56 57 58 |
# File 'lib/import_js/command_line_editor.rb', line 56 def read_line(line_number) @lines[line_number - 1] end |
#tab ⇒ String
Returns 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 |