Skip to content

Commit 5abb190

Browse files
committed
fixes issues with rendering progress, added --progress option (off by default)
1 parent ee4463e commit 5abb190

File tree

5 files changed

+40
-28
lines changed

5 files changed

+40
-28
lines changed

core/ExecCore.fs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ module internal ExecCore =
238238
true
239239
<| target
240240

241-
let progressSink = Progress.openProgress (getDurationDeps ctx getDeps) options.Threads targets
241+
let progressSink = Progress.openProgress (getDurationDeps ctx getDeps) options.Threads targets options.Progress
242242
let stepCtx = {ctx with NeedRebuild = check_rebuild; Progress = progressSink}
243243

244244
try
@@ -251,7 +251,7 @@ module internal ExecCore =
251251
List.map (makeTarget ctx) >> (runTargets ctx options)
252252
)
253253
// some long text (looks awkward) to remove progress message. I do not think it worth spending another half an hour to design proper solution
254-
ctx.Logger.Log Message " \n\n\tBuild completed in %A\n" (System.DateTime.Now - start)
254+
ctx.Logger.Log Message " \n\n Build completed in %A\n" (System.DateTime.Now - start)
255255
with
256256
| exn ->
257257
let th = if options.FailOnError then raiseError else reportError

core/ExecTypes.fs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ type ExecOptions = {
4141

4242
/// Dump dependencies only
4343
DumpDeps: bool
44+
45+
/// Dump dependencies only
46+
Progress: bool
4447
} with
4548
static member Default =
4649
{
@@ -59,6 +62,7 @@ static member Default =
5962
DbFileName = ".xake"
6063
DryRun = false
6164
DumpDeps = false
65+
Progress = false
6266
}
6367
end
6468

core/Logging.fs

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -113,25 +113,26 @@ module private ConsoleSink =
113113
| Verbose -> Some (ConsoleColor.Magenta, ConsoleColor.DarkMagenta)
114114
| _ -> None
115115

