Method: Blufin::Terminal.command

Defined in:
lib/core/terminal.rb

.command(commands, location = nil, verbose = true, verbose_cd = true, capture_real_output = false, pbl: false, tbl: false) ⇒ Object

Runs a series of commands in the terminal.

Returns:

  • void


29
30
31
32
33
34
35
36
37
38
39
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
# File 'lib/core/terminal.rb', line 29

def self.command(commands, location = nil, verbose = true, verbose_cd = true, capture_real_output = false, pbl: false, tbl: false)
    unless commands.is_a?(Array)
        commands = [commands]
    end
    unless location.nil?
        unless File.directory?(File.expand_path(location))
            error('Directory not found.', "Cannot find the following directory: \x1B[38;5;205m#{location}\x1B[0m", true)
        end
    end
    output = []
    if verbose_cd && verbose && commands.any? && !location.nil?
        puts "\x1B[38;5;231m\x1B[48;5;22m Executing \x1B[0m \x1B[0m\xe2\x86\x92\x1B[0m #{Blufin::Terminal::format_command("cd #{location}")}\n"
    end
    if commands.any?
        commands.each do |command|
            if verbose
                puts "\x1B[38;5;231m\x1B[48;5;22m Executing \x1B[0m \x1B[0m\xe2\x86\x92\x1B[0m #{Blufin::Terminal::format_command("#{command}")}\n"
                puts if pbl
            end

            if location.nil?
                if capture_real_output
                    output << `#{command}`
                else
                    output << system("#{command}")
                end
            else
                if capture_real_output
                    output << `cd #{location} && #{command}`
                else
                    output << system("cd #{location} && #{command}")
                end
            end
        end
    end
    if capture_real_output
        output.map! { |v| v.gsub('\n', "\n") }
    end
    puts if tbl
    output
end