Bolding text in console output - c

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);

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.

How to make text colored in c lang in cmd?

how to make text colored in c lang?
The example is given in the image included.
This may be realised via ANSI escape sequences, although the effect depends on the terminal application used. For example to change the text to red use:
puts("\033[31m");
For further information visit Wikipedia, which has decent reference for all codes.

How to underline text using printf in C

I note the question Colorful text using printf in C gives a good example of setting coloured text on the standard console output in Windows. Is there something similar that allows output to be underlined? Or possibly even bolded or italicised?
EDIT: I tried Lundin's answer on using COMMON_LVB_UNDERSCORE with no luck. Attempting to use AddFontResource() to add arial italic font to try italics gives an error that there is an undefined reference to __imp_AddFontResourceA
It is not possible to do so using any standard C functions, as the C language doesn't even recognize the presence of a screen.
With Windows API console functions you can change colors, underline and some other things. The particular function you are looking for is called SetConsoleTextAttribute just as in the post you linked. Change its attributes to include COMMON_LVB_UNDERSCORE.
You might run your program in some environment with a terminal accepting ANSI escape codes.
(I never used Windows - since I am using Linux only -, so I don't know how to set up such environment in Windows; but I heard that it is possible)
With ANSI escape codes, underlining is "\e[4m" with \e being the ASCII ESCAPE character.
Perhaps try using termcaps. Something like this (after initializing termcaps) :
printf(tgetstr("us", NULL)); /* underline on */
printf(""/* your string */);
printf(tgetstr("ue", NULL)); /* underline off */
or more concise :
printf("%s/* your text here */%s", tgetstr("us", NULL), tgetstr("ue", NULL));
https://www.gnu.org/software/termutils/manual/termcap-1.3/html_node/termcap_34.html
The '\r' sequence differs from the newline ('\n') in that it moves the cursor to the
beginning of the current line of output, not to the beginning of the next line. Using
'\r' gives a program the ability to create a file containing more than one character
in one line position.
This prints an underlined text
printf("\f\t\t\tFinal Report\r\t\t\t____________\n");

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

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

Field not found when searching in ILE C programs under debug mode

While I was working on an ILE C program under debug mode, I intended to search the occurrences of a field through the program. This field is part of a structure, so I included the structure variable name in the search as well. But the search did not always work. It displayed the message field not found occasionally, even if that variable was right in front of my eyes. And this happened again when I tried to use that field to set up a conditional breakpoint. Does anyone know why this happened? And how to solve this issue?
If you are doing the search in the ILE debugger using the F command, be sure to start at the top. The ILE Debugger will search from the current point, or sometimes from the last search result. Using T to go to the top of the source will reset that and then you can use F to find again from the top.
Are you taking account of the fact that C is case-sensitive? If you don't use the exact case of the variable name then the variable will not be found; certainly on the conditional breakpoint.

Resources