Class: ImportJS::CommandLineEditor
- Inherits:
-
Object
- Object
- ImportJS::CommandLineEditor
- Defined in:
- lib/import_js/command_line_editor.rb
Overview
This is the implementation of command line integration in Import-JS.
Instance Attribute Summary collapse
-
#ask_for_selections ⇒ Object
readonly
Returns the value of attribute ask_for_selections.
-
#goto ⇒ Object
readonly
Returns the value of attribute goto.
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.
-
#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.
-
#initialize(lines, opts) ⇒ CommandLineEditor
constructor
A new instance of CommandLineEditor.
- #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.
Constructor Details
#initialize(lines, opts) ⇒ CommandLineEditor
Returns a new instance of CommandLineEditor.
4 5 6 7 8 9 10 11 |
# File 'lib/import_js/command_line_editor.rb', line 4 def initialize(lines, opts) @lines = lines @messages = [] @ask_for_selections = [] @selections = opts[:selections] unless opts[:selections].empty? @word = opts[:word] @path_to_file = opts[:path_to_file] end |
Instance Attribute Details
#ask_for_selections ⇒ Object (readonly)
Returns the value of attribute ask_for_selections.
35 36 37 |
# File 'lib/import_js/command_line_editor.rb', line 35 def ask_for_selections @ask_for_selections end |
#goto ⇒ Object (readonly)
Returns the value of attribute goto.
28 29 30 |
# File 'lib/import_js/command_line_editor.rb', line 28 def goto @goto 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).
84 85 86 |
# File 'lib/import_js/command_line_editor.rb', line 84 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.
101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/import_js/command_line_editor.rb', line 101 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 |
#count_lines ⇒ Number
Count the number of lines in the file.
91 92 93 |
# File 'lib/import_js/command_line_editor.rb', line 91 def count_lines @lines.length end |
#current_file_content ⇒ String
38 39 40 |
# File 'lib/import_js/command_line_editor.rb', line 38 def current_file_content @lines.join("\n") end |
#current_word ⇒ String
14 15 16 |
# File 'lib/import_js/command_line_editor.rb', line 14 def current_word @word end |
#cursor ⇒ Array(Number, Number)
Get the cursor position.
58 59 60 61 |
# File 'lib/import_js/command_line_editor.rb', line 58 def cursor # not relevant for this editor [0, 0] end |
#cursor=(_position_tuple) ⇒ Object
Place the cursor at a specified position.
67 68 69 |
# File 'lib/import_js/command_line_editor.rb', line 67 def cursor=(_position_tuple) # no-op end |
#delete_line(line_number) ⇒ Object
Delete a line.
75 76 77 |
# File 'lib/import_js/command_line_editor.rb', line 75 def delete_line(line_number) @lines.delete_at(line_number - 1) end |
#message(str) ⇒ Object
31 32 33 |
# File 'lib/import_js/command_line_editor.rb', line 31 def (str) @messages << str end |
#messages ⇒ String
43 44 45 |
# File 'lib/import_js/command_line_editor.rb', line 43 def @messages.join("\n") end |
#open_file(file_path) ⇒ Object
24 25 26 |
# File 'lib/import_js/command_line_editor.rb', line 24 def open_file(file_path) @goto = file_path end |
#path_to_current_file ⇒ String?
19 20 21 |
# File 'lib/import_js/command_line_editor.rb', line 19 def path_to_current_file @path_to_file 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.
51 52 53 |
# File 'lib/import_js/command_line_editor.rb', line 51 def read_line(line_number) @lines[line_number - 1] end |