Some time ago I had a similar question to you, how all these unix programs can make so fancy stuff with the terminal and how I could use this myself, and like you the first thing I've found was the crt unit.
Well I am not a big fan of the crt unit and I will explain later why, but because of that I went out to try and figure out how this stuff works.
Generally speaking, every operating system and every terminal emulator does this differently. On DOS you basically get a buffer that is what is represented in the console, if you update the buffer the screen updates at the positions accordingly. On windows this is replaced with more traditional api calls, but because windows is backwards compatible to the stone ages, it also supports the dos style. But most modern terminals use so called escape sequences, basically if you write special commands to the output the terminal will interpret this and make things happen.
Each of these different approaches have different capabilities and this is one of the reason why I don't like crt, crt is designed to work on pretty much every system supported by the FPC, therefore it only supports the subset of functions that is available on all platforms.
The second problem is that it tries to simulate some of the functionality not available on the plattform (e.g. the WhereX on Linux by simply counting the characters printed and lines and stuff), for doing this crt basically intercepts every read and write operation, which has the side effect that simply by including crt in the uses clausel, the behavior of your program will change (significantly in some cases) even if you don't use any functionality of crt, which is something I really don't like when using a library of which I only need a few functions.
So whats the alternative? Well there are a few things that work on any terminal. The CR char (#13) will move the cursor the the beginning of the current line and the backspace char (#8) moves the cursor one char backwards.
When you write, you write at the current position of your cursor and override anything that is at that position. So if you move your cursor to the start of the line and write something you override the old line.
So if your goal is to only rewrite the last line, you don't need any special libraries or escape sequences, just don't write a newline at the end of the line and when overriding start your string with #13:
Write('This line will be replaced');
Write(#13, 'With this line!');
But let's say you wanted to do a more complex application, like for example wget which has like 4 lines to display the current state of the download, you must use something more advanced.
You could do so by using crt, but as I said it's not the best library, but if you don't have a problem with not supporting systems like 16 bit DOS or amiga, you could simply go by using escape sequences, as these are currently supportet on all major operating systems (windows, linux, macos, bsd). Also by doing this you have much more possibilties than with the crt unit (e.g. full 24bit true color support)
The great thing about escape sequences is, they are very easy to implement even without a specialized library, simply write a given string into the console and the magic happens
E.g. to override the last 4 lines:
WriteLn('Line 1');
WriteLn('Line 2');
WriteLn('Line 3');
Write('Line 4'); // no newline so we have to go back 1 line less
Write(#27'[3D'); // #27 + '[' is escape sequence, 4D is 3 lines up
WriteLn('Line 1 New');
WriteLn('Line 2 New');
WriteLn('Line 3 New');
To move in any direction us #27'[XY' where X is the number of cells you want to move and Y is A to move to the left, B to move to the right, C to move down and D to move up, so 3D means move 3 up. It will automatically stop at the first visible line (i.e. the first line in the window) so you can't override older lines.
After moving your cursor where you desire, simply rewrite the line with the new content and you are done.
This is what I would recommend if you want to do very simple stuff, no libraries needed, just a few writes. If you want to learn more about escape sequences, the Wikipedia article is quite nice:
LinkSo what if you want to do more complex stuff, like really interactive console applications such as vim or emacs. Then you could use crt, but again here due to the fact that crt only implements the subset of functionality available on all platforms this also limits your possibilities.
Thats why I a few years ago build my own library that specializes on Escape sequences, it can basically everything crt can, but better because it doesn't need to worry about systems I won't ever use. If you want to take a look at it, check it out:
LinkThere is one exception to that statement, it can't track the cursor position (WhereX, WhereY of CRT) the reason for this is, that this is actually really hard to do and the main reason why crt has to do all these naughty things with the input and output files changing program behavior. Basically because I wanted to keep my library seperate from the default readln writeln functionality as good as possible there is no way for it to track the current cursor position. If you want to do this you need to implement that functionality for yourself.
But it is pretty powerful, for example the colortest example shows that you can easiely repaint the whole screen at 30-60 fps (60 on linux, 30 on windows because windows console is very slow) so you can easiely use this to print animations or play games (with a very poor resolution, but it works)
There is just one thing you always need to keep in mind when working with escape sequences, the terminal scrolls when your cursor hits the last line. So having one WriteLn instead of a write at the end, might scroll past the first line, resulting in that line not being accessible anymore (as i said you can only move to the window border)