Skip to content

Commit da55133

Browse files
committed
FuzzySelector.init: Throw error if init fails
1 parent 05fe844 commit da55133

File tree

2 files changed

+16
-13
lines changed

2 files changed

+16
-13
lines changed

Sources/CLI/CLI.swift

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,20 @@ struct FuzzyCLI: AsyncParsableCommand {
2121

2222
mutating func run() async throws {
2323
let lines = DirectoryLister(root: URL(string: ".")!).contents
24-
guard
25-
let selector = FuzzySelector(
24+
let choices: [URL]
25+
do {
26+
let selector = try FuzzySelector(
2627
choices: lines,
2728
installSignalHandlers: self.installSignalHandlers,
2829
matchCaseSensitivity: self.caseSensitivity.matchCaseSensitivity,
2930
multipleSelection: self.multipleSelection,
3031
reverse: self.reverse
3132
)
32-
else {
33-
return
33+
choices = try await selector.run()
34+
} catch {
35+
FileHandle.standardError.write(Data("Error: \(error)\n".utf8))
36+
throw ExitCode(1)
3437
}
35-
let choices = try await selector.run()
3638

3739
for choice in choices {
3840
print(choice)

Sources/FuzzyTUI/FuzzySelector.swift

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -448,30 +448,27 @@ public final class FuzzySelector<T: Selectable, E: Error, Seq> where Seq: AsyncS
448448
private let viewState: ViewState<T>
449449

450450
/// Initialize a `FuzzySelector`.
451-
public init?(
451+
public init(
452452
choices: Seq,
453453
appearance: Appearance? = nil,
454454
installSignalHandlers: Bool = true,
455455
matchCaseSensitivity: MatchCaseSensitivity? = nil,
456456
multipleSelection: Bool = true,
457457
reverse: Bool = true
458-
) {
458+
) throws {
459459
let appearance = appearance ?? .default
460460
guard let terminalSize = TerminalSize.current() else {
461-
// TODO: error
462-
return nil
461+
throw TerminalError(message: "Failed to read terminal size")
463462
}
464463
let ttyHandle = FileHandle(forReadingAtPath: "/dev/tty")!
465464
guard let tty = TTY(fileHandle: ttyHandle) else {
466-
// TODO: error
467-
return nil
465+
throw TerminalError(message: "Input not a TTY")
468466
}
469467

470468
guard let outTTYHandle = FileHandle(forWritingAtPath: "/dev/tty"),
471469
let outTTY = OutTTY(fileHandle: outTTYHandle)
472470
else {
473-
// TODO: error
474-
return nil
471+
throw TerminalError(message: "Output not a TTY")
475472
}
476473

477474
let viewState = ViewState(
@@ -666,3 +663,7 @@ public final class FuzzySelector<T: Selectable, E: Error, Seq> where Seq: AsyncS
666663
self.view.showStatus()
667664
}
668665
}
666+
667+
public struct TerminalError: Error {
668+
public let message: String
669+
}

0 commit comments

Comments
 (0)