Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

latest swift version #52

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion Package.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,26 @@
// swift-tools-version:5.3

import PackageDescription

let package = Package(
name: "Swiftline"
name: "Swiftline",
products: [
.library(
name: "Swiftline",
targets: ["Swiftline"])
],
targets: [
.target(
name: "Swiftline",
swiftSettings: [
.define("DEBUG", .when(configuration: .debug)),
]),
.testTarget(
name: "SwiftlineTests",
dependencies: ["Swiftline"]),
.testTarget(
name: "GeneralTests",
dependencies: ["Swiftline"]),
],
swiftLanguageVersions: [.v4_2, .v5]
)
4 changes: 2 additions & 2 deletions Source/Args.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
public class Args {

/// Return the list of arguments passed to the script
open static var all: [String] {
public static var all: [String] {
return ProcessInfo.arguments
}

Expand All @@ -21,7 +21,7 @@ public class Args {
/// The flags are recognized as short flags `-f` or long flags `--force`
/// The flag value will be the argument that follows the flag
/// `--` is used to mark the terminatin of the flags
open static var parsed: ParsedArgs {
public static var parsed: ParsedArgs {

if let result = cachedResults , ProcessInfo.cacheResults {
return result
Expand Down
4 changes: 2 additions & 2 deletions Source/Argument.swift
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,8 @@ extension String {
distanceFromEndIndex = 0
}

let actualRange = (characters.index(startIndex, offsetBy: range.lowerBound) ..< characters.index(endIndex, offsetBy: -distanceFromEndIndex))
let actualRange = (self.index(startIndex, offsetBy: range.lowerBound) ..< self.index(endIndex, offsetBy: -distanceFromEndIndex))

return self[actualRange]
return String(self[actualRange])
}
}
6 changes: 3 additions & 3 deletions Source/ChooseSettings.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public enum ChoiceIndexType {
public class ChooseSettings<T> {
typealias Item = T

var choices: [(choice: String, callback: (Void) -> T)] = []
var choices: [(choice: String, callback: () -> T)] = []

/// Prompt message to use
public var promptQuestion = ""
Expand All @@ -41,7 +41,7 @@ public class ChooseSettings<T> {
- parameter choice: Item name
- parameter callback: callback called when the item is selected, the value returned from this call back will be returned from choose
*/
public func addChoice(_ choice: String..., callback: @escaping (Void) -> T) {
public func addChoice(_ choice: String..., callback: @escaping () -> T) {
choice.forEach {
choices.append(($0, callback))
}
Expand Down Expand Up @@ -76,7 +76,7 @@ public class ChooseSettings<T> {
}

func choiceWithStringValue(_ value: String) -> T? {
let possibleIndex = choices.index { $0.choice == value }
let possibleIndex = choices.firstIndex { $0.choice == value }
if let index = possibleIndex {
return choices[index].callback()
}
Expand Down
2 changes: 1 addition & 1 deletion Source/Env.swift
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public class Env {
self.keys
.map { String($0) }
.filter { $0 != nil }
.forEach{ self.set($0!, nil) }
.forEach{ self.set($0, nil) }
}

/**
Expand Down
2 changes: 1 addition & 1 deletion Source/RunResults.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,5 @@ func splitCommandToArgs(_ command: String) -> [String] {

func readPipe(_ pipe: TaskPipe) -> String {
let data = pipe.read()
return NSString(data: data as Data, encoding: String.Encoding.utf8.rawValue) as? String ?? ""
return NSString(data: data as Data, encoding: String.Encoding.utf8.rawValue) as String? ?? ""
}
6 changes: 3 additions & 3 deletions Source/StringStyle.swift
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,15 @@ extension StringStyle {
}

fileprivate func addCommandSeperators(_ string: String) -> String {
var rangeWithInset = (string.characters.index(after: string.startIndex) ..< string.characters.index(before: string.endIndex))
var rangeWithInset = (string.index(after: string.startIndex) ..< string.index(before: string.endIndex))
let newString = string.replacingOccurrences(of: startOfCode, with: ";", options: .literal, range: rangeWithInset)

rangeWithInset = (newString.characters.index(after: newString.startIndex) ..< newString.characters.index(before: newString.endIndex))
rangeWithInset = (newString.index(after: newString.startIndex) ..< newString.index(before: newString.endIndex))
return newString.replacingOccurrences(of: "m;", with: ";", options: .literal, range: rangeWithInset)
}

fileprivate func removeEndingCode(_ string: String) -> String {
let rangeWithInset = (string.characters.index(after: string.startIndex) ..< string.endIndex)
let rangeWithInset = (string.index(after: string.startIndex) ..< string.endIndex)
return string.replacingOccurrences(of: endingColorCode(), with: "", options: .literal, range: rangeWithInset)
}

Expand Down
2 changes: 1 addition & 1 deletion Source/StringStyleColorizer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

public extension String {

public struct StringStyleColorizer {
struct StringStyleColorizer {

let string: String

Expand Down
Binary file added Sources/.DS_Store
Binary file not shown.
23 changes: 23 additions & 0 deletions Sources/SwiftLine/Agree.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//
// Agree.swift
// Agree
//
// Created by Omar Abdelhafith on 03/11/2015.
// Copyright © 2015 Omar Abdelhafith. All rights reserved.
//

/**
Displays a yes/no prompt to the user

- parameter prompt: The prompt to display
- returns: the user decision
*/
public func agree(_ prompt: String) -> Bool {
PromptSettings.print(prompt, terminator: " ")
let value = readStringOrEmpty()

let settings = AgreeSettings(prompt: prompt)
let validatedValue = askForValidatedItem(originalValue: value, validator: settings)

return settings.isPositive(validatedValue)
}
41 changes: 41 additions & 0 deletions Sources/SwiftLine/AgreeSettings.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
//
// AgreeSettings.swift
// AgreeSettings
//
// Created by Omar Abdelhafith on 03/11/2015.
// Copyright © 2015 Omar Abdelhafith. All rights reserved.
//


class AgreeSettings: AskerValidator {

let positiveValues = ["Yes", "yes", "Y", "y"]
let negativeValues = ["No", "no", "N", "n"]

let prompt: String

init(prompt: String) {
self.prompt = prompt
}

func validatedItem(forString string: String) -> String {
return string
}

func invalidItemMessage(_ string: String?) -> String? {
if let message = string , positiveValues.contains(message) || negativeValues.contains(message) {
return nil
}

return "Please enter \"yes\" or \"no\"."
}

func newItemPromptMessage() -> String {
return "\(prompt) "
}

func isPositive(_ item: String) -> Bool {
return positiveValues.contains(item)
}

}
63 changes: 63 additions & 0 deletions Sources/SwiftLine/ArgConvertible.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
//
// ArgConvertible.swift
// ArgConvertible
//
// Created by Omar Abdelhafith on 02/11/2015.
// Copyright © 2015 Omar Abdelhafith. All rights reserved.
//


/**
* Any type that extends ArgConvertibleType can be used in ask and choose
*/
public protocol ArgConvertibleType {

/// Create an instance out of a string
static func fromString(_ string: String) -> Self?

/// Return the display name of a type
static func typeName() -> String
}


extension Int: ArgConvertibleType {
public static func fromString(_ string: String) -> Int? {
return Int(string)
}

public static func typeName() -> String {
return "Integer"
}
}


extension Double: ArgConvertibleType {
public static func fromString(_ string: String) -> Double? {
return Double(string)
}

public static func typeName() -> String {
return "Double"
}
}

extension Float: ArgConvertibleType {
public static func fromString(_ string: String) -> Float? {
return Float(string)
}

public static func typeName() -> String {
return "Float"
}
}


extension String: ArgConvertibleType {
public static func fromString(_ string: String) -> String? {
return string
}

public static func typeName() -> String {
return "String"
}
}
61 changes: 61 additions & 0 deletions Sources/SwiftLine/Args.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
//
// Args.swift
// Swiftline
//
// Created by Omar Abdelhafith on 25/11/2015.
// Copyright © 2015 Omar Abdelhafith. All rights reserved.
//


/// Return the command line arguments passed to the script
public class Args {

/// Return the list of arguments passed to the script
public static var all: [String] {
return ProcessInfo.arguments
}

static var cachedResults: ParsedArgs?

/// Return a parsed list of arguments containing the flags and the parameters passed to the scripts
/// The flags are recognized as short flags `-f` or long flags `--force`
/// The flag value will be the argument that follows the flag
/// `--` is used to mark the terminatin of the flags
public static var parsed: ParsedArgs {

if let result = cachedResults , ProcessInfo.cacheResults {
return result
}

var parsedFlags = [String: String]()
let parsedArgs = ArgsParser.parseFlags(all)

parsedArgs.0.forEach {
parsedFlags[$0.argument.name] = $0.value ?? ""
}

var arguments = parsedArgs.1

// the first argument is always the executable's name
var commandName = ""
if let firstArgument = arguments.first { // just in case!
commandName = firstArgument
arguments.removeFirst(1)
}

cachedResults = ParsedArgs(command: commandName, flags: parsedFlags, parameters: arguments)
return cachedResults!
}
}


public struct ParsedArgs {
/// The name of the executable that was invoked from the command line
public let command: String

/// Parsed flags will be prepred in a dictionary, the key is the flag and the value is the flag value
public let flags: [String: String]

/// List of parameters passed to the script
public let parameters: [String]
}
53 changes: 53 additions & 0 deletions Sources/SwiftLine/ArgsParser.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
//
// ArgsParser.swift
// Swiftline
//
// Created by Omar Abdelhafith on 27/11/2015.
// Copyright © 2015 Omar Abdelhafith. All rights reserved.
//

class ArgsParser {

static func parseFlags(_ args: [String]) -> ([Option], [String]) {
var options = [Option]()
var others = [String]()
var previousArgument: Argument?
var argsTerminated = false

for argumentString in args {
let argument = Argument(argumentString)
defer { previousArgument = argument }

if argsTerminated {
others += [argumentString]
continue
}

if argument.isFlagTerminator {
argsTerminated = true
continue
}

if argument.isFlag {
options += [Option(argument: argument)]
continue
}

if let previousArgument = previousArgument , previousArgument.isFlag {
updatelastOption(forArray: &options, withValue: argumentString)
} else {
others += [argument.name]
}
}

return (options, others)

}

static func updatelastOption(forArray array: inout [Option], withValue value: String) {
var previousOption = array.last!
previousOption.value = value
array.removeLast()
array += [previousOption]
}
}
Loading