Skip to content

Commit f2fcf00

Browse files
committed
Support stripping of newline characters in LineReader.
1 parent b09ca26 commit f2fcf00

File tree

2 files changed

+18
-10
lines changed

2 files changed

+18
-10
lines changed

Sources/CommandLineKit/LineReader.swift

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -150,24 +150,30 @@ public class LineReader {
150150
/// throw an error if the terminal cannot be written to.
151151
public func readLine(prompt: String,
152152
maxCount: Int? = nil,
153+
strippingNewline: Bool = true,
153154
promptProperties: TextProperties = TextProperties.none,
154155
readProperties: TextProperties = TextProperties.none,
155156
parenProperties: TextProperties = TextProperties.none) throws -> String {
156157
tempBuf = nil
157158
if self.termSupported {
158159
return try self.readLineSupported(prompt: prompt,
159160
maxCount: maxCount,
161+
strippingNewline: strippingNewline,
160162
promptProperties: promptProperties,
161163
readProperties: readProperties,
162164
parenProperties: parenProperties)
163165
} else {
164-
return try self.readLineUnsupported(prompt: prompt, maxCount: maxCount)
166+
return try self.readLineUnsupported(prompt: prompt,
167+
maxCount: maxCount,
168+
strippingNewline: strippingNewline)
165169
}
166170
}
167171

168-
private func readLineUnsupported(prompt: String, maxCount: Int?) throws -> String {
172+
private func readLineUnsupported(prompt: String,
173+
maxCount: Int?,
174+
strippingNewline: Bool) throws -> String {
169175
print(prompt, terminator: "")
170-
if let line = Swift.readLine() {
176+
if let line = Swift.readLine(strippingNewline: strippingNewline) {
171177
return maxCount != nil ? String(line.prefix(maxCount!)) : line
172178
} else {
173179
throw LineReaderError.EOF
@@ -176,6 +182,7 @@ public class LineReader {
176182

177183
private func readLineSupported(prompt: String,
178184
maxCount: Int?,
185+
strippingNewline: Bool,
179186
promptProperties: TextProperties,
180187
readProperties: TextProperties,
181188
parenProperties: TextProperties) throws -> String {
@@ -196,12 +203,17 @@ public class LineReader {
196203
char = completionChar
197204
}
198205
if let rv = try self.handleCharacter(char, editState: editState) {
206+
if editState.moveEnd() {
207+
try self.updateCursorPos(editState: editState)
208+
}
209+
// It's unclear to me why it's necessary to set the cursor to column 0
210+
try self.output(text: "\n" + AnsiCodes.setCursorColumn(0))
199211
line = rv
200212
return
201213
}
202214
}
203215
}
204-
return line
216+
return strippingNewline ? line : line + "\n"
205217
}
206218

207219
private func completeLine(editState: EditState) throws -> UInt8? {

Sources/CommandLineKitDemo/main.swift

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,21 +61,17 @@ if let ln = LineReader() {
6161
return nil
6262
}
6363
}
64-
do {
65-
try ln.clearScreen()
66-
} catch {
67-
print(error)
68-
}
6964
print("Type 'exit' to quit")
7065
var done = false
7166
while !done {
7267
do {
7368
let output = try ln.readLine(prompt: "> ",
7469
maxCount: 200,
70+
strippingNewline: true,
7571
promptProperties: TextProperties(.green, nil, .bold),
7672
readProperties: TextProperties(.blue, nil),
7773
parenProperties: TextProperties(.red, nil, .bold))
78-
print("\nOutput: \(output)")
74+
print("Entered: \(output)")
7975
ln.addHistory(output)
8076
if output == "exit" {
8177
break

0 commit comments

Comments
 (0)