Home > Software engineering >  Is it possible to clear the console in swift?
Is it possible to clear the console in swift?

Time:01-29

I am creating a console application (coursework) and it would be nice to clear the console after going to the next screen

For example, after the user has selected 1 case, the console will be cleared and a new menu will appear on the clean console, and not under the previous text

import Foundation
func getValue() -> Int? {
guard let line = readLine(), let value = Int(line) else {
    return nil
}
return value
}

question: while true {
print("""
      1. Log in
      2. Create new user
      3. Quit
      """)
guard let value = getValue() else {
    continue
}
switch value {
case 1:
    print("you have selected number one")
case 2:
    print("you have selected number two")
case 3:
    print("Good bye")
    break question
default:
    print("Try again")
    // print("\u{001B}[2J") //<- Didn't work
}
}

As in this example, I would like the field to be cleared after each incorrect input

As in this example, I would like the field to be cleared and reappeared after each incorrect input

enter image description here

print("\u{001B}[2J") just get a lot of empty lines

enter image description here

CodePudding user response:

There are two appraoches i can think of that might work for you. The first one is already an answer to a similar question:

print("\u{001B}[2J")

The second way would to just print many newlines until the old output is off screen.

  •  Tags:  
  • Related