haskellanimationterminalascii-art

How to efficiently make ASCII art in terminal with Haskell?


This is not a duplicate of Output ascii animation from Haskell?, as that post was, in spite of the title, about how to ensure a minimal pause between frames; regarding the actual drawing, the OP considers

straightforward using putStrLn together with clearScreen from the ansi-terminal package, found via this answer.

What I'm wandering is: do I really have to clearScreen and re-putStrLn every line in the terminal, even if, say, only a handful of characters in the whole terminal change?

Think about implementing snake in a terminal, where most of the time each frame would differ from the previous one by two characters. I think it would be much more efficitent to just update individual characters at the desired positions on the screen, rather than re-render the whole terminal.

What options do I have in Haskell?


No, I'm not looking for ncurses. I'm just curious to know how I can do animations in a terminal, by clearing the screen once, and then just setting/clearing individual screen positions.


Solution

  • You overwrite characters in the terminal by moving the cursor to the position you want then writing the character.

    The ansi-terminal package has functions like setCursorPosition for this.

    Alternatively you can look up the codes and write them yourself. For example this will write 'x' at 5, 5.

    putStr "\ESC[5;5Hx"