0

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

wupiupi
  • 59
  • 6

2 Answers2

0

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.

Chris
  • 180
  • 1
  • 14
  • 2
    He's already tried to print the escape code (see the commented out line at the bottom of his code) – JeremyP Jan 28 '22 at 09:27
  • My bad, I'm blind. – Chris Jan 28 '22 at 09:28
  • if I run this through the terminal, I get a lot of empty lines. And I meant is it possible to clean it? :) – wupiupi Jan 28 '22 at 09:29
  • @wupiupi It is the correct control sequence to clear the screen for the macOS terminal but, if you scroll up you will still be able to see the old output. There's nothing you can do about that. Try editing a file in `vi` and then scrolling up - same problem. – JeremyP Jan 28 '22 at 09:48
  • @JeremyP oh... sad but true – wupiupi Jan 28 '22 at 09:51
  • @wupiupi Something that might help is after clearing the screen, move the cursor to the top left of the screen. The escape sequence for that is `print("\u{001B}[1;1H", terminator: "")` - if you don't put the terminator in, the `print` appends a new line so your text appears on the second line – JeremyP Jan 28 '22 at 09:57
  • @JeremyP can you explain please what terminator: "" means? – wupiupi Jan 28 '22 at 10:10
  • 1
    @wupiupi https://developer.apple.com/documentation/swift/1541053-print – Joakim Danielson Jan 28 '22 at 11:25
  • @wupiupi It's the optional `terminator:` argument. The default for that argument is `"\n"` which is why `print("foo bar") outputs a newline at the end of printing the text. – JeremyP Jan 28 '22 at 13:04
  • 1
    @wupiupi In my example, I set the terminator to an empty string `""`. This stops `print` from putting a newline on the end. – JeremyP Jan 28 '22 at 13:06
0

I made the following function:

func CLS() {
  print ("\u{001B}[2J") // clear the screen
  print ("\u{001B}[0;0H", terminator: "") // jump to 0, 0
} //end func: CLS()

print ("hi")
CLS()
print ("there")
Timothy G.
  • 6,335
  • 7
  • 30
  • 46