-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathbmk_progress.bmx
More file actions
132 lines (107 loc) · 2.88 KB
/
Copy pathbmk_progress.bmx
File metadata and controls
132 lines (107 loc) · 2.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
SuperStrict
Import BRL.StandardIO
Import "bmk_config.bmx"
?win32
Extern "win32"
Function GetStdHandle:Byte Ptr(nStdHandle:Int)="HANDLE __stdcall GetStdHandle(DWORD)!"
Function GetConsoleMode:Int(hConsoleHandle:Byte Ptr, lpMode:Int Ptr)="WINBOOL __stdcall GetConsoleMode(HANDLE, LPDWORD)!"
Function GetFileType:Int(hFile:Byte Ptr)="DWORD __stdcall GetFileType(HANDLE)!"
End Extern
Const STD_OUTPUT_HANDLE:Int = -11
Const FILE_TYPE_DISK:Int = 1
Global _ProgressLineIsConsole:Int
?
?Not win32
Extern
Function isatty:Int(fd:Int)
End Extern
?
Function IsInteractiveStdout:Int()
If opt_no_progress Then
Return False
End If
' Disable for certain environments
Local term:String = GetEnv_("TERM")
If term.ToLower() = "dumb" Then
Return False
End If
If GetEnv_("CI") <> "" Then
Return False
End If
?win32
Local h:Byte Ptr = GetStdHandle(STD_OUTPUT_HANDLE)
Local mode:Int
If h And GetConsoleMode(h, Varptr mode) <> 0 Then
_ProgressLineIsConsole = True
Return True
End If
Local ft:Int = GetFileType(h)
If ft = FILE_TYPE_DISK Then
Return False
End If
' If TERM is set and stdout isn't disk, assume interactive.
If term <> "" Then
Return True
End If
?
?Not win32
Return isatty(1)
?
End Function
Global _UseProgressLine:Int = False
Const ESC:String = Chr(27)
Function InitProgressLine()
_UseProgressLine = IsInteractiveStdout()
EnableAnsiIfPossible()
End Function
Function UpdateProgressLine(pct:Int, path:String)
If Not _UseProgressLine Then Return
Local file:String = StripDir(path)
Local line:String = BuildProgressBarLine(pct, file)
' Clear + rewrite
WriteStdout("~r" + ESC + "[2K" + line)
End Function
Function ClearProgressLine()
If Not _UseProgressLine Then Return
WriteStdout("~r" + ESC + "[2K")
End Function
Function BuildProgressBarLine:String(pct:Int, file:String)
Local barW:Int = 30
If pct < 0 Then
pct = 0
Else If pct > 100 Then
pct = 100
End If
Local filled:Int = (pct * barW) / 100
Local bar:String = "[" + "#".Replicate(filled) + "-".Replicate(barW - filled) + "]"
' Example: "[########------] 72% file.c"
Return bar + " " + pct + "% " + file
End Function
?win32
Extern "win32"
Function SetConsoleMode:Int(hConsoleHandle:Byte Ptr, dwMode:Int)="WINBOOL __stdcall SetConsoleMode(HANDLE, DWORD)!"
End Extern
Const ENABLE_VIRTUAL_TERMINAL_PROCESSING:Int = $0004
Function EnableAnsiIfPossible()
If Not _UseProgressLine Then Return
If _ProgressLineIsConsole Then
Local h:Byte Ptr = GetStdHandle(STD_OUTPUT_HANDLE)
Local mode:Int
If GetConsoleMode(h, Varptr mode) = 0 Then Return
' Try to add VT processing
SetConsoleMode(h, mode | ENABLE_VIRTUAL_TERMINAL_PROCESSING)
End If
End Function
?
?Not win32
Function EnableAnsiIfPossible()
' nothing needed
End Function
?
Function LogLine(s:String)
If _UseProgressLine Then
' Return to start of bar line and clear it
WriteStdout("~r" + ESC + "[2K")
EndIf
Print s
End Function