How can output be directed to specific coordinates in the linux terminal? - c

To be more specific, I am looking for an equivalent to the Windows API function: WriteConsoleOutputCharacter. I know this style of direct output is possible, as I have seen the behaviour used before in the time that I have used Linux. However, I am still a baby when it comes to Linux specific C libraries, and have not been able to ascertain any clues as to where I may find such functionality. Any assistance would be appreciated.

Check out ncurses library.
It allows you to create some text-based UI in terminal. It is more than you asked, but if you need more than only this one function it may be best option for you.

You can use the ANSI control sequence
Say, following will print 'ddd' string 3 lines above the prompt in bash
echo -e "\e[3Addd"

This line will put a clock in right upper conner of a terminal (from commandlinefu.com)
while sleep 1;do tput sc;tput cup 0 $(($(tput cols)-29));date;tput rc;done

Related

Is there a way to get help for some C functions inside of vim/Neovim?

This question may be a little off topic. But I was wondering if there was a way for me to look at the descriptions of C functions using vim or neovim. Is it possible to look at their documentations by doing something like :help? This would really be helpful since I wouldn't need to lookup to my browser everytime.
I am unclear about these things:
Can :help be my friend here ?
Can I use LSPs to do something like this ?
I am using latest Neovim inside Ubunutu 20.04 in WSL. Is this helpful somehow ?
By pressing K, the keyword under the cursor is looked up using a configured keyword lookup program, the default being man. This works pretty much out of the box for the C standard library.
For C++, you might want to look at something like cppman.
Well yes, you can get the description of C functions by using a LSP (language server plugin)! Here is an image of me using clangd as my LSP:
You'd "just" need to install the LSP and start it. I don't know how familiar you're with neovim, but just in case if you don't know how to install a plugin or to be more specifique: If you don't know how you can install a LSP server, then you can do the following:
There're plenty videos how to set up a LSP-Server for your language. Here's an example.
If you don't want to set up on your own, you can pick up one of the preconfigured neovim setups (some of my friends are recommending lunarvim)
But yeah, that's it. If you have any further questions feel free to ask them in the comments.
Happy vimming c(^-^)c
Let's explain how "K" command works in more detail.
You can run external commands by prefixing them with :! command. So running man tool is as easy as
:!man <C-R><C-W>
Here <C-R><C-W> is a special key combination used to put word under cursor from text buffer down to command line.
Same for showing Vim's built-in help page
:help <C-R><C-W>
As it feels tedious to type that, Vim also defines K Normal mode command that does pretty much the same thing. Except the tool name is taken from value of an option named "keywordprg".
So doing set keywordprg=man (default for *nix systems) makes K to invoke !man tool; while set keywordprg=:help is for bultin help.
Also, the option :h 'keywordprg' is made global or local-to-buffer, so any Vim buffer is able to overwrite global setting. For example, this is already done by standard runtime for "vim" and "help" buffers, so they call ":help" instead of "man".
The problem with :!man command is that it shows "black console". It'd be nice if we could capture man's output and open it inside Vim just like a builtin help page. Then we could also apply some pretty highlighting, assign key macros and all such. This is a pretty common trick and it is already done by a standard plugin shipped with Vim/Neovim.
A command that the plugin provides is called :Man, so you can open :Man man instead of :!man man, for example. The plugin is preactivated in Neovim; for Vim you still need to source one file manually. So to make use of this plugin you'll need something like this
set keywordprg=:Man
if !has("nvim")
source $VIMRUNTIME/ftplugin/man.vim
endif
The previous answer recommending cppman is the way to go. There is no need to install a bulky language server just for the purpose of having the hover functionality. However, make sure you're caching the man pages via cppman -c. Otherwise, there will be a noticeable delay since cppman is fetching the page from cppreference.com on the fly.
If you like popups for displaying documentation, convert the uncompressed man pages (groff -t -e -mandoc -Tascii <man-page> | col -bx), and set keywordprg to your own wrapper to search for keywords according to your needs.

C - How to change font size in Ncurses?

