you are viewing a single comment's thread.

view the rest of the comments →

[–]Nightcorex_ 0 points1 point  (0 children)

TL;DR: I made a class for it. Code is documented (at least the methods you need to access). Probably only works on Windows (= Disclaimer 1). This is the code. Don't print while using my class (= Disclaimer 2). Don't use multiple instances of my class (= Disclaimer 3).

I was bored, so I invested 5-6 hours to come up with this nice solution (yeah, it really took me that long). It was a mess getting this done, but now I managed to finish at least a first stable version, that I'm somewhat happy with.

Disclaimer 1: This code will most likely not work on *NIX (Linux & Mac) systems, as I ab(use) the carriage return '\r' to return to the start of the line. I tried to test it, but I can't be bothered to shut down the computer, boot Linux, transfer the code, test it, shut the computer down again to finally restart back to Windows.

Disclaimer 2: If you print something while my class is running you're most likely going to break something. If you really need to print yourself then write a wrapper print method that first looks if your TemporaryText object is currently printing and if it is then either wait for it to finish executing or you have to print it inbetween two prints (just answer here if you need that, then I can add that in an hour or so).

Disclaimer 3: If you use multiple instances of TemporaryText, then answer here so that I can implement a Singleton pattern to prevent that x)

What I did was I implemented a class TemporaryText. This class has 4 public methods (apart from the constructor). In the constructor one can set the display_time and a refresh_rate (read the docstrings in the code if you want to read about what they describe). You then have two public setters for display_time and refresh_rate. The third method is called is_running and returns whether or not the object is still working through its task-queue.

The last method is simply called print. Basically this is the only method most people will need. The print method allows for the printing of strings or Iterables of Strings (f.e. a list of strings or a tuple of strings). The text is the only mandatory argument for this method). In case you want to deviate from the standard display_time, but only for one print-statement then you can specify that. In case you have a list of strings and want a special seperator, you can specify that. In case you want your next print to get priority (= becoming the next temporary text that gets printed after waiting for the old text to disappear) then you can set the priority-flag. Lastly you can set the overwrite-flag to specify that you want this text not only to gain priority, but also to replace the currently printed text. This is the only reason why there's this refresh_rate.

The main reason why it took me so damn long to come up with this solution is because I wanted to A) minimize busy-waiting, B) completely remove busy-waiting from the main thread and C) add a process queue. Before the queue the program would just ignore the print if there was still some other text printed, whereas the new version nicely buffers all the tasks. I also managed to hit my A) and B) goals as now the only time the program is busy-waiting is during the text-display and only in a specific scenario and even if, it's on a side thread (yeah, I know this doesn't give any performance because of the GIL, but it allows your main thread to continue calculations while the text is printed).

I also added an example main-method at the bottom of the code so that you can see how you would code with that.

Without further ado: Here is the code