如何清除哈斯克尔的终端屏幕?

在用户从应用程序的菜单中选择了一个选项后,如何清除终端屏幕?

47861 次浏览

On Unix systems you can do System.system "clear" which just invokes the command-line utility clear. For a solution that does not depend on external tools, you'd need a library that abstracts over different terminal-types like for example ansi-terminal.

This is what you may be looking for:

ansi-terminal: Simple ANSI terminal support, with Windows compatibility

You can find it in Hackage and install using cabal install ansi-terminal. It specifically has functions for clearing the screen, displaying colors, moving the cursor, etc.

Using it to clear the screen is easy: (this is with GHCI)

import System.Console.ANSI

clearScreen

:! run the shell command
:! cls under windows
:! clear under linux and OS X

On a terminal that understands ANSI escape sequences (I believe every term in Unix/Linux systems) you can do it simply with:

clear = putStr "\ESC[2J"

The 2 clears the entire screen. You can use 0 or 1 respectively if you want to clear from the cursor to end of screen or from cursor to the beginning of the screen.

However I don't think this works in the Windows shell.

A quick way on Windows would be to

import System.Process


clear :: IO ()
clear = system "cls"

Under Linux (Ubuntu at least), that's the code I use for clearing the terminal:

import qualified System.Process as SP


clearScreen :: IO ()
clearScreen = do
_ <- SP.system "reset"
return ()

Just press Ctrl+L (works on Windows)

On Windows, use Ctrl + L for Haskell command prompt terminal. And, for GUI use Ctrl + S.