116+
let fmtTs (ts:System.TimeSpan) =
117+
(if ts.TotalHours >= 1.0 then "h'h'\ mm'm'\ ss's'"
118+
else if ts.TotalMinutes >= 1.0 then "mm'm'\ ss's'"
119+
else "'0m 'ss's'")
120+
|> ts.ToString
121+
122+
let po = MailboxProcessor.Start(fun mbox ->
116123

117-
let po = MailboxProcessor.Start(fun mbox ->
118-
let fmtTs (ts:System.TimeSpan) =
119-
(if ts.TotalHours >= 1.0 then "h'h'\ mm'm'\ ss's'"
120-
else if ts.TotalMinutes >= 1.0 then "mm'm'\ ss's'"
121-
else "'0m 'ss's'")
122-
|> ts.ToString
123124
let rec loop (progressMessage) =
124-
let wipe pos =
125-
match progressMessage with
126-
| None -> ()
127-
| Some x ->
128-
let extraChars = (String.length x) - pos
129-
System.Console.Write (if extraChars > 0 then (String.replicate extraChars " ") else "")
125+
let wipeProgressMessage () =
126+
let len = progressMessage |> Option.fold (fun _ -> String.length) 0
127+
// printfn "cleft: %A len: %d" Console.CursorLeft len
128+
match len - Console.CursorLeft with
129+
| e when e > 0 -> System.Console.Write (String.replicate e " ")
130+
| _ -> ()
130131
let renderProgress = function
131132
| Some (outputString: string) ->
132133
Console.ForegroundColor <- ConsoleColor.White
133134
Console.Write outputString
134-
wipe outputString.Length
135+
wipeProgressMessage()
135136

136137
Console.ResetColor()
137138
| None -> ()
@@ -141,17 +142,17 @@ module private ConsoleSink =
141142
match msg with
142143
| Message(level, text) ->
143144
match level |> levelToColor with
144-
| Some (color, text_color) ->
145-
let levelStr = sprintf "[%s] " (LevelToString level)
145+
| Some (color, textColor) ->
146+
// in case of CRLF in the string make sure we washed out the progress message
146147

147148
Console.ForegroundColor <- color
148-
Console.Write levelStr
149+
Console.Write (sprintf "\r[%s] " (LevelToString level))
149150

150-
Console.ForegroundColor <- text_color
151+
Console.ForegroundColor <- textColor
151152
text |> System.Console.Write
152153

153-
wipe (levelStr.Length + text.Length)
154-
System.Console.WriteLine ""
154+
wipeProgressMessage()
155+
System.Console.WriteLine()
155156
renderProgress progressMessage
156157

157158
| _ -> ()
@@ -163,13 +164,14 @@ module private ConsoleSink =
163164
| a when a >= 100 || a < 0 || timeLeft.TotalMilliseconds < 100.0 -> ""
164165
| pct ->
165166
let barlen = pct * ProgressBarLen / 100
166-
sprintf "%3d%% [%s%s] %s\r" pct (String.replicate barlen "=") (String.replicate (ProgressBarLen - barlen) "-") (fmtTs timeLeft)
167+
sprintf "\r%3d%% Complete [%s%s] %s Left" pct (String.replicate barlen "=") (String.replicate (ProgressBarLen - barlen) " ") (fmtTs timeLeft)
168+
|> Some
167169

168-
renderProgress (Some outputString)
169-
return! loop (Some outputString)
170+
renderProgress outputString
171+
return! loop outputString
170172

171173
| Flush ch ->
172-
wipe 0
174+
wipeProgressMessage()
173175
Console.Write "\r"
174176
ch.Reply ()
175177
return! loop None
@@ -205,7 +207,9 @@ let ConsoleLogger =
205207

206208
/// Ensures all logs finished pending output.
207209
let FlushLogs () =
208-
ConsoleSink.po.PostAndReply (ConsoleSink.Flush, 100) |> ignore
210+
try
211+
ConsoleSink.po.PostAndReply (ConsoleSink.Flush, 200) |> ignore
212+
with _ -> ()
209213

210214
/// Draws a progress bar to console log.
211215
let WriteConsoleProgress =

core/Program.fs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ Options:
3434
target1;target2;..targetN -- execute the targets simultaneously
3535
-d <name>=<value> - defines a script variable value
3636
--dryrun - defines a script variable value
37+
--progress, -p - display progress indicator
3738
3839
"""
3940
exit(0)
@@ -55,6 +56,8 @@ Options:
5556
({optionsSoFar with DryRun = true}, TopLevel)
5657
| "--dump" ->
5758
({optionsSoFar with DumpDeps = true}, TopLevel)
59+
| "--progress" | "-p" ->
60+
({optionsSoFar with Progress = true}, TopLevel)
5861

5962
| x when x.StartsWith("-") || x.StartsWith("/") ->
6063
printfn "Option '%s' is unrecognized" x

core/Progress.fs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ let estimateEndTime getDurationDeps threadCount groups =
189189
/// <param name="getDurationDeps">gets the dependency duration in ms</param>
190190
/// <param name="threadCount"></param>
191191
/// <param name="goals"></param>
192-
let openProgress getDurationDeps threadCount goals =
192+
let openProgress getDurationDeps threadCount goals toConsole =
193193

194194
let progressBar = WindowsProgress.createTaskbarIndicator() |> Impl.ignoreFailures
195195
let machine_state = {Cpu = BusyUntil 0<ms> |> List.replicate threadCount; Tasks = Map.empty}
@@ -217,7 +217,8 @@ let openProgress getDurationDeps threadCount goals =
217217
let percentDone = timePassed * 100 / (timePassed + leftTime) |> int
218218
let progressData = System.TimeSpan.FromMilliseconds (leftTime/1<ms> |> float), percentDone
219219
do ProgressMessage.Progress progressData |> progressBar
220-
do WriteConsoleProgress progressData
220+
if toConsole then
221+
do WriteConsoleProgress progressData
221222

222223
let updTime = ref System.DateTime.Now
223224
let advanceRunningTime rt =

0 commit comments

Comments
 (0)