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
- #cat_string(string, file = nil) ⇒ Object
- #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.
39 40 41 42 43 44 45 46 47 48 |
# File 'lib/act/command.rb', line 39 def initialize(argv) @stdin = STDIN.read unless STDIN.tty? @open = argv.flag?('open') @prettify = argv.flag?('prettify', false) @number_lines = argv.flag?('line-numbers', false) @lexer = argv.option('lexer', false) @always_color = argv.flag?('always-color') @file_string = argv.shift_argument super end |
Class Method Details
.arguments ⇒ Object
16 17 18 19 20 |
# File 'lib/act/command.rb', line 16 def self.arguments [ CLAide::Argument.new('PATH', false), ] end |
.completion_description ⇒ Object
32 33 34 35 36 37 |
# File 'lib/act/command.rb', line 32 def self.completion_description description = super # _path_files function description[:paths] = :all_files description end |
.options ⇒ Object
22 23 24 25 26 27 28 29 30 |
# File 'lib/act/command.rb', line 22 def self. [ ['--open', 'Open the file in $EDITOR instead of printing it'], ['--prettify', 'Prettify output'], ['--always-color', 'Always color the output'], ['--line-numbers', 'Show output with line numbers'], ['--lexer=NAME', 'Use the given lexer'], ].concat(super) end |
Instance Method Details
#cat_file(file) ⇒ void
This method returns an undefined value.
124 125 126 127 128 129 130 |
# File 'lib/act/command.rb', line 124 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 cat_string(string, file) end |
#cat_string(string, file = nil) ⇒ Object
132 133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/act/command.rb', line 132 def cat_string(string, file = nil) if string path = file.path if file @lexer ||= Helper.lexer(path, string) string = Helper.strip_indentation(string) string = Helper.prettify(string, @lexer) if @prettify string = Helper.syntax_highlight(string, @lexer) if ansi_output? || @always_color string = Helper.add_line_numbers(string, file.from_line, file.highlight_line) if @number_lines && file UI.puts UI.tty? ? "\n#{string}" : string else UI.warn '[!] Nothing to show' end end |
#infer_local_path(path) ⇒ String, Nil
96 97 98 99 100 101 102 103 104 105 |
# File 'lib/act/command.rb', line 96 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.
109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/act/command.rb', line 109 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? if defined? Bundler Bundler.with_clean_env do system(command) end else system(command) end end |
#pre_process_file_string(string) ⇒ String
90 91 92 |
# File 'lib/act/command.rb', line 90 def pre_process_file_string(string) string.sub(/https?:\/\//, '') end |
#run ⇒ Object
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/act/command.rb', line 57 def run if @stdin && !@open cat_string(@stdin) return end @file_string ||= '.' 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) if inferred file.path = inferred path_exists = true end end if @open open_file(file) else if path_exists cat_file(file) else UI.warn '[!] File not found' end end end |
#validate! ⇒ Object
50 51 52 53 |
# File 'lib/act/command.rb', line 50 def validate! super help! 'A file is required.' unless @file_string || @open || @stdin end |