Skip to content

Commit

Permalink
Merge pull request #16 from qutheory/fix-trim
Browse files Browse the repository at this point in the history
Fix trim
  • Loading branch information
loganwright committed May 29, 2016
2 parents 20b6164 + 4dc3ae7 commit b71c5a3
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions vapor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,16 @@ enum Error: ErrorProtocol { // Errors pertaining to running commands
case terminalSize
}

let whiteSpace = [Character(" "), Character("\n"), Character("\t"), Character("\r")]

extension String {
func trim(trimCharacter: Character = Character(" ")) -> String {
func trim(trimCharacters: [Character] = whiteSpace) -> String {
// while characters
var mutable = self
while let next = mutable.characters.first where next == trimCharacter {
while let next = mutable.characters.first where trimCharacters.contains(next) {
mutable.remove(at: mutable.startIndex)
}
while let next = mutable.characters.last where next == trimCharacter {
while let next = mutable.characters.last where trimCharacters.contains(next) {
mutable.remove(at: mutable.index(before: mutable.endIndex))
}
return mutable
Expand Down Expand Up @@ -129,8 +131,8 @@ func getPackageName() -> String {
func terminalSize() throws -> (width: Int, height: Int) {
// Get the columns and lines from tput
let tput = "/usr/bin/tput"
let cols = try runWithOutput("\(tput) cols").trim(trimCharacter: "\n")
let lines = try runWithOutput("\(tput) lines").trim(trimCharacter: "\n")
let cols = try runWithOutput("\(tput) cols").trim(trimCharacters: ["\n"])
let lines = try runWithOutput("\(tput) lines").trim(trimCharacters: ["\n"])

if let cols = Int(cols), lines = Int(lines) {
return (cols, lines)
Expand Down

0 comments on commit b71c5a3

Please sign in to comment.