Skip to content

Commit

Permalink
Merge pull request #3 from yutailang0119/develop
Browse files Browse the repository at this point in the history
Update Swift to 5.0
  • Loading branch information
yutailang0119 authored Jun 21, 2019
2 parents b715485 + 1feffb9 commit dbaa8a0
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 36 deletions.
15 changes: 12 additions & 3 deletions Package.resolved
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
{
"object": {
"pins": [
{
"package": "llbuild",
"repositoryURL": "https://github.com/apple/swift-llbuild.git",
"state": {
"branch": "master",
"revision": "3aeecb428d202afe15633266dc862de27feab723",
"version": null
}
},
{
"package": "SwiftPM",
"repositoryURL": "https://github.com/apple/swift-package-manager.git",
"state": {
"branch": null,
"revision": "6983434787dec4e543e9d398a0a9acf63ccd4da1",
"version": "0.2.1"
"branch": "swift-5.0.1-RELEASE",
"revision": "3a57975e10be0b1a8b87992ddf3a49701036f96c",
"version": null
}
}
]
Expand Down
6 changes: 3 additions & 3 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// swift-tools-version:4.0
// swift-tools-version:5.0
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription
Expand All @@ -9,10 +9,10 @@ let package = Package(
.library(name: "ProgressSpinnerKit", targets: ["ProgressSpinnerKit"]),
],
dependencies: [
.package(url: "https://github.com/apple/swift-package-manager.git", from: "0.2.1"),
.package(url: "https://github.com/apple/swift-package-manager.git", .revision("swift-5.0.1-RELEASE")),
],
targets: [
.target(name: "ProgressSpinnerKit", dependencies: ["Utility"]),
.target(name: "ProgressSpinnerKit", dependencies: ["SPMUtility"]),
.testTarget(name: "ProgressSpinnerKitTests", dependencies: ["ProgressSpinnerKit"]),
.target(name: "Demo", dependencies: ["ProgressSpinnerKit"]),
]
Expand Down
40 changes: 25 additions & 15 deletions Sources/ProgressSpinnerKit/ProgressSpinner.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import Foundation
import Basic
import Utility
import SPMUtility

private var fps: useconds_t {
let fps: Double = 1 / 60
Expand Down Expand Up @@ -56,7 +56,10 @@ final class SingleLineProgressSpinnar: ProgressSpinnable {
self.stream <<< "Start..."
self.stream <<< "\n"

queue.async { [unowned self] in
queue.async { [weak self] in
guard let self = self else {
return
}
while self.isProgressing {
self.stream <<< self.spinner.frame
self.stream <<< "\n"
Expand Down Expand Up @@ -108,7 +111,10 @@ final class SimpleProgressSpinner: ProgressSpinnable {
isClear = false
}

queue.async { [unowned self] in
queue.async { [weak self] in
guard let self = self else {
return
}
while self.isProgressing {
self.stream <<< self.spinner.frame
self.stream <<< "\n"
Expand Down Expand Up @@ -155,15 +161,18 @@ final class ProgressSpinner: ProgressSpinnable {
isProgressing = true

queue.async { [weak self] in
while self?.isProgressing ?? false {
self?.term.clearLine()
self?.term.write(self?.header ?? "", inColor: .green, bold: true)
self?.term.write(self?.spinner.frame ?? "", inColor: .green)
self?.term.endLine()
guard let self = self else {
return
}
while self.isProgressing {
self.term.clearLine()
self.term.write(self.header, inColor: .green, bold: true)
self.term.write(self.spinner.frame, inColor: .green)
self.term.endLine()

self?.term.moveCursor(up: 1)
self.term.moveCursor(up: 1)

usleep(self?.sleepInterval ?? 0)
usleep(self.sleepInterval)
}
}

Expand All @@ -181,9 +190,10 @@ final class ProgressSpinner: ProgressSpinnable {
}

/// Creates colored or simple progress spinner based on the provided output stream.
public func createProgressSpinner(forStream stream: OutputByteStream, header: String, isShowStopped: Bool = true, spinner: Spinner = Spinner(kind: .box1)) -> ProgressSpinnable {
guard let stdStream = stream as? LocalFileOutputByteStream else {
return SimpleProgressSpinner(stream: stream, header: header, isShowStopped: isShowStopped, spinner: spinner)
public func createProgressSpinner(forStream stderrStream: ThreadSafeOutputByteStream, header: String, isShowStopped: Bool = true, spinner: Spinner = Spinner(kind: .box1)) -> ProgressSpinnable {

guard let stdStream = stderrStream.stream as? LocalFileOutputByteStream else {
return SimpleProgressSpinner(stream: stderrStream.stream, header: header, isShowStopped: isShowStopped, spinner: spinner)
}

// If we have a terminal, use animated progress spinener.
Expand All @@ -193,9 +203,9 @@ public func createProgressSpinner(forStream stream: OutputByteStream, header: St

// If the terminal is dumb, use single line progress spinner.
if TerminalController.terminalType(stdStream) == .dumb {
return SingleLineProgressSpinnar(stream: stream, header: header, isShowStopped: isShowStopped, spinner: spinner)
return SingleLineProgressSpinnar(stream: stderrStream.stream, header: header, isShowStopped: isShowStopped, spinner: spinner)
}

// Use simple progress spinner by default.
return SimpleProgressSpinner(stream: stream, header: header, isShowStopped: isShowStopped, spinner: spinner)
return SimpleProgressSpinner(stream: stderrStream.stream, header: header, isShowStopped: isShowStopped, spinner: spinner)
}
2 changes: 1 addition & 1 deletion Sources/ProgressSpinnerKit/Spinner.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public struct Spinner {
cursor = 0
}

public enum Kind {
public enum Kind: CaseIterable {
case box1
case box2
case box3
Expand Down
26 changes: 12 additions & 14 deletions Tests/ProgressSpinnerKitTests/ProgressSpinnerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@ final class ProgressSpinnerTests: XCTestCase {
/// Test progress bar when writing to a non tty stream.
func testSimpleProgresSpinner() {
let outStream = BufferedOutputByteStream()
let spinner = Spinner(kind: oneOf(.box1, .bar1, .spin1))
let isShowStopped = oneOf(true, false)
let progressSpinner = ProgressSpinnerKit.createProgressSpinner(forStream: outStream, header: "test", isShowStopped: isShowStopped, spinner: spinner)
let spinner = Spinner(kind: Spinner.Kind.allCases.randomElement()!)
let isShowStopped = Bool.random()
let headerText = "test"
let progressSpinner = ProgressSpinnerKit.createProgressSpinner(forStream: outStream, header: headerText, isShowStopped: isShowStopped, spinner: spinner)
XCTAssertTrue(progressSpinner is SimpleProgressSpinner)

let second = arc4random_uniform(10)
Expand All @@ -33,10 +34,10 @@ final class ProgressSpinnerTests: XCTestCase {
let frameCount = Int(ceil(Double(duration) / Double(fps * 100)))
var verificationSpinner = spinner
let verificationSuffix = isShowStopped ? "Stop\n" : ""
let verificationFrames = (0..<frameCount).reduce(into: "test\n") { result, _ in
let verificationFrames = (0..<frameCount).reduce(into: "\(headerText)\n") { result, _ in
result += "\(verificationSpinner.frame)\n"
} + verificationSuffix
XCTAssertEqual(outStream.bytes.asString, verificationFrames)
XCTAssertEqual(outStream.bytes.validDescription, verificationFrames)
}

/// Test progress bar when writing a tty stream.
Expand All @@ -45,9 +46,10 @@ final class ProgressSpinnerTests: XCTestCase {
XCTFail("Couldn't create pseudo terminal.")
return
}
let spinner = Spinner(kind: oneOf(.box1, .bar1, .spin1))
let isShowStopped = oneOf(true, false)
let progressSpinner = ProgressSpinnerKit.createProgressSpinner(forStream: pty.outStream, header: "TestHeader", isShowStopped: isShowStopped, spinner: spinner)
let spinner = Spinner(kind: Spinner.Kind.allCases.randomElement()!)
let isShowStopped = Bool.random()
let headerText = "TestHeader"
let progressSpinner = ProgressSpinnerKit.createProgressSpinner(forStream: pty.outStream, header: headerText, isShowStopped: isShowStopped, spinner: spinner)
XCTAssertTrue(progressSpinner is ProgressSpinner)

var output = ""
Expand All @@ -65,7 +67,7 @@ final class ProgressSpinnerTests: XCTestCase {
thread.join()
pty.closeMaster()

let chuzzledOutput = output.chuzzle()!
let chuzzledOutput = output.spm_chuzzle()!
let prefix = "\u{1B}[2K"
XCTAssertTrue(chuzzledOutput.hasPrefix(prefix))
let suffix = isShowStopped ? "\u{1B}[32m\u{1B}[1mStop\u{1B}[0m" : ""
Expand All @@ -77,7 +79,7 @@ final class ProgressSpinnerTests: XCTestCase {

var verificationSpinner = spinner
let verificationFrames = (0..<outputFrames.count).map { _ in
return "\u{1B}[32m\u{1B}[1mTestHeader\u{1B}[0m\u{1B}[32m\(verificationSpinner.frame)\u{1B}[0m"
return "\u{1B}[32m\u{1B}[1m\(headerText)\u{1B}[0m\u{1B}[32m\(verificationSpinner.frame)\u{1B}[0m"
}
XCTAssertEqual(outputFrames, verificationFrames)

Expand All @@ -96,10 +98,6 @@ final class ProgressSpinnerTests: XCTestCase {

}

private func oneOf<T: Any>(_ items: T...) -> T {
return items[Int(arc4random_uniform(UInt32(items.count)))]
}

private final class PseudoTerminal {
let master: Int32
let slave: Int32
Expand Down

0 comments on commit dbaa8a0

Please sign in to comment.