Skip to content

Commit

Permalink
Migrate away from redundant update functions (#36)
Browse files Browse the repository at this point in the history
* removed redundant functions to update spinner properties
  • Loading branch information
dominicegginton authored Sep 26, 2020
1 parent 5782b0e commit b2f13d4
Showing 1 changed file with 31 additions and 55 deletions.
86 changes: 31 additions & 55 deletions Sources/Spinner/Spinner.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public final class Spinner {
self.frameIndex = 0
}
}
/// Text that is displayed next to spinner
/// Text that is displayed next to spinner
var text: String
/// Boolean representing fs the spinner is currently animating
var running: Bool
Expand Down Expand Up @@ -78,14 +78,31 @@ public final class Spinner {
- Parameter terminator: String - The terminator used for ending writing a line to the terminal, default is '\n' this will return the curser to a new line
*/
public func stop(finalFrame: String? = nil, text: String? = nil, color: Color? = nil, terminator: String = "\n") {
self.stopSpinner(finalFrame: finalFrame, text: text, color: color, terminator: terminator)
self.running = false
if let text = text {
self.updateText(text)
}
if let color = color {
self.updateColor(color)
}
var finalPattern: SpinnerPattern?
if let frame = finalFrame {
finalPattern = SpinnerPattern(singleFrame: frame)
}
if let pattern = finalPattern {
self.text += Array(repeating: " ", count: self.getPatternPadding(pattern))
self.pattern = pattern
}
self.unhideCursor()
self.renderSpinner()
print(terminator: terminator)
}

/**
Clears the spinner from the terminal and returns the curser to the start of the spinner
*/
public func clear() {
self.stopSpinner(finalFrame: "", text: "", terminator: "\r")
self.stop(finalFrame: "", text: "", terminator: "\r")
}

/**
Expand All @@ -94,7 +111,8 @@ public final class Spinner {
- Parameter _: SpinnerPattern - New pattern the spinner should animate over
*/
public func updatePattern(_ newPattern: SpinnerPattern) {
self.setPattern(newPattern)
self.format += Array(repeating: " ", count: self.getPatternPadding(newPattern))
self.pattern = newPattern
}

/**
Expand All @@ -103,7 +121,8 @@ public final class Spinner {
- Parameter _: String - New text the spinner should display
*/
public func updateText(_ newText: String) {
self.setText(newText)
self.format += Array(repeating: " ", count: self.getTextPadding(newText))
self.text = newText
}

/**
Expand All @@ -112,7 +131,7 @@ public final class Spinner {
- Parameter _: Double - New speed the spinner should animate at
*/
public func updateSpeed(_ newSpeed: Double) {
self.setSpeed(newSpeed)
self.speed = newSpeed
}

/**
Expand All @@ -121,7 +140,7 @@ public final class Spinner {
- Parameter _: Color - New color for the spinner
*/
public func updateColor(_ newColor: Color) {
self.setColor(newColor)
self.color = newColor
}

/**
Expand All @@ -130,7 +149,7 @@ public final class Spinner {
- Parameter _: String - New format for spinner to display as
*/
public func updateFormat(_ newFormat: String) {
self.setFormat(newFormat)
self.format = newFormat
}

/**
Expand All @@ -139,7 +158,7 @@ public final class Spinner {
- Parameter _: String The persistent text that will be displayed on the completed spinner, default will keep the current text of the spinner
*/
public func succeed(_ text: String? = nil) {
self.stopSpinner(finalFrame: "", text: text, color: .green)
self.stop(finalFrame: "", text: text, color: .green)
}

/**
Expand All @@ -148,7 +167,7 @@ public final class Spinner {
- Parameter _: String The persistent text that will be displayed on the completed spinner
*/
public func failure(_ text: String? = nil) {
self.stopSpinner(finalFrame: "", text: text, color: .red)
self.stop(finalFrame: "", text: text, color: .red)
}

/**
Expand All @@ -157,7 +176,7 @@ public final class Spinner {
- Parameter _: String The persistent text that will be displayed on the completed spinner, default will keep the current text of the spinner
*/
public func warning(_ text: String? = nil) {
self.stopSpinner(finalFrame: "", text: text, color: .yellow)
self.stop(finalFrame: "", text: text, color: .yellow)
}

/**
Expand All @@ -166,50 +185,7 @@ public final class Spinner {
- Parameter _: String The persistent text that will be displayed on the completed spinner, default will keep the current text of the spinner
*/
public func information(_ text: String? = nil) {
self.stopSpinner(finalFrame: "", text: text, color: .blue)
}

func stopSpinner(finalFrame: String? = nil, text: String? = nil, color: Color? = nil, terminator: String = "\n") {
self.running = false
if let text = text {
setText(text)
}
if let color = color {
setColor(color)
}
var finalPattern: SpinnerPattern?
if let frame = finalFrame {
finalPattern = SpinnerPattern(singleFrame: frame)
}
if let pattern = finalPattern {
self.text += Array(repeating: " ", count: self.getPatternPadding(pattern))
self.pattern = pattern
}
self.unhideCursor()
self.renderSpinner()
print(terminator: terminator)
}

func setColor(_ newColor: Color) {
self.color = newColor
}

func setPattern(_ newPattern: SpinnerPattern) {
self.format += Array(repeating: " ", count: self.getPatternPadding(newPattern))
self.pattern = newPattern
}

func setSpeed(_ newSpeed: Double) {
self.speed = newSpeed
}

func setFormat(_ newFormat: String) {
self.format = newFormat
}

func setText(_ newText: String) {
self.format += Array(repeating: " ", count: self.getTextPadding(newText))
self.text = newText
self.stop(finalFrame: "", text: text, color: .blue)
}

func getTextPadding(_ newText: String) -> Int {
Expand Down

0 comments on commit b2f13d4

Please sign in to comment.