How do I clear the terminal screen in Haskell?

HaskellTerminalGhci

Haskell Problem Overview


How can I clear a terminal screen after my user has selected an option from my application's menu?

Haskell Solutions


Solution 1 - Haskell

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

Solution 2 - Haskell

This is what you may be looking for:

http://hackage.haskell.org/packages/archive/ansi-terminal/0.5.0/doc/html/System-Console-ANSI.html">ansi-terminal:</a></b> 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

Solution 3 - Haskell

Just press Ctrl+L (works on Windows)

Solution 4 - Haskell

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.

Solution 5 - Haskell

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

Solution 6 - Haskell

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.

Solution 7 - Haskell

A quick way on Windows would be to

import System.Process

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

Solution 8 - Haskell

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 ()

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionNubkadiyaView Question on Stackoverflow
Solution 1 - HaskellvoidView Answer on Stackoverflow
Solution 2 - HaskellZachSView Answer on Stackoverflow
Solution 3 - HaskellmitanessView Answer on Stackoverflow
Solution 4 - HaskellFederico SquartiniView Answer on Stackoverflow
Solution 5 - HaskellShuvam ShahView Answer on Stackoverflow
Solution 6 - Haskellsepp2kView Answer on Stackoverflow
Solution 7 - HaskellBorisView Answer on Stackoverflow
Solution 8 - HaskellStephane RollandView Answer on Stackoverflow