lib/act/command.rb in act-0.0.1 vs lib/act/command.rb in act-0.0.2

- old
+ new

@@ -2,73 +2,108 @@ require 'claide' require 'active_support/core_ext/string/strip' module Act class Command < CLAide::Command - self.command = 'act' self.description = 'Act the command line tool to act on files' def self.options [ - ['--open', "Open the file in $EDITOR instead of printing it"], - ['--no-line-numbers', "Show output without line numbers"], + ['--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 def self.run(argv) argv = CLAide::ARGV.new(argv) if argv.flag?('version') - puts VERSION + UI.puts VERSION exit 0 end super(argv) end def initialize(argv) @open = argv.flag?('open') @number_lines = argv.flag?('line-numbers', true) - @file = argv.shift_argument + @file_string = argv.shift_argument super end def validate! super - help! "A file is required." unless @file + help! 'A file is required.' unless @file_string end + CONTEXT_LINES = 5 + def run - file_information = @file.split(':') - path = file_information[0] - line = file_information[1] - context_lines = 5 + clean_file_string = pre_process_file_string(@file_string) + file = ArgumentParser.parse_file_information(clean_file_string, CONTEXT_LINES) - if @open - command = Helper.open_in_editor_command(path, line) - system(command) + 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 - string = File.read(path) if File.exists?(path) + UI.warn '[!] File not found' + end + end - if string - if line - line = line.to_i - start_line = Helper.start_line(string, line, context_lines) - end_line = Helper.end_line(string, line, context_lines) - string = Helper.select_lines(string, start_line, end_line) - puts "Showing from line #{start_line} to #{end_line}" - end + # @return [String] + # + def pre_process_file_string(string) + string.sub(/https?:\/\//, '') + end - string = Helper.strip_indentation(string) - string = Helper.syntax_highlith(string, path) if self.ansi_output? - string = Helper.add_line_numbers(string, start_line, line) if @number_lines - - puts - puts string - else - puts "[!] File not found" + # @return [String, Nil] + # + 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 + # @return [void] + # + 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 + + # @return [void] + # + 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 end end