Module: GitThinUtils

Constant Summary collapse

LOGC =
1<<0
LOGN =
1<<1
LOGPRUNE =
1<<2
LOGNone =
0
LOGA =
LOGC|LOGN

Instance Method Summary collapse

Instance Method Details

#logC(text, note = true) ⇒ Object



126
127
128
129
130
131
132
# File 'lib/git-thin/utils/utils.rb', line 126

def logC(text,note = true)
    if note
        logInner '34',"[COMMAND] #{text}"
    else
        print(text)
    end
end

#logE(text, note = true) ⇒ Object

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



97
98
99
100
101
102
103
# File 'lib/git-thin/utils/utils.rb', line 97

def logE(text,note = true )
    if note
        logInner '31',"[ERROR] !!#{text}"
    else
        print(text)
    end
end

#logInner(color_code, text) ⇒ Object



90
91
92
93
94
# File 'lib/git-thin/utils/utils.rb', line 90

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

#logN(text, note = true) ⇒ Object



118
119
120
121
122
123
124
# File 'lib/git-thin/utils/utils.rb', line 118

def logN(text,note = true)
    if note
        logInner '32',"[NOTE] #{text}"
    else
        print(text)
    end
end

#logP(text) ⇒ Object



105
106
107
108
109
# File 'lib/git-thin/utils/utils.rb', line 105

def logP(text)
    print("\r")
    print(text)
    STDOUT.flush
end

#logW(text, note = true) ⇒ Object



110
111
112
113
114
115
116
# File 'lib/git-thin/utils/utils.rb', line 110

def logW(text,note = true )
    if note
        logInner '33',"[WARNING] #{text}"
    else
        print(text)
    end
end


86
87
88
# File 'lib/git-thin/utils/utils.rb', line 86

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

#run_shell(command, force = 0, log_type = LOGA, retryCount = 0) ⇒ Object



16
17
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
78
79
80
81
82
83
84
# File 'lib/git-thin/utils/utils.rb', line 16

def run_shell(command,force = 0,log_type = LOGA,retryCount = 0 )
     if log_type === false
         log_type = LOGPRUNE
     elsif log_type === true
         log_type = LOGNone
     end
     if (log_type & LOGC ) == LOGC
         logC command+"\n",(log_type & LOGPRUNE )!=LOGPRUNE
     end
     stdin, stdout, stderr,wait_thr = Open3.popen3(command)
    pid = wait_thr[:pid]
    out_lines = []
    error_lines = []
    stdout.sync = true
     stderr.sync = true
     out = Thread.new do
        # while !stdout.eof?
        #     c = stdout.getc
        #     putc c
        #     stdout.flush
        # end
        stdout.each do |line|
            out_lines.push line
            if log_type && log_type > LOGC
                logN line,(log_type & LOGPRUNE )!=LOGPRUNE
            end
        end
    end
    err = Thread.new do
        # while !stderr.eof?
        #     c = stderr.getc
        #     putc c
        #     stderr.flush
        # end
        stderr.each do |line|
            error_lines.push line

            if log_type
                logW line,(log_type & LOGPRUNE )!=LOGPRUNE
            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,log_type,retryCount-1
                return
            else
                logE error_lines,(log_type & LOGPRUNE )

                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



12
13
14
# File 'lib/git-thin/utils/utils.rb', line 12

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