Class: Act::Command

Inherits:
CLAide::Command
  • Object
show all
Defined in:
lib/act/command.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(argv) ⇒ Command

Returns a new instance of Command.



28
29
30
31
32
33
# File 'lib/act/command.rb', line 28

def initialize(argv)
  @open = argv.flag?('open')
  @number_lines = argv.flag?('line-numbers', true)
  @file = argv.shift_argument
  super
end

Class Method Details

.optionsObject



11
12
13
14
15
16
17
# File 'lib/act/command.rb', line 11

def self.options
  [
    ['--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

.run(argv) ⇒ Object



19
20
21
22
23
24
25
26
# File 'lib/act/command.rb', line 19

def self.run(argv)
  argv = CLAide::ARGV.new(argv)
  if argv.flag?('version')
    puts VERSION
    exit 0
  end
  super(argv)
end

Instance Method Details

#runObject



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/act/command.rb', line 40

def run
  file_information = @file.split(':')
  path = file_information[0]
  line = file_information[1]
  context_lines = 5

  if @open
    command = Helper.open_in_editor_command(path, line)
    system(command)
  else
    string = File.read(path) if File.exists?(path)

    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

      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"
    end
  end
end

#validate!Object



35
36
37
38
# File 'lib/act/command.rb', line 35

def validate!
  super
  help! "A file is required." unless @file
end