0001 // 0002 // ChooseSettings.swift 0003 // ChooseSettings 0004 // 0005 // Created by Omar Abdelhafith on 03/11/2015. 0006 // Copyright © 2015 Omar Abdelhafith. All rights reserved. 0007 // 0008 0009 0010 /** 0011 Choice index type 0012 0013 - Letters: Use letters as choice index (a. b. c.) 0014 - Numbers: Use numbers as choice index (1. 2. 3.) 0015 */ 0016 public enum ChoiceIndexType{ 0017 case Letters 0018 case Numbers
ChooseSettings.swift:33 public var index = ChoiceIndexType.Numbers0019 } 0020 0021 0022 0023 /// Settings to costumize the behavior of choose 0024 public class ChooseSettings
ChooseSettings.swift:33 public var index = ChoiceIndexType.NumbersChooseSettings.swift:96 if index == .Numbers {<T
Choose.swift:35 public func choose<T>(costumizationBlock: (ChooseSettings<T> -> Void)) -> T {Choose.swift:51 public func choose<T>(prompt: String, type: T.Type, costumizationBlock: (ChooseSettings<T> -> Void)) -> T {Choose.swift:67 public func choose<T>(type: T.Type, costumizationBlock: (ChooseSettings<T> -> Void)) -> T {Choose.swift:77 func choose<T>(settings: ChooseSettings<T>, type: T.Type) -> T {Choose.swift:89 func getChooseSettings<T>(costumizationBlock: ChooseSettings<T> -> Void) -> ChooseSettings<T> {Choose.swift:89 func getChooseSettings<T>(costumizationBlock: ChooseSettings<T> -> Void) -> ChooseSettings<T> {Choose.swift:90 let settings = ChooseSettings<T>()ChooseSettings.swift:109 extension ChooseSettings: AskerValidator {> { 0025 typealias Item = T 0026 0027 var choices
ChooseSettings.swift:25 typealias Item = TChooseSettings.swift:27 var choices: [(choice: String, callback: Void -> T)] = []ChooseSettings.swift:44 public func addChoice(choice: String..., callback: Void -> T) {ChooseSettings.swift:61 func choiceForInput(item: String) -> T? {ChooseSettings.swift:69 func choiceWithIntValue(value: Int) -> T? {ChooseSettings.swift:78 func choiceWithStringValue(value: String) -> T? {: [(choice: String, callback: Void -> T)] = [] 0028 0029 /// Prompt message to use 0030 public var promptQuestion
ChooseSettings.swift:46 choices.append(($0, callback))ChooseSettings.swift:53 let validChoices = Array(1...choices.count).map { "\($0)" }ChooseSettings.swift:58 return choices.map { $0.choice }ChooseSettings.swift:71 if index >= 0 && index < choices.count {ChooseSettings.swift:72 return choices[index].callback()ChooseSettings.swift:79 let possibleIndex = choices.indexOf { $0.choice == value }ChooseSettings.swift:81 return choices[index].callback()= "" 0031 0032 /// Choice index used for choose items 0033 public var index
Choose.swift:54 settings.promptQuestion = promptChoose.swift:82 PromptSettings.print("\(settings.promptQuestion)", terminator: "")= ChoiceIndexType.Numbers 0034 0035 /// Index suffix used between the index and the item 0036 public var indexSuffix
ChooseSettings.swift:96 if index == .Numbers {= ". " 0037 0038 /** 0039 Add a new item to the list of choices 0040 0041 - parameter choice: Item name 0042 - parameter callback: callback called when the item is selected, the value returned from this call back will be returned from choose 0043 */ 0044 public func addChoice
ChooseSettings.swift:89 return "\(index)\(indexSuffix)\(string)"(choice: String..., callback: Void -> T) { 0045 choice.forEach { 0046 choices.append(($0, callback)) 0047 } 0048 } 0049 0050 // MARK:- Internal 0051 0052 func validChoices
Choose.swift:22 $0.addChoice(choice) { return choice }() -> [String] { 0053 let validChoices = Array(1...choices.count).map { "\($0)" } 0054 return validChoices + stringChoices() 0055 } 0056 0057 func stringChoices
ChooseSettings.swift:120 let choicesString = validChoices().joinWithSeparator(", ")() -> [String] { 0058 return choices.map { $0.choice } 0059 } 0060 0061 func choiceForInput
ChooseSettings.swift:54 return validChoices + stringChoices()ChooseSettings.swift:88 return zip(indexChoices(), stringChoices()).map { index, string inChooseSettings.swift:94 return stringChoices().enumerate().map { itemIndex, string in(item: String) -> T? { 0062 if let value = Int(item) { 0063 return choiceWithIntValue(value) 0064 } else { 0065 return choiceWithStringValue(item) 0066 } 0067 } 0068 0069 func choiceWithIntValue
ChooseSettings.swift:111 return choiceForInput(string)!ChooseSettings.swift:115 if choiceForInput(string!) != nil {(value: Int) -> T? { 0070 let index = value - 1 0071 if index >= 0 && index < choices.count { 0072 return choices[index].callback() 0073 } 0074 0075 return nil 0076 } 0077 0078 func choiceWithStringValue
ChooseSettings.swift:63 return choiceWithIntValue(value)(value: String) -> T? { 0079 let possibleIndex = choices.indexOf { $0.choice == value } 0080 if let index = possibleIndex { 0081 return choices[index].callback() 0082 } 0083 0084 return nil 0085 } 0086 0087 func preparePromptItems
ChooseSettings.swift:65 return choiceWithStringValue(item)() -> [String] { 0088 return zip(indexChoices(), stringChoices()).map { index, string in 0089 return "\(index)\(indexSuffix)\(string)" 0090 } 0091 } 0092 0093 func indexChoices
Choose.swift:79 let items = settings.preparePromptItems()() -> [String] { 0094 return stringChoices().enumerate().map { itemIndex, string in 0095 0096 if index == .Numbers { 0097 return "\(itemIndex + 1)" 0098 } else { 0099 let character = "a".unicodeScalars.first!.value + UInt32(itemIndex) 0100 return String(Character(UnicodeScalar(character))) 0101 } 0102 } 0103 } 0104 0105 } 0106 0107 // MARK:- Internal Class 0108 0109 extension ChooseSettings: AskerValidator { 0110 func validatedItem(forString string: String) -> T { 0111 return choiceForInput(string)! 0112 } 0113 0114 func invalidItemMessage(string: String?) -> String? { 0115 if choiceForInput(string!) != nil { 0116 return nil 0117 } 0118 0119 let baseMessage = "You must choose one of" 0120 let choicesString = validChoices().joinWithSeparator(", ") 0121 0122 return "\(baseMessage) [\(choicesString)]." 0123 } 0124 0125 func newItemPromptMessage() -> String { 0126 return "? " 0127 } 0128 }
ChooseSettings.swift:88 return zip(indexChoices(), stringChoices()).map { index, string in