-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathLineReader.swift
More file actions
715 lines (662 loc) · 24.6 KB
/
LineReader.swift
File metadata and controls
715 lines (662 loc) · 24.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
//
// LineReader.swift
// CommandLineKit
//
// Created by Matthias Zenger on 07/04/2018.
// Copyright © 2018-2021 Google LLC
// Copyright © 2017 Andy Best <andybest.net at gmail dot com>
// Copyright © 2010-2014 Salvatore Sanfilippo <antirez at gmail dot com>
// Copyright © 2010-2013 Pieter Noordhuis <pcnoordhuis at gmail dot com>
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// * Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
//
// * Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// * Neither the name of the copyright holder nor the names of its contributors
// may be used to endorse or promote products derived from this software without
// specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
// ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
// ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
import Foundation
public class LineReader {
/// Does this terminal support this line reader?
public let termSupported: Bool
/// Terminal type
public let currentTerm: String
/// Does the terminal support colors?
public let fullColorSupport: Bool
/// If false (the default) any edits by the user to a line in the history will be discarded
/// if the user moves forward or back in the history without pressing Enter. If true, all
/// history edits will be preserved.
public var preserveHistoryEdits = false
/// The history of previous line reads
private var history: LineReaderHistory
/// Temporary line read buffer to handle browsing of histories
private var tempBuf: String?
/// A callback for handling line completions
private var completionCallback: ((String) -> [String])?
/// A callback for handling hints
private var hintsCallback: ((String) -> (String, TextProperties)?)?
/// A POSIX file handle for the input
private let inputFile: Int32
/// A POSIX file handle for the output
private let outputFile: Int32
/// Initializer
public init?(inputFile: Int32 = STDIN_FILENO,
outputFile: Int32 = STDOUT_FILENO,
completionCallback: ((String) -> [String])? = nil,
hintsCallback: ((String) -> (String, TextProperties)?)? = nil) {
self.inputFile = inputFile
self.outputFile = outputFile
self.currentTerm = Terminal.current
if isatty(inputFile) != 1 {
return nil
} else {
self.termSupported = LineReader.supportedBy(terminal: self.currentTerm)
}
self.fullColorSupport = Terminal.fullColorSupport(terminal: self.currentTerm)
self.history = LineReaderHistory()
self.completionCallback = completionCallback
self.hintsCallback = hintsCallback
}
public static var supportedByTerminal: Bool {
return LineReader.supportedBy(terminal: Terminal.current)
}
public static func supportedBy(terminal: String) -> Bool {
#if os(macOS)
if let xpcServiceName = ProcessInfo.processInfo.environment["XPC_SERVICE_NAME"],
xpcServiceName.localizedCaseInsensitiveContains("com.apple.dt.xcode") {
return false
}
#endif
switch terminal {
case "", "xcode", "dumb", "cons25", "emacs":
return false
default:
return true
}
}
/// Adds a string to the history buffer.
public func addHistory(_ item: String) {
self.history.add(item)
}
/// Adds a callback for tab completion. The callback is taking the current text and returning
/// an array of Strings containing possible completions.
public func setCompletionCallback(_ callback: @escaping (String) -> [String]) {
self.completionCallback = callback
}
/// Adds a callback for hints as you type. The callback is taking the current text and
/// optionally returning the hint and a tuple of RGB colours for the hint text.
public func setHintsCallback(_ callback: @escaping (String) -> (String, TextProperties)?) {
self.hintsCallback = callback
}
/// Loads history from a file and appends it to the current history buffer. This method can
/// throw an error if the file cannot be found or loaded.
public func loadHistory(fromFile path: String) throws {
try self.history.load(fromFile: path)
}
/// Saves history to a file. This method can throw an error if the file cannot be written to.
public func saveHistory(toFile path: String) throws {
try self.history.save(toFile: path)
}
/// Sets the maximum amount of items to keep in history. If this limit is reached, the oldest
/// item is discarded when a new item is added. Setting the maximum length of history to 0
/// (the default) will keep unlimited items in history.
public func setHistoryMaxLength(_ historyMaxLength: UInt) {
self.history.maxLength = historyMaxLength
}
/// Clears the screen. This method can throw an error if the terminal cannot be written to.
public func clearScreen() throws {
if self.termSupported {
try self.output(text: AnsiCodes.homeCursor)
try self.output(text: AnsiCodes.clearScreen)
}
}
/// The main function of LineReader. This method shows a prompt to the user at the beginning
/// of the line and reads the input from the user, returning it as a string. The method can
/// throw an error if the terminal cannot be written to.
public func readLine(prompt: String,
maxCount: Int? = nil,
strippingNewline: Bool = true,
promptProperties: TextProperties = TextProperties.none,
readProperties: TextProperties = TextProperties.none,
parenProperties: TextProperties = TextProperties.none) throws -> String {
tempBuf = nil
if self.termSupported {
return try self.readLineSupported(prompt: prompt,
maxCount: maxCount,
strippingNewline: strippingNewline,
promptProperties: promptProperties,
readProperties: readProperties,
parenProperties: parenProperties)
} else {
return try self.readLineUnsupported(prompt: prompt,
maxCount: maxCount,
strippingNewline: strippingNewline)
}
}
private func readLineUnsupported(prompt: String,
maxCount: Int?,
strippingNewline: Bool) throws -> String {
Swift.print(prompt, terminator: "")
if let line = Swift.readLine(strippingNewline: strippingNewline) {
return maxCount != nil ? String(line.prefix(maxCount!)) : line
} else {
throw LineReaderError.EOF
}
}
private func readLineSupported(prompt: String,
maxCount: Int?,
strippingNewline: Bool,
promptProperties: TextProperties,
readProperties: TextProperties,
parenProperties: TextProperties) throws -> String {
var line: String = ""
if fileno(stdout) == self.outputFile {
fflush(stdout)
}
try self.withRawMode {
if let col = self.cursorColumn, col > 1 {
try self.output(text: "\n" + AnsiCodes.setCursorColumn(0))
}
try self.output(text: promptProperties.apply(to: prompt))
let editState = EditState(prompt: prompt,
maxCount: maxCount,
promptProperties: promptProperties,
readProperties: readProperties,
parenProperties: parenProperties)
while true {
guard var char = self.readByte() else {
return
}
if char == ControlCharacters.Tab.rawValue && self.completionCallback != nil,
let completionChar = try self.completeLine(editState: editState) {
char = completionChar
}
if let rv = try self.handleCharacter(char, editState: editState) {
if editState.moveEnd() {
try self.updateCursorPos(editState: editState)
}
// It's unclear to me why it's necessary to set the cursor to column 0
try self.output(text: "\n" + AnsiCodes.setCursorColumn(0))
line = rv
return
}
}
}
return strippingNewline ? line : line + "\n"
}
private func completeLine(editState: EditState) throws -> UInt8? {
guard let completionCallback = self.completionCallback else {
return nil
}
let completions = completionCallback(editState.buffer)
guard completions.count > 0 else {
self.ringBell()
return nil
}
// Loop to handle inputs
var completionIndex = 0
while true {
if completionIndex < completions.count {
try editState.withTemporaryState {
try self.setBuffer(editState: editState, new: completions[completionIndex])
}
} else {
try refreshLine(editState: editState)
}
guard let char = self.readByte() else {
return nil
}
switch char {
case ControlCharacters.Tab.rawValue:
// Move to next completion
completionIndex = (completionIndex + 1) % (completions.count + 1)
if completionIndex == completions.count {
self.ringBell()
}
case ControlCharacters.Esc.rawValue:
// Show the original buffer
if completionIndex < completions.count {
try refreshLine(editState: editState)
}
return char
default:
// Update the buffer and return
if completionIndex < completions.count {
try self.setBuffer(editState: editState, new: completions[completionIndex])
}
return char
}
}
}
private func handleCharacter(_ ch: UInt8, editState: EditState) throws -> String? {
switch ch {
case ControlCharacters.Enter.rawValue:
try refreshLine(editState: editState, decorate: false)
return editState.buffer
case ControlCharacters.CtrlA.rawValue:
try self.moveHome(editState: editState)
case ControlCharacters.CtrlE.rawValue:
try self.moveEnd(editState: editState)
case ControlCharacters.CtrlB.rawValue:
try self.moveLeft(editState: editState)
case ControlCharacters.CtrlC.rawValue:
// Throw an error so that CTRL+C can be handled by the caller
throw LineReaderError.CTRLC
case ControlCharacters.CtrlD.rawValue:
// If there is a character at the right of the cursor, remove it
if editState.eraseCharacterRight() {
try self.refreshLine(editState: editState)
} else {
self.ringBell()
}
case ControlCharacters.CtrlP.rawValue:
// Previous history item
try self.moveHistory(editState: editState, direction: .previous)
case ControlCharacters.CtrlN.rawValue:
// Next history item
try self.moveHistory(editState: editState, direction: .next)
case ControlCharacters.CtrlL.rawValue:
// Clear screen
try self.clearScreen()
try self.refreshLine(editState: editState)
case ControlCharacters.CtrlT.rawValue:
if editState.swapCharacterWithPrevious() {
try refreshLine(editState: editState)
} else {
self.ringBell()
}
case ControlCharacters.CtrlU.rawValue:
// Delete whole line
try self.setBuffer(editState: editState, new: "")
case ControlCharacters.CtrlK.rawValue:
// Delete to the end of the line
if editState.deleteToEndOfLine() {
try self.refreshLine(editState: editState)
} else {
self.ringBell()
}
case ControlCharacters.CtrlW.rawValue:
// Delete previous word
if editState.deletePreviousWord() {
try self.refreshLine(editState: editState)
} else {
self.ringBell()
}
case ControlCharacters.Backspace.rawValue:
// Delete character
if editState.backspace() {
try self.refreshLine(editState: editState)
} else {
self.ringBell()
}
case ControlCharacters.Esc.rawValue:
try self.handleEscapeCode(editState: editState)
default:
// Read unicode character and insert it at the cursor position using UTF8 encoding
var scalar = UInt32(ch)
if ch >> 7 == 0 {
// done
} else if ch >> 5 == 0x6 {
let ch2 = self.forceReadByte()
scalar = (UInt32(ch & 0x1F) << 6) | UInt32(ch2 & 0x3F)
} else if ch >> 4 == 0xE {
let ch2 = self.forceReadByte()
let ch3 = self.forceReadByte()
scalar = (UInt32(ch & 0xF) << 12) | (UInt32(ch2 & 0x3F) << 6) | UInt32(ch3 & 0x3F)
} else if ch >> 3 == 0x1E {
let ch2 = self.forceReadByte()
let ch3 = self.forceReadByte()
let ch4 = self.forceReadByte()
scalar = (UInt32(ch & 0x7) << 18) |
(UInt32(ch2 & 0x3F) << 12) |
(UInt32(ch3 & 0x3F) << 6) |
UInt32(ch4 & 0x3F)
}
let char = Character(UnicodeScalar(scalar) ?? UnicodeScalar(" "))
if editState.insertCharacter(char) {
try refreshLine(editState: editState)
} else {
self.ringBell()
}
if self.bytesAvailable > 0 {
self.ringBell()
}
}
return nil
}
private func handleEscapeCode(editState: EditState) throws {
let fst = self.readCharacter()
switch fst {
case "[":
let snd = self.readCharacter()
switch snd {
// Handle multi-byte sequence ^[[0...
case "0", "1", "2", "3", "4", "5", "6", "7", "8", "9":
let trd = self.readCharacter()
switch trd {
case "~":
switch snd {
case "1", "7":
try self.moveHome(editState: editState)
case "3":
try self.deleteCharacter(editState: editState)
case "4":
try self.moveEnd(editState: editState)
default:
break
}
case ";":
let fot = self.readCharacter()
let fth = self.readCharacter()
// Shift
if fot == "2" {
switch fth {
case "C":
try self.moveRight(editState: editState)
case "D":
try self.moveLeft(editState: editState)
default:
break
}
}
break
case "0", "1", "2", "3", "4", "5", "6", "7", "8", "9":
_ = self.readCharacter()
// ignore these codes for now
break
default:
break
}
// ^[...
case "A":
try self.moveHistory(editState: editState, direction: .previous)
case "B":
try self.moveHistory(editState: editState, direction: .next)
case "C":
try self.moveRight(editState: editState)
case "D":
try self.moveLeft(editState: editState)
case "H":
try self.moveHome(editState: editState)
case "F":
try self.moveEnd(editState: editState)
default:
break
}
case "O":
// ^[O...
let snd = self.readCharacter()
switch snd {
case "H":
try self.moveHome(editState: editState)
case "F":
try self.moveEnd(editState: editState)
case "P":
// F1
break
case "Q":
// F2
break
case "R":
// F3
break
case "S":
// F4
break
default:
break
}
case "b":
// Alt+Left
try self.moveToWordStart(editState: editState)
case "f":
// Alt+Right
try self.moveToWordEnd(editState: editState)
default:
break
}
}
private var cursorColumn: Int? {
do {
try self.output(text: AnsiCodes.cursorLocation)
} catch {
return nil
}
var buf = [UInt8]()
while true {
if let c = self.readByte() {
if c == 82 { // "R"
break
}
buf.append(c)
} else {
return nil
}
}
guard buf[0] == 0x1B && buf[1] == 0x5B,
let cursor = String(bytes: buf[2..<buf.count], encoding: .utf8)?.split(separator: ";"),
cursor.count == 2 else {
return nil
}
return Int(String(cursor[1]))
}
private var numColumns: Int {
var winSize = winsize()
if ioctl(1, UInt(TIOCGWINSZ), &winSize) == -1 || winSize.ws_col == 0 {
return 80
} else {
return Int(winSize.ws_col)
}
}
/// This constant is unfortunately not defined right now for usage in Swift; it is specific
/// to macOS. Thus, this code is not portable!
private static let FIONREAD: UInt = 0x4004667f
private var bytesAvailable: Int {
var available: Int = 0
guard ioctl(self.inputFile, LineReader.FIONREAD, &available) >= 0 else {
return 0
}
return available
}
private func updateCursorPos(editState: EditState) throws {
if editState.requiresMatching() {
try self.refreshLine(editState: editState)
} else {
let cursorWidth = editState.cursorWidth
let numColumns = self.numColumns
let cursorRows = cursorWidth / numColumns
let cursorCols = cursorWidth % numColumns
var commandBuf = AnsiCodes.beginningOfLine
commandBuf += AnsiCodes.cursorDown(cursorRows)
commandBuf += AnsiCodes.cursorForward(cursorCols)
try self.output(text: commandBuf)
}
}
private func refreshLine(editState: EditState, decorate: Bool = true) throws {
let cursorWidth = editState.cursorWidth
let numColumns = self.numColumns
let cursorRows = cursorWidth / numColumns
let cursorCols = cursorWidth % numColumns
var commandBuf = AnsiCodes.beginningOfLine +
editState.promptProperties.apply(to: editState.prompt)
if decorate, let idx = editState.matchingParen() {
var fst = editState.buffer.index(before: editState.location)
var snd = idx
if fst > snd {
snd = fst
fst = idx
}
let one = String(editState.buffer.prefix(upTo: fst))
let two = String(editState.buffer[editState.buffer.index(after: fst)..<snd])
let three = String(editState.buffer.suffix(from: editState.buffer.index(after: snd)))
let highlightProperties = editState.readProperties.with(editState.parenProperties)
commandBuf += editState.readProperties.apply(to: one)
commandBuf += highlightProperties.apply(to: String(editState.buffer[fst]))
commandBuf += editState.readProperties.apply(to: two)
commandBuf += highlightProperties.apply(to: String(editState.buffer[snd]))
commandBuf += editState.readProperties.apply(to: three)
} else {
commandBuf += editState.readProperties.apply(to: editState.buffer)
}
let hints = decorate ? try self.refreshHints(editState: editState) : ""
commandBuf += hints.isEmpty ? " " : hints
commandBuf += AnsiCodes.clearCursorToBottom +
AnsiCodes.beginningOfLine +
AnsiCodes.cursorDown(cursorRows) +
AnsiCodes.cursorForward(cursorCols)
try self.output(text: commandBuf)
}
private func readByte() -> UInt8? {
var input: UInt8 = 0
if read(self.inputFile, &input, 1) == 0 {
return nil
}
return input
}
private func forceReadByte() -> UInt8 {
var input: UInt8 = 0
_ = read(self.inputFile, &input, 1)
return input
}
private func readCharacter() -> Character? {
var input: UInt8 = 0
_ = read(self.inputFile, &input, 1)
return Character(UnicodeScalar(input))
}
private func ringBell() {
do {
try self.output(character: ControlCharacters.Bell.character)
} catch {
// ignore failure
}
}
private func output(character: ControlCharacters) throws {
try self.output(character: character.character)
}
private func output(character: Character) throws {
try self.output(text: String(character))
}
private func output(text: String) throws {
if write(self.outputFile, text, text.utf8.count) == -1 {
throw LineReaderError.generalError("Unable to write to output")
}
}
private func setBuffer(editState: EditState, new buffer: String) throws {
if editState.setBuffer(buffer) {
_ = editState.moveEnd()
try self.refreshLine(editState: editState)
} else {
self.ringBell()
}
}
private func moveLeft(editState: EditState) throws {
if editState.moveLeft() {
try self.updateCursorPos(editState: editState)
}
}
private func moveRight(editState: EditState) throws {
if editState.moveRight() {
try self.updateCursorPos(editState: editState)
}
}
private func moveHome(editState: EditState) throws {
if editState.moveHome() {
try self.updateCursorPos(editState: editState)
} else {
self.ringBell()
}
}
private func moveEnd(editState: EditState) throws {
if editState.moveEnd() {
try self.updateCursorPos(editState: editState)
} else {
self.ringBell()
}
}
private func moveToWordStart(editState: EditState) throws {
if editState.moveToWordStart() {
try self.updateCursorPos(editState: editState)
} else {
self.ringBell()
}
}
private func moveToWordEnd(editState: EditState) throws {
if editState.moveToWordEnd() {
try self.updateCursorPos(editState: editState)
} else {
self.ringBell()
}
}
private func deleteCharacter(editState: EditState) throws {
if editState.deleteCharacter() {
try self.refreshLine(editState: editState)
}
}
private func moveHistory(editState: EditState,
direction: LineReaderHistory.HistoryDirection) throws {
// If we're at the end of history (editing the current line), push it into a temporary
// buffer so it can be retrieved later
if self.history.currentIndex == self.history.historyItems.count {
tempBuf = editState.buffer
} else if self.preserveHistoryEdits {
self.history.replaceCurrent(editState.buffer)
}
if let historyItem = self.history.navigateHistory(direction: direction) {
try self.setBuffer(editState: editState, new: historyItem)
} else if case .next = direction {
try self.setBuffer(editState: editState, new: tempBuf ?? "")
} else {
self.ringBell()
}
}
private func refreshHints(editState: EditState) throws -> String {
guard let hintsCallback = self.hintsCallback,
let (hint, properties) = hintsCallback(editState.buffer) else {
return ""
}
let currentLineLength = editState.prompt.count + editState.buffer.count
if hint.count + currentLineLength > self.numColumns {
return ""
} else {
return properties.apply(to: hint) + AnsiCodes.origTermColor
}
}
private func withRawMode(body: () throws -> ()) throws {
var originalTermios: termios = termios()
defer {
_ = tcsetattr(self.inputFile, TCSADRAIN, &originalTermios)
}
if tcgetattr(self.inputFile, &originalTermios) == -1 {
throw LineReaderError.generalError("could not get term attributes")
}
var raw = originalTermios
raw.c_iflag &= ~tcflag_t(BRKINT | ICRNL | INPCK | ISTRIP | IXON)
raw.c_oflag &= ~tcflag_t(OPOST)
raw.c_cflag |= tcflag_t(CS8)
raw.c_lflag &= ~tcflag_t(ECHO | ICANON | IEXTEN | ISIG)
// VMIN = 16
raw.c_cc.16 = 1
guard tcsetattr(self.inputFile, TCSADRAIN, &raw) >= 0 else {
throw LineReaderError.generalError("Could not set raw mode")
}
try body()
}
}