Reset screen point to the top of screen in Windows & Linux console - c

I have a little routine that's run under Linux and Windows written in C and displays output on the console. I'm not linking in any form of curses or anything like that.
Currently I clear the screen using
#ifdef __WIN32
system( "cls" );
#else
system( "clear" );
#endif
Then I have a bunch of printf statements to update the status. What I'd like just reset the screenpointer to 0,0 so I can then just overlay my printfs. I'd rather avoid compiling in any more extensions especially since I'm coding for 2 different OS'.

For Unix-like platforms, the usual way to do this is using the curses library.

Looks like I may have found a windows specific way of doing it SetConsoleCursorPosition
Ansi escape sequence \033[0;0H for Linux - just printf that to the console.

Yes, for unix platforms, curses (or ncurses, these days) is the way to go. And there are versions that work under windows, so you could do it the same way on both systems.

For windows - You can use ANSI escape characters.
http://www.lexipixel.com/news/star_dot_star/using_ansi_escape_sequences.htm
http://www.robvanderwoude.com/ansi.html
printf "\x[0;0H"
It used to be that Ansi.sys needed to be loaded before you could do this, but it's worth a shot.
Instructions for adding ANSI support
http://www.windowsnetworking.com/kbase/WindowsTips/WindowsXP/UserTips/CommandPrompt/CommandInterpreterAnsiSupport.html
Note: that Ansi.sys only works under command.com. You can't use it with cmd.exe

Related

save and clear terminal window in linux [duplicate]

I'm having a hard time even googling this, because I don't know the right keywords. Some command-line apps (such as vi and less) take over the whole console screen and present an interactive interface to the user. Upon exiting such an app, the screen is returned to the state it was in before the app was launched. I want to write a program that behaves in this fashion, but again, I don't even know what this is called, so I can't find any documentation for how it's accomplished.
So, my question is threefold:
What keywords can I use to find documentation on this?
If you are so inclined, links to such documentation would be helpful.
Lastly, can I accomplish this in a scripting language like Ruby, or even bash? I have no problem with C, but the environment I work in is more amenable to interpreted languages.
As said in some comments, you are looking for ncurses. The Linux Documentation Project have a very good HOWTO on ncurses for C that I used myself to start on it
https://tldp.org/HOWTO/NCURSES-Programming-HOWTO/
The feature you are describing is the alternate screen buffer. I think that [N]Curses will enable this by default. There are certainly curses bindings for Ruby, Python, and other scripting languages.
you can even access ncurses in bash by using the tput program. The whole ncurses library (like curses before it) works by sending escape sequences to the terminal. The xterm program emulates a vt100 terminal (and also a Tektronic terminal) and there were various combinations of characters which would move the cursor, clear the screen, draw various characters etc. These would generally start with an escape character, hence the name: escape sequence. You also sometimes see these escape sequences in people's PS1 shell variables with the \e to provide the escape character; often used to colour the prompt or set the window title.
tput refers to the terminfo database to figure out what the escape sequences are to perform the functions you've asked it to do.
see the manual page, type:
man 5 terminfo
for more details

system("cls"); not working in C language

system("cls") is no working in C language . I added conio.h header file but it always say cls not found. I am using xcode. But same code executes perfectly on visual studio.
That's because system("cls"); has nothing to do with the c language.
The conio.h header is as far as I know an old MS-DOS header, you can't use it portably. The system() function executes external programs from within a c program, cls is an MS-DOS program to clear the text buffer of a MS-DOS console.
In your picture, it's clear that you are not executing the program in an MS-DOS console so it wont work.
Using external programs is almost always a bad idea, except if those programs are guaranteed to be installed with your's1. The reason is that any program that relies on other programs being available in the target environment, will fail when the external programs aren't.
I understand that it's easy to see a lot of code using non standard tricks like system("cls"), but if you find good learning resources that will not be the case. Try to study each and every function you learn and determine whether or not it's a standard function and a good practice to use it the way you see it.
1TeX distributions work like that, they are just several programs that exchange text, following very closely the UNIX Philosophy. But they are all distributed together.
You are trying to run cls command (to clear the screen) on the console using the system(). cls command only exists on DOS or command prompt on Windows.
system("cls");
If your program is running on Bash on Linux or MacOSX, you can try clear.
system("clear");
It's not working because the system() is a library function of stdlib.
You need to use #include<stdlib.h> in order to use system("cls") in C.

Print Unicode characters in C, using ncurses

I have to draw a box in C, using ncurses;
First, I have defined some values for simplicity:
#define RB "\e(0\x6a\e(B" (ASCII 188,Right bottom, for example)
I have compiled with gcc, over Ubuntu, with -finput-charset=UTF-8 flag.
But, if I try to print with addstr or printw, I get the hexa code.
What I`m doing wrong?
ncurses defines the values ACS_HLINE, ACS_VLINE, ACS_ULCORNER, ACS_URCORNER, ACS_LLCORNER and ACS_LRCORNER. You can use those constants in addch and friends, which should result in your seeing the expected box characters. (There's lots more ACS characters; you'll find a complete list in man addch.)
ncurses needs to know what it is drawing because it needs to know exactly where the cursor is all the time. Outputting console control sequences is not a good idea; if ncurses knows how to handle the sequence, it has its own abstraction for the feature and you should use that abstraction. The ACS ("alternate character set") defines are one of those abstractions.
A few issues:
if your program writes something like "\e(0\x6a\e(B" using addstr, then ncurses (any curses implementation) will translate the individual characters to printable form as described in the addch manual page.
ncurses supports line-drawing for commonly-used pseudo-graphics using symbols (such as ACS_HLINE) which are predefined characters with the A_ALTCHARSET attribute combined. You can read about those in the Line Graphics section of the addch manual page.
the code 0x6a is ASCII j, which (given a VT100-style mapping) would be the lower left corner. The curses symbol for that is ACS_LRCORNER.
you cannot write the line-drawing characters with addstr; instead addch, addchstr are useful. There are also functions oriented to line-drawing (see box and friends).
running in Ubuntu, your locale encoding is probably UTF-8. To make your program work properly, it should initialize the locale as described in the Initialization section of the ncurses manual page. In particular:
setlocale(LC_ALL, "");
Also, your program should link against the ncursesw library (-lncursesw) to use UTF-8, rather than just ncurses (-lncurses).
when compiling on Ubuntu, to use the proper header definitions, you should define _GNU_SOURCE.
BTW, maybe I'm probably arriving somewhat late to the party but I'll give you some insight that might or not shed some light and skills for your "box drawing" needs.
As of 2020 I'm involved in a funny project on my own mixing Swift + Ncurses (under OSX for now, but thinking about mixing it with linux). Apparently it works flawlessly.
The thing is, as I'm using Swift, internally it all reduces to "importing .h and .c" files from some Darwin.ncurses library the MacOS Xcode/runtime offers.
That means (I hope) my newly acquired skills might be useful for you because apparently we're using the very same .h and .c files for our ncurses needs. (or at least they should be really similar)
Said that:
As of now, I "ignored" ACS_corner chars (I can't find them under swift/Xcode/Darwin.ncurses runtime !!!) in favour of pure UTF "corner chars", which also exist in the unicode pointspace, look:
https://en.wikipedia.org/wiki/Box-drawing_character
What does it mean? Whenever I want to use some drawing box chars around I just copy&paste pure UTF-8 chars into my strings, and I send these very strings onto addstr.
Why does it work? Because as someone also answered above, before initializing ncurses with initscr(), I just claimed "I want a proper locale support" in the form of a setlocale(LC_ALL, ""); line.
What did I achieve? Apparently pure magic. And very comfortable one, as I just copy paste box chars inside my normal strings. At least under Darwin.ncurses/OSX Mojave I'm getting, not only "bounding box chars", but also full UTF8 support.
Try the "setlocale(LC_ALL, ""); initscr();" approach and tell us if "drawing boxes" works also for you under a pure C environment just using UTF8 bounding box chars.
Greetings and happy ncursing!

C commandline app : how to programatically move cursor

I am writing a commandline calculator in C, and it would be very useful if it had the ability to move the cursor each time you close (for example) a parenthesis highlighting the previous one, like in emacs, etc.
For example, if you launch emacs, and type:
(blah)
then for a while after you type the closing parenthesis, the first one is highlighted.
I've tried some googling, but I don't really know what to search for.
Is there a simple and multiplatform (at the very least it's fine if it'd work on Linux, but I'd like it to work at least on Windows as well) way to move the cursor in this way?
If you want better control over the console, take a look at the ncurses library.
The Linux console can also be controlled through console codes. No libraries needed, just printf the appropriate codes to stdout.
The things you should search for are 'termcap', 'terminfo' or 'curses.'
ncurses should be able to do what you're asking for.
Check out ANSI escape codes. They are pretty basic, but a good place to start. The upshot is that they work for most terminals (Linux and Windows).

More Control Over C Standard Output

I can't seem to find the right way to ask the almighty google...
In such programs as a command-line progress bar, the output buffer seems to be directly manipulated. It can't print a character to a terminal in any place it wants. How is such control over a program's output controlled in standard C? Is there a special library that I can look up?
look at curses
it`s a lib for unix/linux
If you just want a progress bar, you can just print a single 'X' for every 2% completion. This should fill 50 characters on a line.
If you want something more fancy, on Linux you can try the classic "curses" library, or if you just want a dialog box, you could try the library that the Debian install utilities use, but I forget its name.
you can do the progress bar with \r check this
for doing more advanced stuff you can use ncurses
It's not part of standard C. These things work by writing some special character sequences that are recognized by the terminal emulator which takes care of the cursor positioning and stuff.
The big gorilla here is the ncurses library, but you can do a lot of cool stuff with less learning curve. Try using \r to move to beginning of line and using simple control sequences to clear to end of line, turn bold on and off, etcetera. The tput(1) command is invaluable. For example, I wrote a simple application the does highlighted text, and to turn highlighting on and off I just called the commands tput smso and tput rmso. You can capture the results using C with popen(3); using a shell it is even easier.
You could use ANSI escape coding to control the termincal output. This is how a lot of MUD games do their output.

Resources