Is there anyway? I cant seem to find any function that does this. I tried googling this but could not find anything.
I do not believe it is possible. The terminal is not for such things. It is meant for displaying text in sometimes varying colors. If you want to change the font size, you would need to open a window and draw to it (That might not actually be the correct term. Graphics aren't my forte). If this is what you want to do, I suggest looking into sdl. It is fairly simple to set up and is easy (IMO) to use. And because I know stackoverflow doesn't like flamewars, I am by no means saying it is the best. Im sure there are plenty of alternatives that are just as good. I just have not used them
If the text is being displayed in an xterm then it can be changed.
Edit .Xresources and add (probably at the top):
xterm*font: *-fixed-*-*-*-20-*
xterm*geometry: 80x24+5+5
This specifies a font size of 20 with a 80x24 charactor xterminal window located in the upper left of the display (widthxheight+xoffset+yoffset)
I was unable to use a font larger than 20. I start the application from a terminal window with the command line below on Ubuntu 14.04 with XFCE GUI.
xterm -e my_c_app
Well, it can be done if you are running on Windows command windows. I did that.
Ncurses will use your console windows settings: colors, size and typeface.
This works, but it is a somewhat basic mode of use. If needed you can even change the character attributes by program, before initializing ncurses terminal.
I tried with monospaced typefaces without any problems, tried even with proportional typefaces. It works also, but here visual quality is lesser due to the "entangling" of some characters, because ncurses keeps spacing in monospace mode.
I didn't try (yet) to change the character mode "in flight", but believe that this is possible. But i think that you can use only one mode/size for a time, that is: you cannot mix typefaces / sizes at the same screen.
There's no way with ncurses, but certain terminals support changing fonts. For xterm, for example:
$ echo -e '\x1b]50;-misc-fixed-medium-r-normal--20-200-75-75-c-100-iso10646-1\a'
XTerm Control Sequences
Change Terminal Font Size with C++

Bolding text in console output

For extra credit, the professor wants us to use bolding and/or underlining to text output in the current project.
The example he gave was b\bb o\bo l\bl d\bd is displayed as b o l d
Following that example, I marked up SPACE as
printf("\033[7mS\bSP\bPA\bAC\bCE\E- move forward one page\033[0m");
I'm also implementing reverse video by enclosing strings within \033[7m and \033[0m fields. The reverse video inverts the colors of the line appropriately, but doesn't seem to be affecting the bolding, since both strings with and without the reverse video are not bolding.
Could it be the standard shell used in Ubuntu 10.10 that is at fault?
I agree about using curses, but given your starting point ....
I think you want to use the 'bright' feature of VT100 for the bold, ESC[1m
You can probably find better doc on VT100 codes, but using this page I found the codes. ANSI/VT100 Escape Codes
I hope this helps.
Unless you're just trying to be masochistic, try using curses (or ncurses) instead.
// warning: Going from distant memory here...
curs_attron(A_INVERSE); // maybe A_REVERSE? I don't remember for sure.
curs_addstr("SPACE - move forward one page");
curs_attroff(A_INVERSE);

Using Visual Studio 2010, how do I provide input to a Visual C++ Win32 Console Application?

I am using Visual Studio 2010 Pro for simple C programming, I would like to know how I can provide input to the program without having to manually do so. The enviroment I am used to working is your standard commandline Unix enviroment. Once I compile a C file call "inputsInts" it becomes "a.out" and to test input I would type:
The easy way
echo 1 2 3 4| ./a.out //to provide input
The number of ints input was 4 //output
The easier way
more input.txt| ./a.out //to provide input
The number of ints input was 4 //output
The tedious way
./a.out
//now I would manually type
1 2 3 4 s //in this case I have to type a letter to move on
The number of ints input was 4 //output
Hard is how I have to do it in Visual Studio 2010. I would like to be able to just input in an area the input ahead of time or at least have it read a text file. Obviously I can't test large sets of data by manually typing it in. At the moment I am just doing the coding in VS2010 and going to the unix enviroment to do most testing. I would like to stay in the VS2010 enviroment until I am ready to do a final test in Unix.
I have altered the question quite a bit since I first posted, so the original answers may seem off a bit. Again I appretiate everyone's time and help.
This is just the simple code for an example:
#include
int main () {
int x, n = 0;
while (scanf("%d", &x)==1)
n++;
printf("The number of ints input was %d\n", n);
return(0);
}
You need to create a "Console Application" when you start a new Visual Studio project. This will give you a program that runs from a Windows Command Prompt window, otherwise known as the Cmd window after the name of the shell program that runs underneath it. The command window is located under Programs->Accessories in Windows XP, not sure about other versions of Windows. Once you open a command window, things will work similarly to what you're used to on Linux.
cd MyProject
echo 1 2 3 4|.\MyProject.exe
MyProject.exe <input.txt
The cmd.exe shell has a pipe operator that works similar to the Unix pipe operator. It has some quirks in some versions of Windows but in general, you should be able to do many of the same things with it.
You can run your program pretty much the same way at the Windows command line, the only obvious difference being that you need to specify the correct executable name instead of a.out.
To do roughly the same from inside the VS IDE, you'd probably need to store your sample input in a text file, and then specify something like < sample.txt as the arguments to supply to the program in the project's debug settings.
In the Windows Shell (cmd.exe) you can use a pipe similar to linux for commands like
dir|more
Outside the shell, you're talking about a GUI environment (as in Linux's GUI) so passing info from one program to the next will be a bit different.
However, you can achieve similar functionality using pipes (shared memory in Windows). See here for a full explanation with examples from the folks at Microsoft Developer Network:
http://msdn.microsoft.com/en-us/library/aa365780%28v=VS.85%29.aspx
Or if you're feeling to lazy to poke around, here's an example of transactions on named pipes:
http://msdn.microsoft.com/en-us/library/aa365789%28v=VS.85%29.aspx
...or you can simply dump and read from output files.
(Both of these methods are similar to those used with Linux programs)
More info on your specific needs would be helpful.

Which numerical values do the F-Keys ( F[1-12]) and the Arrow-keys have?

I'd like to write an application in C which uses arrow-keys to navigate and F-keys for other functions, such as saving to a file, language selection, etc.
Propably the values depend on the platform, so how could I find out which values the keys have?
If they don't, or if you know them, I don't have to know how to find out;)
Edit:
My platforms are Linux and M$ Windows.
Therefore, I'm looking for a solution as portable as possible.
(Propably something like
#ifdef __unix__
#define F1 'some number'
/* ... */
#define ARROW_UP 'some other number'
#elif __WIN32__ || MSDOS /*whatever*/
#define F1 'something'
/* ... */
#define ARROW_UP 'something different'
#endif
)
I think that depends on $TERM, but either way it's going to be a sequence of characters. I get this:
% read x; echo $x | od -c --
^[[15~
0000000 033 [ 1 5 ~ \n
0000006
That's my F5 key, and apologies for this being a *nix-centric answer, if that's not your platform.
This problem is a lot messier than anyone would like. In brief, each of these keys sends a sequence of characters, and the details depend on which terminal emulator is being used. On Unix, you have a couple of choices:
Write your app to use the curses library and use its interface to the terminfo database.
Parse the the terminfo database yourself to find the sequences. Or to use an API, look at the sources for tput to see how this is done.
Use the command-line program tput to help discover the sequences. For example, to learn the sequence for F10, run tput kf10 | od -a. Keep in mind this changes from terminal to terminal, so you should run tput each time you run your app.
Write your application using one of the X libraries and get access to 'key symbols' rather than a sequence of characters. the XLookupKeysym function is a good place to get started; you'll find names of the symbols in /usr/include/X11/keysymdef.h. If you are going to connect over the network from non-X systems (e.g., Windows) this option is not so good.
I have no idea how to do any of this on Windows.
If you're on a Unix or Linux machine, simply enter cntl-V and then the key while in vim*. You'll see the code.
HTH
cheers,
Rob
This handy site has lots of useful nuggets of info on the raw scancodes:
http://www.win.tue.nl/~aeb/linux/kbd/scancodes.html#toc1
Some of the keys generate 2 character key codes.
One technique I have successfully used on the pc, is to get the 2 characters, and set the 7th bit on the second character, and return that character as the answer. It lets the rest of the program treat each keystroke as a single character.
The solution to your problem with depend on what keys you need to support. Hiding the translation behind a function of some sort, is generally a good idea.

Resources