|
| 1 | +import Foundation |
| 2 | + |
| 3 | +func outputCode(_ code: ANSIControlCode) { |
| 4 | + try! FileHandle.standardOutput.write(contentsOf: Data(code.ansiCommand.message.utf8)) |
| 5 | + // fdopen() on stdout is fast; also the returned file MUST NOT be fclose()d |
| 6 | + // This avoids concurrency complaints due to accessing global `stdout`. |
| 7 | + fflush(fdopen(STDOUT_FILENO, "w+")) |
| 8 | +} |
| 9 | + |
| 10 | +func fillScreen<T>(viewState: ViewState<T>) throws { |
| 11 | + outputCode(.clearScreen) |
| 12 | + let choices = viewState.choices.suffix(viewState.height - 1) |
| 13 | + let startLine = viewState.height - choices.count |
| 14 | + var lineNumber = 0 |
| 15 | + for (index, choice) in zip(choices.indices, choices) { |
| 16 | + outputCode(.moveCursor(x: 0, y: startLine + index)) |
| 17 | + print(index == viewState.current ? "> " : " ", terminator: "") |
| 18 | + print(choice, lineNumber) |
| 19 | + lineNumber += 1 |
| 20 | + } |
| 21 | +} |
| 22 | + |
| 23 | +func moveUp<T>(viewState: ViewState<T>) { |
| 24 | + guard let current = viewState.current, current > 0 else { return } |
| 25 | + guard let currentLine = viewState.line(forChoiceIndex: current) else { |
| 26 | + debug("moveUp didn't receive line for current \(current)") |
| 27 | + fatalError() |
| 28 | + } |
| 29 | + |
| 30 | + outputCode(.setCursorHidden(true)) |
| 31 | + defer { outputCode(.setCursorHidden(false)) } |
| 32 | + |
| 33 | + outputCode(.moveCursor(x: 0, y: currentLine)) |
| 34 | + print(" ") |
| 35 | + |
| 36 | + if currentLine > 4 || !viewState.canScrollUp { |
| 37 | + // we don't need to scroll or we can't scroll |
| 38 | + outputCode(.moveCursor(x: 0, y: currentLine - 1)) |
| 39 | + print(">") |
| 40 | + viewState.moveUp() |
| 41 | + outputCode(.moveCursor(x: 0, y: viewState.height)) |
| 42 | + } else { |
| 43 | + outputCode(.moveCursor(x: 0, y: 0)) |
| 44 | + outputCode(.insertLines(1)) |
| 45 | + viewState.moveUp() |
| 46 | + viewState.scrollUp() |
| 47 | + |
| 48 | + print(" ", viewState.choices[viewState.visibleLines.lowerBound], separator: "") |
| 49 | + guard let newCurrentLine = viewState.line(forChoiceIndex: current - 1) else { fatalError() } |
| 50 | + outputCode(.moveCursor(x: 0, y: newCurrentLine)) |
| 51 | + print("> ", separator: "") |
| 52 | + outputCode(.moveCursor(x: 0, y: viewState.height)) |
| 53 | + outputCode(.clearLine) |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +func moveDown<T>(viewState: ViewState<T>) { |
| 58 | + guard let current = viewState.current, current < viewState.choices.count - 1 else { return } |
| 59 | + guard let currentLine = viewState.line(forChoiceIndex: current) else { |
| 60 | + fatalError() |
| 61 | + } |
| 62 | + |
| 63 | + outputCode(.setCursorHidden(true)) |
| 64 | + defer { outputCode(.setCursorHidden(false)) } |
| 65 | + |
| 66 | + outputCode(.moveCursor(x: 0, y: currentLine)) |
| 67 | + print(" ") |
| 68 | + |
| 69 | + if currentLine < viewState.height - 4 || !viewState.canScrollDown { |
| 70 | + outputCode(.moveCursor(x: 0, y: currentLine + 1)) |
| 71 | + print(">") |
| 72 | + viewState.moveDown() |
| 73 | + outputCode(.moveBottom(viewState: viewState)) |
| 74 | + } else { |
| 75 | + outputCode(.moveCursor(x: 0, y: 0)) |
| 76 | + outputCode(.clearLine) |
| 77 | + outputCode(.moveBottom(viewState: viewState)) |
| 78 | + outputCode(.moveCursorUp(n: 1)) |
| 79 | + outputCode(.scrollUp(1)) |
| 80 | + |
| 81 | + viewState.moveDown() |
| 82 | + viewState.scrollDown() |
| 83 | + |
| 84 | + print(" ", viewState.choices[viewState.visibleLines.upperBound], separator: "") |
| 85 | + |
| 86 | + guard let newCurrentLine = viewState.line(forChoiceIndex: current + 1) else { fatalError() } |
| 87 | + |
| 88 | + outputCode(.moveCursor(x: 0, y: newCurrentLine)) |
| 89 | + print("> ") |
| 90 | + |
| 91 | + outputCode(.moveBottom(viewState: viewState)) |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +@main |
| 96 | +struct Fzf { |
| 97 | + static func main() throws -> Void { |
| 98 | + let terminalSize = TerminalSize.current() |
| 99 | + debug("----------------------------------------", reset: true) |
| 100 | + debug("Terminal height: \(terminalSize.height)") |
| 101 | + guard let tty = TTY(fileHandle: STDIN_FILENO) else { |
| 102 | + // TODO: error |
| 103 | + return |
| 104 | + } |
| 105 | + |
| 106 | + let lastLine = 20 |
| 107 | + let viewState = ViewState( |
| 108 | + choices: (0 ... lastLine).map { "line\($0)" }, |
| 109 | + height: terminalSize.height, |
| 110 | + maxWidth: terminalSize.width - 3, |
| 111 | + visibleLines: max(lastLine - terminalSize.height + 2, 0) ... (lastLine) |
| 112 | + ) |
| 113 | + |
| 114 | + debug("Visible lines: \(viewState.visibleLines)") |
| 115 | + try fillScreen(viewState: viewState) |
| 116 | + while true { |
| 117 | + let key = try tty.withRawMode { () -> TerminalKey? in |
| 118 | + var buffer = [UInt8](repeating: 0, count: 3) |
| 119 | + let bytesRead = read(STDIN_FILENO, &buffer, 3) |
| 120 | + if bytesRead == 3 && buffer[0] == 0x1B && buffer[1] == 0x5B { |
| 121 | + switch buffer[2] { |
| 122 | + case 0x41: return .up |
| 123 | + case 0x42: return .down |
| 124 | + default: return nil |
| 125 | + } |
| 126 | + } |
| 127 | + return nil |
| 128 | + } |
| 129 | + guard let key else { break } |
| 130 | + switch key { |
| 131 | + case .down: moveDown(viewState: viewState) |
| 132 | + case .up: moveUp(viewState: viewState) |
| 133 | + } |
| 134 | + } |
| 135 | + } |
| 136 | +} |
| 137 | + |
| 138 | +private func debug(_ message: String, reset: Bool = false) { |
| 139 | + let fh = FileHandle(forUpdatingAtPath: "/tmp/swiftfzfdebug.log")! |
| 140 | + if reset { |
| 141 | + try! fh.truncate(atOffset: 0) |
| 142 | + } |
| 143 | + if message.isEmpty { return } |
| 144 | + |
| 145 | + try! fh.seekToEnd() |
| 146 | + try! fh.write(contentsOf: Data(message.utf8)) |
| 147 | + try! fh.write(contentsOf: Data("\n".utf8)) |
| 148 | + try! fh.close() |
| 149 | +} |
0 commit comments