0001 // 0002 // Chooser.swift 0003 // Choose 0004 // 0005 // Created by Omar Abdelhafith on 03/11/2015. 0006 // Copyright © 2015 Omar Abdelhafith. All rights reserved. 0007 // 0008 0009 0010 0011 /** 0012 Presents a user with a menu of items to choose from 0013 0014 - parameter prompt: The menu prompt message 0015 - parameter choices: List of choices 0016 0017 - returns: The user selected item 0018 */ 0019 public func choose(prompt: String, choices: String...) -> String { 0020 return choose(prompt, type: String.self) { 0021 for choice in choices { 0022 $0.addChoice(choice) { return choice } 0023 } 0024 } 0025 } 0026 0027 0028 /** 0029 Presents a user with a menu of items to choose from 0030 0031 - parameter costumizationBlock: Closure to be called with a ChooseSettings, changes to the settings are reflected to the prompt 0032 0033 - returns: The user selected item 0034 */ 0035 public func choose<T>(costumizationBlock: (ChooseSettings<T> -> Void)) -> T { 0036 0037 let settings = getChooseSettings(costumizationBlock) 0038 return choose(settings, type: T.self) 0039 } 0040 0041 0042 /** 0043 Presents a user with a menu of items to choose from 0044 0045 - parameter prompt: The menu prompt message 0046 - parameter type: The value type to be expected from the user 0047 - parameter costumizationBlock: Closure to be called with a ChooseSettings, changes to the settings are reflected to the prompt 0048 0049 - returns: The user selected item 0050 */ 0051 public func choose<T>(prompt: String, type: T.Type, costumizationBlock: (ChooseSettings<T> -> Void)) -> T { 0052 0053 let settings = getChooseSettings(costumizationBlock) 0054 settings.promptQuestion = prompt 0055 return choose(settings, type: type) 0056 } 0057 0058 0059 /** 0060 Presents a user with a menu of items to choose from 0061 0062 - parameter type: The value type to be expected from the user 0063 - parameter costumizationBlock: Closure to be called with a ChooseSettings, changes to the settings are reflected to the prompt 0064 0065 - returns: The user selected item 0066 */ 0067 public func choose<T>(type: T.Type, costumizationBlock: (ChooseSettings<T> -> Void)) -> T { 0068 0069 let settings = getChooseSettings(costumizationBlock) 0070 return choose(settings, type: type) 0071 } 0072 0073 0074 // MARK :- Internal functions 0075 0076 0077 func choose
Choose.swift:20 return choose(prompt, type: String.self) {<T>(settings: ChooseSettings<T>, type: T.Type) -> T { 0078 0079 let items = settings.preparePromptItems() 0080 0081 items.forEach { PromptSettings.print($0) } 0082 PromptSettings.print("\(settings.promptQuestion)", terminator: "") 0083 0084 let stringRead = readStringOrEmpty() 0085 0086 return askForValidatedItem(originalValue: stringRead, validator: settings) 0087 } 0088 0089 func getChooseSettings
Choose.swift:38 return choose(settings, type: T.self)Choose.swift:55 return choose(settings, type: type)Choose.swift:70 return choose(settings, type: type)<T>(costumizationBlock: ChooseSettings<T> -> Void) -> ChooseSettings<T> { 0090 let settings = ChooseSettings<T>() 0091 costumizationBlock(settings) 0092 return settings 0093 }
Choose.swift:37 let settings = getChooseSettings(costumizationBlock)Choose.swift:53 let settings = getChooseSettings(costumizationBlock)Choose.swift:69 let settings = getChooseSettings(costumizationBlock)