Printing to a progress bar to the terminal from Java/C++ - linux

I want to update the percentage progress of my program on the screen.
I have a progress bar that I print. I add a * when the progress is more than 10%, but I also want to write the actual percentage correct up to every digit. However, I do not want to write a new number every time, since it will have the following effect:
1
2
3
4
Instead, I would like to just maintain one single figure on the screen that keeps getting updated.
How do I do that? (… in Java/C++, but I think it's irrelevant)

In C++ I believe you want to throw \r to bring you back to the beginning of the line (instead of \n) or you'll want to look at the curses library. Not sure, but you can probably do something similar in Java.

Outputting a \b will move the cursor back one column, and outputting a \r will return it to the first column. From there you can print the new value to be displayed.
std::cout << "111\r22\r3" << std::endl;

For C++, I think the curses offers textual GUIs or something... somewhere someone has already written a textual based progress bar, you should use their implementation so you don't have to do their work over again.
Also, if you didn't know it, java has a progress bar that you can use.
http://docs.oracle.com/javase/tutorial/uiswing/components/progress.html

Related

Sublime text multiple cursors?

Sublime Text is so damn advanced and this seems like such a stupid question, but...
I started writing a for loop in PHP (using SFTP), loved that it gave me a choice to auto-generate the loop. However, it enters this weird multi-cursor mode, which
1)I am not really sure how to use/exit without using the mouse;
2) it seems useless, seeing as all 3 type the same thing, even though I need to change, for example, the $i > x or $i = x.
Although Sublime does indeed support the idea of multiple cursors (which is an incredible time saver and useful as all get out, as we're about to see), what you're actually asking about here is a snippet which in this case happens to also include multiple cursors.
The general idea is that for code that you're likely to type many times (e.g. a for loop), you can create a snippet that will generate the bulk of the text for you in one shot, and then allow you to easily customize it as needed. In this case, the snippet in question is part of the default functionality of Sublime and is provided by the shipped PHP package.
To answer point #2 in your question first, this is far from useless. As seen here, I enter the text for and then press Tab to expand the snippet out. The first thing to notice here is that the status line says Field 1 of 4 to tell me that I'm in a snippet and that it contains four fields.
The first field is the name of the control variable for the loop, and all of them are selected so that as I change the name, all of them change at the same time because when there are multiple cursors, the text you type appears at all of them at the same time.
Once I'm done changing the name of the variable, I press Tab again to go to the next field, which allows me to easily change the point at which the loop starts. Another press of Tab takes me to the third field, where I can specify where the loop ends.
One last press of Tab exits the snippet and selects the text in the loop, so I can start writing my code (caveat: I am not a PHP developer).
At this point you can see Sublime offering another snippet for echo, which would expand out to an echo statement complete with quotes, then allow me to edit the text in the echo and skip to the end.
Circling back around to the first point in your question, you can use Esc at any point to jump out of a snippet and go back to regular editing. You can also use Tab or Shift+Tab to move through the fields in the snippet, and pressing Tab at the last field in the snippet exits it as well.
In this particular case, the first field in the snippet sets up multiple cursors, and so exiting the snippet while this field is active leaves multiple cursors in effect. You can jump back to a single cursor by pressing Esc one more time (this is true regardless of how you ended up with multiple cursors).

How is the fancy command line implemented in Linux?

When installing things in Linux I often see stdout change after printing eg. there might be a counter showing installation progress that starts at 1% and goes up to 100%. How is this done? When I write C programs and I print something using printf, I can't change it afterwards - if I type 1%, it stays that way. How is it done? Is there a different function I have to use?
\r brings you back to the beginning of the line without issuing the \n to go to the next line. Use this to overwrite text on the screen to build progress bars, etc.
See:
How to add a progress bar to a shell script?

What exactly does "printw" do? (Ncurses)

Could someone please tell me what exactly does printw do? I tried looking for information but just could not find anything.
5 seconds on Google revealed some nice documentation.
printw() class: Print formatted output similar to printf()
and
6.3.1. printw() and mvprintw
These two functions work much like printf(). mvprintw() can be used to move
the cursor to a position and then print. If you want to move the cursor first
and then print using printw() function, use move() first and then use printw()
though I see no point why one should avoid using mvprintw(), you have the
flexibility to manipulate.
Source - NCURSES-Programming-HOWTO
Type man printw (I suppose you are not programming with Windows).

PutS only displaying one character on TI-84 Plus Silver edition

I have recently discovered that my TI-84 plus silver edition can be programmed in hex. I have been messing around with it but have had a few bugs. Whenever I try to make a bcall to PutS it only prints one character and moves on. If I add a second PutS command it puts the second letter down and to the right by one. My current code is:
AsmPrgm
219D9D
EFD9481C
C9
48692100
Necro reply: It seems like you are using one of the new Math Print OSes. Because of the "pretty print" math, TI had to change how strings are printed to the screen. Unfortunately, they broke older functions that as _PutS. One way to get around it is to just put your calc into Classic mode. This disables math print, so printing characters to the homescreen will work as expected.
Go to [Mode],[down] 8 times, [right], [Enter]
It works on my 84+ too. There is nothing wrong with this program.
Possible solutions:
Check if you typed in the hexadecimal correctly.
Perhaps you are using a different OS version that has the bcall at a different location in memory. My os version is 2.43 (no mathprint)
Good luck!

QB64 Output issue

How do I scroll up and see previous displayed lines in the output?
I'm using QB64, and had to run a program 1000 times, but I cannot see the starting lines. Actually, I can't see the first 800 lines or so.
Is there a way to expand the window or scroll up to see everything? Because I do not have a scroll bar, and if I use any key to scroll up, it exits the console window.
Thanks in advance.
The new version of QB64 supports a console window, which should be able to do what you want. I'm not too used to console though, but if all else fails you could always save the text in a string array and examine that. There are some examples of creating lists in the community at qb64.net or you could make your own list with scrollbars, etc.
Input a line in the loop for data output:
if x > 24 then
if 24 MOD x then sleep
endif
MOD is the command for modulus where the modulus will return the remainder of 24 and x. in this case if it is zero then it will pause the console output until the user presses any key.
If you don't want to mess with custom buffers, just use the console:
$CONSOLE:ONLY
_DEST _CONSOLE
With that in the begining of your program, QB64 will create only a console window, which by default can be scrolled back so the past lines can be seen.

Resources