xtool.git
Publicgit clone https://git.maproj.org/xtool.gitxtool.git / Sources / XCLIUtils / Console.swift
Console.swift111 lines · 3.6 KiBRaw
| 1 | import Foundation |
| 2 | import XUtils |
| 3 | import Dependencies |
| 4 | |
| 5 | package enum Console { |
| 6 | package static func prompt(_ message: String) async throws -> String { |
| 7 | d.out.print(message, terminator: "", flush: true) |
| 8 | guard let line = try await d.input.readLine() else { throw Console.Error("Input ended before a response was received.") } |
| 9 | return line |
| 10 | } |
| 11 | |
| 12 | package static func promptRequired(_ message: String, existing: String?) async throws -> String { |
| 13 | let value: String |
| 14 | if let existing { |
| 15 | value = existing |
| 16 | } else { |
| 17 | value = try await Console.prompt(message) |
| 18 | } |
| 19 | guard !value.isEmpty else { |
| 20 | throw Console.Error("Input cannot be empty.") |
| 21 | } |
| 22 | return value |
| 23 | } |
| 24 | |
| 25 | package static func getPassword(_ message: String) async throws -> String { |
| 26 | if !message.isEmpty { |
| 27 | d.out.print(message, terminator: "") |
| 28 | } |
| 29 | let password = try await withoutEcho { try await prompt("") } |
| 30 | d.out.print() |
| 31 | return password |
| 32 | } |
| 33 | |
| 34 | private static func withoutEcho<T>(_ action: () async throws -> T) async rethrows -> T { |
| 35 | #if os(Windows) |
| 36 | // based on https://stackoverflow.com/a/4497117/3769927 |
| 37 | // TODO: Confirm that this works (or even compiles) |
| 38 | |
| 39 | let hConsole = CreateFileA("CONIN$", GENERIC_WRITE | GENERIC_READ, FILE_SHARE_READ, 0, OPEN_EXISTING, 0, 0) |
| 40 | var dwOldMode: DWORD = 0 |
| 41 | GetConsoleMode(hConsole, &dwOldMode) |
| 42 | |
| 43 | let dwNewMode = dwOldMode & ~ENABLE_ECHO_INPUT |
| 44 | SetConsoleMode(hConsole, dwNewMode) |
| 45 | defer { SetConsoleMode(hConsole, dwOldMode) } |
| 46 | |
| 47 | return try action() |
| 48 | #else |
| 49 | var origAttr = termios() |
| 50 | tcgetattr(STDIN_FILENO, &origAttr) |
| 51 | |
| 52 | var newAttr = origAttr |
| 53 | newAttr.c_lflag = newAttr.c_lflag & ~tcflag_t(ECHO) |
| 54 | tcsetattr(STDIN_FILENO, TCSANOW, &newAttr) |
| 55 | defer { tcsetattr(STDIN_FILENO, TCSANOW, &origAttr) } |
| 56 | |
| 57 | return try await action() |
| 58 | #endif |
| 59 | } |
| 60 | |
| 61 | package static func chooseNumber(in range: Range<Int>) async throws -> Int { |
| 62 | let message = "Choice (\(range.lowerBound)-\(range.upperBound - 1)): " |
| 63 | while true { |
| 64 | if let choice = try await Int(prompt(message)), range.contains(choice) { |
| 65 | return choice |
| 66 | } |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | package static func choose<T>( |
| 71 | from elements: [T], |
| 72 | onNoElement: () throws -> T, |
| 73 | multiPrompt: @autoclosure () -> String, |
| 74 | formatter: (T) throws -> String |
| 75 | ) async throws -> T { |
| 76 | switch elements.count { |
| 77 | case 0: |
| 78 | return try onNoElement() |
| 79 | case 1: |
| 80 | return elements[0] |
| 81 | default: |
| 82 | d.out.print(multiPrompt()) |
| 83 | try elements.enumerated().forEach { index, element in |
| 84 | try d.out.print("\(index): \(formatter(element))") |
| 85 | } |
| 86 | let choice = try await chooseNumber(in: elements.indices) |
| 87 | return elements[choice] |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | private static let yesSet: Set<String> = ["yes", "y"] |
| 92 | private static let noSet: Set<String> = ["no", "n"] |
| 93 | |
| 94 | package static func confirm(_ message: String) async throws -> Bool { |
| 95 | while true { |
| 96 | let resp = try await prompt("\(message) (yes/no): ").lowercased() |
| 97 | if yesSet.contains(resp) { |
| 98 | return true |
| 99 | } else if noSet.contains(resp) { |
| 100 | return false |
| 101 | } |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | package struct Error: Swift.Error, CustomStringConvertible { |
| 106 | package let description: String |
| 107 | package init(_ description: String) { |
| 108 | self.description = description |
| 109 | } |
| 110 | } |
| 111 | } |