What libraries are needed for graphics like vim or nano? - c

What library is used to make a static terminal window like vim, nano, irssi or aptitude as opposed to a scrolling terminal?
I'm using C, but I'd be interested in libraries for other languages (for example, a C++ specific library).

I believe the library you are looking for is Curses (or NCurses). There is also PDCurses for cross-platform (includind Win32) development.
I also remember the days of Conio on DOS based systems.

Curses like library are usually used for that. pdcurses for windows, most *nixes come with a native version, or e.g. ncurses

That would be ncurses.

You can also use the termcap library (curses provides this) to lookup the terminal control strings for the terminal you're connected to, and send them yourself to implement whatever you like. Or, if you don't mind requiring a modern terminal that supports at least a common subset of the ANSI/ECMA standard, you can simply hard-code the standard terminal escapes.

Related

Cross-Platform API to monitor file system in C?

I'm looking for a cross platform library to detect when files in a directory are added or modified.
I know there are OS specific way to do this (inotify for Linux, FindFirstChangeNotification for windows, etc...).
But is there a platform independent library that works specifically in C? (Like the QFileSystemWatcher in C++)
inotify is Linux specific, if you want some UNIX portable features you are pobably looking for something like libfam. it is name of library. Full package name is fileschanged.
fileschanged is a GNU/Linux command-line utility that reports when files have been altered.
It's now 2021 so maybe septag/dmon is your cup of tea.
From the description:
Single header C99 portable library for monitoring directory changes
... or maybe the more beefy fswatch ?
Sounds like a good use case for golang.
Simply change $GOOS and/or $GOARCH and run go build.
Boom - trivial cross-platform development.

Using Terminal Control Codes with C

I am interested in creating a terminal-based text interface in C without the use of a library like ncurses. I know that through using tput and a variety of escape codes, it is possible to create such an interface. However, I am uncertain of how to use tput or similar commands in C.
First, I am wondering what the best option is for implementing something like this in C without external libraries (so it can be compiled and run on a bare bone system).
Second, if using tput is the best option, how can I call these commands from C?
I understand that using a pre-existing library, such as ncurses would greatly simplify the process, but I would like to create my program without them.
Thank you in advance.
The standard C library does not provide any functions that can be used instead of the functions in ncurses. You'll have to use ncurses or another third party library that provides the equivalent functionality.

conio.h is missing from Windows

I usually use VS but trying cygwin for the first time. I am using windows 7 but on compiling hello world program using gcc, it says "fatal error: conio.h: no such file or directory".
I am using Windows 7 and it seems conio.h is missing from my system. Can someone please tell me how to resolve this issue.
Thanks!!
In Cygwin there doesn't exist any such header file called conio.h! Also, you don't need it either because it automatically holds screen for you without using getch() and for clrscr() you do have system("clear") in Cygwin!
conio not being part of the standard library, you cannot expect it to be available cross-platform, or even between compilers on the same platform.
Being, non-standard, the name conio has been used by both Borland and Microsoft for libraries with differing APIs - Microsoft's is much smaller. So for that reason you might avoid it for portability.
It is not a matter of conio not being on Windows, Cygwin is a POSIX API layer and tool-chain for building and running POSIX code on Windows. The libraries provided with it are independent of those provided with Visual Studio.
There are a number of solutions including:
Use an alternative console I/O library, such as ncurses.
Use a conio source code implementation for Linux such as this (which uses ncurses and implements Borland's API).
The second solution is perhaps useful if you have a lot of legacy code using conio, but is overkill if you just want to prevent a console windows from closing. For that you could just use getchar() in any case and accept that you will have to press enter rather than any key.
If you are using Cygwin just to be able to use GCC on Windows, you might be better off using MinGW/GCC instead. This uses Microsoft's C runtime rather than GNU, and the Win32 API rather than POSIX.

Clearing screen and kbhit() function

I got some problems writing my snake game program. I need to make game working on linux and windows. I found some topics how to clear screen on linux and windows using the #ifdef Windows etc. The problem is i need to use C89 standard, and im not sure that system("cls") is in C89. Could you help me with finding C89 functions to clear screen, and tell me something about kbhit() function on linux? Sorry for my english, and thanks for help.
C89 does not have terminal handling functions. Instead, you should use OS specific functions. So you need to have, say, a source file only for windows functions and another for linux. Another option is to use a cross platform library. I would choose ncurses for this task:
http://www.tldp.org/HOWTO/NCURSES-Programming-HOWTO/
It works on any unix system, including linux and Mac OS. For windows versions, see:
Is ncurses available for windows?
With ncurses, you have functions like erase() and clear() to clear the screen.
On Unix-liked systems including Linux and macOS, you can use ncurses library (POSIX API). In case of Windows (or even Linux or macOS), the following code will work on ANSI terminals on any systems.
printf("\033[2J\033[H");
/* or */
printf("\033[0;0f");

How to write an application that uses the terminal as GUI? (in C)

I'd like to write an application (in C) that uses the terminal of a *nix OS as its GUI. I mean an application of the kinds of emacs, vi etc. that take up the whole terminal window and jump back to the input prompt after quitting.
How would you go about doing that, preferably in C? Can someone point me to a tutorial covering that?
You need to use ncurses:
http://en.wikipedia.org/wiki/Ncurses
http://tldp.org/HOWTO/NCURSES-Programming-HOWTO/
It is available on all major distros.
Well, actually this is not GUI (graphic user interface) but a text based interface. You can use the library ncurses to create such applications in C.
Use a library like ncurses, it is specifically designed for this purpose.
Throwing in alternate solutions so that this question thread does not look so monotonic:
the slang library (mc uses it, for example)

Resources