Skip to content

Commit

Permalink
Use swift concurrency
Browse files Browse the repository at this point in the history
  • Loading branch information
gestrich committed Nov 5, 2023
1 parent 528511f commit 31e7d57
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 14 deletions.
5 changes: 4 additions & 1 deletion Package.swift
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
// swift-tools-version:5.2
// swift-tools-version:5.6
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription

let package = Package(
name: "swift-utilities",
platforms: [
.macOS(.v10_15)
],
products: [
// Products define the executables and libraries produced by a package, and make them visible to other packages.
.library(
Expand Down
10 changes: 4 additions & 6 deletions Sources/swift-utilities/CommandLineAction.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,11 @@
import Foundation

public struct CommandLineAction {
public let longName: String
public let shortName: String?
public let action: () throws -> Void
public let name: String
public let action: () async throws -> Void

public init(longName: String, shortName: String? = nil, action: @escaping () throws -> Void) {
self.longName = longName
self.shortName = shortName
public init(actionName: String, action: @escaping () async throws -> Void) {
self.name = actionName
self.action = action
}

Expand Down
10 changes: 3 additions & 7 deletions Sources/swift-utilities/CommandLineUtils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -123,15 +123,11 @@ public func getEnvironmentVariable(key: String) -> String? {

//MARK: Text Input

public func promptForAction(title: String = "Select Option", actions: [CommandLineAction]) throws {
public func promptForAction(title: String = "Select Option", actions: [CommandLineAction]) async throws {
let selection = promptForSelection(title: title, displayOneBasedIndex: true, options: actions.map({
var option = $0.longName
if let shortName = $0.shortName {
option = "\(option) (-\(shortName))"
}
return option
$0.name
}))
try actions[selection].action()
try await actions[selection].action()
}

public func promptForSelection(title: String, displayOneBasedIndex: Bool = false, options: [String]) -> Int {
Expand Down

0 comments on commit 31e7d57

Please sign in to comment.