Module: DealUtils

Included in:
Deal::Command, Deal::DealRule, Deal::Text
Defined in:
lib/deal/utils/utils.rb

Instance Method Summary collapse

Instance Method Details

#logC(text) ⇒ Object



102
103
104
# File 'lib/deal/utils/utils.rb', line 102

def logC(text)
   logInner '34',"[COMMAND] #{text}"
end

#logE(text) ⇒ Object

打印不同级别的 log。根据级别不同,样式(颜色)不同



90
91
92
# File 'lib/deal/utils/utils.rb', line 90

def logE(text)
   logInner '31',"[ERROR] !!#{text}"
end

#logInner(color_code, text) ⇒ Object



83
84
85
86
87
# File 'lib/deal/utils/utils.rb', line 83

def logInner(color_code,text)
   clolr="\033[#{color_code}m"
   nc="\033[0m"
   puts "#{clolr}#{text}#{nc}"
end

#logN(text) ⇒ Object



98
99
100
# File 'lib/deal/utils/utils.rb', line 98

def logN(text)
   logInner '32',"[NOTE] #{text}"
end

#logW(text) ⇒ Object



94
95
96
# File 'lib/deal/utils/utils.rb', line 94

def logW(text)
   logInner '33',"[WARNING] #{text}"
end


79
80
81
# File 'lib/deal/utils/utils.rb', line 79

def print_console(str)
    run_shell "echo #{str}"
end

#processObject



4
5
6
7
8
9
10
11
12
# File 'lib/deal/utils/utils.rb', line 4

def process
    startTime = Time.new
    if block_given?
        yield
    end
    endTime = Time.new
    logN "执行耗时:#{format("%.2f",(endTime-startTime)*1000).to_f}ms"

end

#run_shell(command, force = false, slient = false, retryCount = 0) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
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
70
71
72
73
74
75
76
77
# File 'lib/deal/utils/utils.rb', line 18

def run_shell(command,force = false,slient = false,retryCount = 0 )
     # if not slient
     #     logC command
     # end
     if slient<2
         logC command
     end
     stdin, stdout, stderr,wait_thr = Open3.popen3(command)
    pid = wait_thr[:pid]
    out_lines = []
    error_lines = []
    stdout.sync = true
    out = Thread.new do
        # while !stdout.eof?
        #     c = stdout.getc
        #     putc c
        # end
        stdout.each do |line|
            out_lines.push line
            if not slient
                logN line
            end
        end
    end
    err = Thread.new do
        stderr.each do |line|
            error_lines.push line
            if not slient
                logW line
            end
        end
    end
    out.join
    err.join

    stderr.close
    stdin.close
    stdout.close
    status = wait_thr.value
    if status.exitstatus != 0
        if !force
            if retryCount > 0
                sleep 1
                run_shell command,force,slient,retryCount-1
                return
            else
                if slient
                    logE error_lines
                end
                exit 1
            end
        end
    end
    unless block_given?

        return  [out_lines,error_lines,status.exitstatus]
    else
        yield out_lines, error_lines,status.exitstatus
    end
end

#set_progress(index, char = '*') ⇒ Object



14
15
16
# File 'lib/deal/utils/utils.rb', line 14

def set_progress(index, char = '*')
    print (char * (index / 2.5).floor).ljust(40, " "), " #{index}%\r"
end