Improve the progress bar appearance

This commit is contained in:
Andrew Naylor 2015-08-22 02:01:56 +08:00
parent 705a2eb315
commit 83726c76be

View file

@ -6,22 +6,71 @@
// Copyright (c) 2015 Andrew Naylor. All rights reserved.
//
let csi = "\u{001B}["
@objc class DownloadQueueObserver: CKDownloadQueueObserver {
var started = false
func downloadQueue(queue: CKDownloadQueue, statusChangedForDownload download: SSDownload!) {
if let activePhase = download.status.activePhase {
let percentage = String(Int(floor(download.status.percentComplete * 100))) + "%"
// let phase = String(activePhase.phaseType)
print("\(csi)2K\(csi)0G" + percentage)
if !started {
return
}
progress(download.status.progressState)
}
func downloadQueue(queue: CKDownloadQueue, changedWithAddition download: SSDownload!) {
print("Downloading: " + download.metadata.title)
started = true
println("==> Downloading " + download.metadata.title)
}
func downloadQueue(queue: CKDownloadQueue, changedWithRemoval download: SSDownload!) {
print("")
print("Finished: " + download.metadata.title)
println("")
println("==> Installed " + download.metadata.title)
exit(EXIT_SUCCESS)
}
}
struct ProgressState {
let percentComplete: Float
let phase: String
var percentage: String {
return String(format: "%.1f%%", arguments: [floor(percentComplete * 100)])
}
}
func progress(state: ProgressState) {
let barLength = 60
let completeLength = Int(state.percentComplete * Float(barLength))
var bar = ""
for i in 0..<barLength {
if i < completeLength {
bar += "#"
}
else {
bar += "-"
}
}
print("\(csi)2K\(csi)0G\(bar) \(state.percentage) \(state.phase)")
}
extension SSDownloadStatus {
var progressState: ProgressState {
let phase = activePhase?.phaseDescription ?? "Waiting"
return ProgressState(percentComplete: percentComplete, phase: phase)
}
}
extension SSDownloadPhase {
var phaseDescription: String {
switch phaseType {
case 0:
return "Downloading"
case 1:
return "Installing"
default:
return "Waiting"
}
}
}