如何清除 Swift 中的终端屏幕? [英] How to clear the Terminal screen in Swift?

查看:31
本文介绍了如何清除 Swift 中的终端屏幕?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为 Swift 2 中的命令行编写 BASIC 解释器,但我找不到实现简单命令 CLS(清除终端中的所有文本)的方法.我应该简单地循环打印空格,还是有一个我不知道的功能可以清除终端屏幕吗?

I am writing a BASIC Interpreter for the Command Line in Swift 2, and I cannot find a way to implement the simple command, CLS (clear all text from the Terminal.) Should I simply print spaces in a loop, or is there a function I'm not aware of that would clear the Terminal screen?

推荐答案

使用内置的 clear 命令或者 system

Use the built-in clear command either with system

system("clear")

popen(询问 Google)

or popen (ask Google)

或者,通过命令行使用 AppleScript 模拟按下 Ctrl+L:

Alternatively, simulate the pressing of Ctrl+L using AppleScript via the command line:

osascript -e 'tell app "terminal" to tell app "system events" to keystroke "l" using {control down}'

system 在较新的 Swift 版本中不再可用.请参阅鲁道夫·亚当科维奇的回答.

system is no longer available in newer verions of Swift. See Rudolf Adamkovič's answer.

这篇关于如何清除 Swift 中的终端屏幕?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