Class: Act::Command
- Inherits:
-
CLAide::Command
- Object
- CLAide::Command
- Act::Command
- Defined in:
- lib/act/command.rb
Constant Summary collapse
- CONTEXT_LINES =
5
Class Method Summary collapse
Instance Method Summary collapse
- #cat_file(file) ⇒ void
- #infer_local_path(path) ⇒ String, Nil
-
#initialize(argv) ⇒ Command
constructor
A new instance of Command.
- #open_file(file) ⇒ void
- #pre_process_file_string(string) ⇒ String
- #run ⇒ Object
- #validate! ⇒ Object
Constructor Details
#initialize(argv) ⇒ Command
Returns a new instance of Command.
27 28 29 30 31 32 |
# File 'lib/act/command.rb', line 27 def initialize(argv) @open = argv.flag?('open') @number_lines = argv.flag?('line-numbers', true) @file_string = argv.shift_argument super end |
Class Method Details
.options ⇒ Object
10 11 12 13 14 15 16 |
# File 'lib/act/command.rb', line 10 def self. [ ['--open', 'Open the file in $EDITOR instead of printing it'], ['--no-line-numbers', 'Show output without line numbers'], ['--version', 'Show the version of Act'], ].concat(super) end |
Instance Method Details
#cat_file(file) ⇒ void
This method returns an undefined value.
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/act/command.rb', line 93 def cat_file(file) string = File.read(file.path) if file.from_line && file.to_line string = Helper.select_lines(string, file.from_line, file.to_line) end if string string = Helper.strip_indentation(string) string = Helper.syntax_highlith(string, file.path) if self.ansi_output? string = Helper.add_line_numbers(string, file.from_line, file.highlight_line) if @number_lines UI.puts "\n#{string}" else UI.warn '[!] Nothing to show' end end |
#infer_local_path(path) ⇒ String, Nil
71 72 73 74 75 76 77 78 79 80 |
# File 'lib/act/command.rb', line 71 def infer_local_path(path) path_components = Pathname(path).each_filename.to_a until path_components.empty? path_components.shift candidate = File.join(path_components) if File.exist?(candidate) return candidate end end end |
#open_file(file) ⇒ void
This method returns an undefined value.
84 85 86 87 88 89 |
# File 'lib/act/command.rb', line 84 def open_file(file) line = file.highlight_line || file.from_line command = Helper.open_in_editor_command(file.path, line) UI.puts command if self.verbose? system(command) end |
#pre_process_file_string(string) ⇒ String
65 66 67 |
# File 'lib/act/command.rb', line 65 def pre_process_file_string(string) string.sub(/https?:\/\//, '') end |
#run ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/act/command.rb', line 41 def run clean_file_string = pre_process_file_string(@file_string) file = ArgumentParser.parse_file_information(clean_file_string, CONTEXT_LINES) path_exists = File.exist?(file.path) unless path_exists inferred = infer_local_path(file.path) file.path = inferred path_exists = true if inferred end if path_exists if @open open_file(file) else cat_file(file) end else UI.warn '[!] File not found' end end |
#validate! ⇒ Object
34 35 36 37 |
# File 'lib/act/command.rb', line 34 def validate! super help! 'A file is required.' unless @file_string end |