C++ - How to delete a newline that is printed to the console - c++

As the title says, I am wondering how to delete a newline that is printed to the console, if it is possible.
I have seen the following:
How to delete a newline using \b
Except that is about using \b to delete a new line, and I would just like to know how to do it, using any possible method.
More specifically:
If I have
----
- -
----
printed to the console, I would like to know how to delete the last two lines, to have just ----.
Thanks in advance!
Note: I left out an OS to see if I can get an answer that would work on any OS.

As I know, there is no portable way to work with not only current line of output,
but with several lines on console.
So you need some kind of wrapper around suitable functionality in each OS,
the most portable library for this, as I know is:
http://pdcurses.sourceforge.net/

Related

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.

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

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.

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.

Clearing terminal in Linux with C++ code

Okay, I have been researching on how to do this, but say I am running a program that has a whole bit of output on the terminal, how would I clear the screen from within my program so that I can keep my program running?
I know I can just type clear in terminal and it clears it fine, but like I said, for this program it would be more beneficial for me.
I found something that works, however, I'm not sure what it is or what it is doing.
cout << "\033[2J\033[1;1H";
That works but I have no clue what it is, if you could explain it, than I would much appreciate it.
These are ANSI escape codes. The first one (\033[2J) clears the entire screen (J) from top to bottom (2). The second code (\033[1;1H) positions the cursor at row 1, column 1.
All ANSI escapes begin with the sequence ESC[, have zero or more parameters delimited by ;, and end with a command letter (J and H in your case). \033 is the C-style octal sequence for the escape character.
See here for the full roadshow.
Instead of depending on specific escape sequences that may break in unexpected situations (though accepting that trade-off is fine, if it's what you want), you can just do the same thing you'd do at your shell:
std::system("clear");
Though generally system() is to be avoided, for a user-interactive program neither the extra shell parsing nor process overhead is significant. There's no problem with shell escaping either, in this case.
You could always fork/exec to call clear if you did want to avoid system(). If you're already using [n]curses or another terminal library, use that.
For portability you should get the string from termcap's cl (clear) capability (Clear screen and cursor home). (Or use std::system("clear") as told by Roger Pate).
man 3 termcap (in ncurses)
man 5 termcap
set | grep TERMCAP
you can write in a terminal "clear > data" and read in data the escapes sequance
0x1B[H0x1B[2J0x1B[3J
so
std::cout << "\033[H\033[2J\033[3J" ;

OutputDebugString + DebugView = not tabs!

I am dumping \t delimited data using using OutputDebugString and then use ex-Sysinternals DebugView to capture it.
The problem is that all the data in DebugView appear to be space delimited, hence I need to perfrorm CTRL+H "\x20" "t" to replace spaces with the tabs before I can use it (I really need tab delimited data).
Is there anyway to tell DebugView not to replace tabs with spaces?
Or maybe there is a better tool available to capture output of the OutputDebugString function?
Any ideas are very welcome!
It seems this is a "feature" in DebugView. I have tried with Hoo Wintail and this dude collects tabs without any problem. So I see 3 solutions:
You get Hoo Wintail (highly recommended)
You write your on tool (look here for some idea how to do it or even get a complete one)
You redirect to file.
I strongly vote for option 1.
Why not write them on a local log-file ? (only on debug mode ?)
You can use multiple spaces instead of a tab.
DebugOutput and DebugView are intended for situations as implied by their name: debug. They are not intended to replace file-save functionality.
You are probably in the situation where analyzing the debug output means analyzing the tab-delimited format. Find another character that can be used instead of tab, e.g. | or # or ^.
Then open the debug output in an advanced editor (e.g. UltraEdit) and convert the character back to Tab.