CharlyDelta
CharlyDelta

Reputation: 938

Overwrite text written to console

I want to achieve the following: I have a program which generates files, and it should print a warning if the file with the given name already exists, asking the user if it should be overridden. It looks like the following:

Processing file                     test.sdf
Checking SDF file...                [OK]
Parsing SDF...                      [OK]
Generating NDDL model file...       [WARN]
Warning. The file "/home/chris/models/test-model.nddl" already exists. Overwrite? [y/N] 

Now, if the user decides what to do, I want to rewind the console cursor to the [ character of [WARN], and overwrite it with [OK] or [FAIL], and then overwrite the following lines with the next outputs of the program.

I found that I could achieve this with ANSI control sequences. Since I am just using Ubuntu I'm okay with that.

I came up with two ideas:

1) Rewind the cursor until I find the string [WARN] and then begin to write again.

2) Move the cursor up line by line until the line Generating NDDL model file... [WARN] is removed, and overwrite it with for example Generating NDDL model file... [FAIL].

But with both approaches I have a problem which I just cannot solve or find a way googling...

Problem with 1): I can't figure out how to read the character at the current cursor position. But anyway, I don't think that this would be a good idea. It just doesn't seem to be reliable.

Problem with 2): Since the path of the input file can be arbitrary, I have no control over the amount of lines which have been printed after the [WARN] appears, so I just don't know how much std::cout << "\033[F" << "\033[2K" << std::flush; I should run (ANSI Control Sequences for move-cursor-one-line-up and clear-all-contents-in-line). Also, I don't know how big the width of the terminal window is, so I can't calculate it either (no idea if this would be a good idea though...)

I am sure that there must be a way to achieve this, but I just can't figure out a good and reliable way to do so...

Does anybody of you got an idea? I appreciate every kind of help

Upvotes: 0

Views: 1151

Answers (2)

user177800
user177800

Reputation:

X/Y Problem

If you insist on manipulating the console directly, then why not just back over the entire last two lines completely? Why try and edit a line when you can just reprint the entire thing. Also you are not guaranteed to be able to manipulate every console the same way between platform and shells.

You are printing the lines out you should easily know how many to back up and overwrite. If you can't do that, just clear the screen and write everything back out again.

Solution

The correct way to present an interface like this is with acurses library ( or equivalent ) and control the output to the console completely. That gives you complete control.

If you think this is overkill then you are just doomed to recreate the functionality of curses a bit of a time and end up with a mess at the end.

Upvotes: 3

Carlos H Romano
Carlos H Romano

Reputation: 626

Well, there are some possibilites using the carriage return (\r), which will take you to the start of the current line, or by using the backspace (\b), which will rewind one character position and then you just have to write all over again. No curses. No ANSI crazyness.

Upvotes: 0

Related Questions