Skip to content

Commit 471c455

Browse files
committed
Enabling field maxCharacter limit
1 parent 35109a6 commit 471c455

1 file changed

Lines changed: 16 additions & 13 deletions

File tree

Sources/Window.swift

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -166,24 +166,27 @@ public class Window {
166166
let key = keyboard.getKey(window:self)
167167
switch key.keyType {
168168
case .isCharacter:
169-
// Update the string
170-
string.insert(key.character!, at:insertionPoint)
169+
// Only allow insertion if we're below the maxCharacters permitted
170+
if (string.count < maxCharacters) {
171+
// Update the string
172+
string.insert(key.character!, at:insertionPoint)
171173

172-
// Move the insertion point forward
173-
insertionPoint = string.index(after:insertionPoint)
174+
// Move the insertion point forward
175+
insertionPoint = string.index(after:insertionPoint)
174176

175-
// Write the chracter and move the cursor forward
176-
write(key.character!)
177+
// Write the chracter and move the cursor forward
178+
write(key.character!)
177179

178-
// Print the remaining characters
179-
if insertionPoint < string.endIndex {
180-
let remainingCharacters = String(string[insertionPoint...])
180+
// Print the remaining characters
181+
if insertionPoint < string.endIndex {
182+
let remainingCharacters = String(string[insertionPoint...])
181183

182-
// Write the remaining characters
183-
write(remainingCharacters)
184+
// Write the remaining characters
185+
write(remainingCharacters)
184186

185-
// Move the cursor back
186-
cursor.position = cursor.position.offsetBy(xOffset:-remainingCharacters.count, yOffset:0)
187+
// Move the cursor back
188+
cursor.position = cursor.position.offsetBy(xOffset:-remainingCharacters.count, yOffset:0)
189+
}
187190
}
188191
case .backspace:
189192
if insertionPoint > string.startIndex {

0 commit comments

Comments
 (0)