How to program a spinning cursor in c++ on OSX? - c++

I need a progress indicator in the form of a spinning cursor for a command line program. I read a couple of threads with the advice of using the backspace character \b to delete the last printed character. However, when using \b with cout on OSX, the result is some UTF-8 character (an upside-down question mark).
Does anyone know if there is a way to get this problem solved with standard c++ means?

You can use the ncurses library for cross-platform terminal access. You csn make nice text-only UIs using this.

Related

c++ bell sound in eclipse using '\a'

I am using Windows, Eclipse CDT and Mingw-w64. I want to call an alert ( aka bell aka beep) by using '\a' character literal.When I run this:
cout<<'\a';
computer doesn't produce any sound. I tried with endl, it didn't work. Sound on computer is on. I referred to this question, which technically asks the same thing, but it doesn't have any solution for Eclipse, it just tells that we need to try it in terminal, and tells that '\a' is not configured in Eclipse. My question is how can I force Eclipse to make the sound instead of printing square character?
P.S. I know there are other alternatives implementations for the beep sound, I want to use explicitly '\a'.
P.P.S In addition to alert literal, \b, \v, \f (backspace, vertical tab, formfeed) don't work as expected(maybe because output doesn't go to a printer), they print square chars and \r (carriage return) starts newline instead of going to the beginning of the current one.

Scrolling character letter in C++

I am building a part of a tower defense game in a strictly console environment and i am stuck at the moving of a creature lets say "c", i would like the letter "c" to start on the left and move a space at a time to the right on the same line basically:
c (one second later)
c (one second later)
c and so on....
i thought that this could be implimented with an array but am lost, i want to be able to use simple code, not weird libraries and weird methods, just simple as possible. Thank you
One method is display all the characters, then a carriage return ('\r') and then reprint the line.
This allows you to "walk" characters across. This will only work on video terminals that do not advance a line upon receiving a CR.
Another method would be to print 10 backspace characters, a space, then your 10 'c'. This may not be as fast as the carriage return method above, but worth looking at.
As others have said, you may want to look into a terminal library such as ncurses. The library allows you to position the cursor on the screen, based on the terminal type. This may require setting up the console window to emulate a terminal.

Linux Printing - How To

I find it hard to explain but I will try my best. Some times in Linux- in the Terminal- things get printed but you can still write over them. eg when using wget you get a progress bar like this:
[===================> ]
Now if you type something while it is doing this it will 'overwrite' it. My question is how to recreate this in c++.
Will you use something like
cout <<
or something else?
I hope you understand what I am getting at...
btw I am using the most recent version of Arch with xfce4
Printing a carriage return character \r is typically interpreted in Linux as returning you to the beginning of the line. Try this, for example:
std::cout << "Hello\rJ";
The output will be:
Jello
This does depend on your terminal, however, so you should look up the meaning of particular control characters for your terminal.
For a more cross-platform solution and the ability to do more complex text-based user interfaces, take a look at ncurses.
You can print the special character \b to go back one space. Then you can print a space to blank it out, or another character to overwrite what was there. You can also use \r to return to the beginning of the current output line and write again from there.
Controlling the terminal involved sending various escape sequences to it, in order to move the cursor around and such.
http://www.ibiblio.org/pub/historic-linux/ftp-archives/tsx-11.mit.edu/Oct-07-1996/info/vt102.codes
You could also use ncurses to do this.

alt+backspace to delete words in vim

How can I remap alt+backspace to delete words like native *NIX text manipulation? I checked out this thread: Using alt+backspace key in vim command line to delete by words
And the examples like: cmap <a-bs> <c-w> and :imap <A-BS> <C-W> don't do anything. And the accepted answer was actually to not even remap it, but to use ctrl+w. Since VIM's alt+backspace doesn't do anything I'd rather remap it to something I'm used to.
I'm using terminal based VIM (specifically in iTerm)
On macOS with iTerm2, I have the option keys mapped to +Esc (like many people), and I found that pressing Option+Backspace actually was interpreted by vim as an Escape press followed by a Backspace press, so the following binding worked perfectly for me; I recommend trying it even if your configuration is different than mine, just in case it works for you!
:imap <Esc><BS> <C-w>
The Alt/Meta key is problematic in Vim and most terminals, see this answer of mine for an overview of the situation (the situation is the same for Meta and Alt).
In short, Vim doesn't receive Alt at all: hitting Alt+Backspace is exactly the same as hitting Backspace.
Anyway, it will be better for you in the long term to learn and get accustomed to Vim's default key-mappings.
If you are on OSX, macvim uses the standard key bindings, so pressing Alt+Backspace will delete the entire word. Same goes for navigating between words with Alt+RightArrow and Alt+LeftArrow.

C++ standard output format

I want to create a C++ console application that print some text to different parts of the console. For example in QBasic you can use:
locate(8,5)
print "hi"
And hi would be printed in column 8 line 5. In C++ when I use cout it always prints on the next line, and begins printing in the first column.
Is there any way I can do this?
C++ itself does not have this feature, it's I/O model is a fairly simple, sequential one.
If you want to do fancy cursor positioning, you'll need to output (for example) control characters which your terminal will recognise as special commands (such as ANSI or VT escape sequences), or use a library like curses (see ncurses here) which can do a lot of the grunt work for you, not just cursor positioning but also things like text mode windows and so forth.
A library, like ncurses can help you do this.