Printing an Ascii Spinning "Cursor" in the Console

Printing an ASCII spinning cursor in the console

Yes, this works on Windows, OS X, and Linux. Improving on Niklas' suggestion, you can make this more general like so:

def show_wait_cursor(seconds,fps=10)
chars = %w[| / - \\]
delay = 1.0/fps
(seconds*fps).round.times{ |i|
print chars[i % chars.length]
sleep delay
print "\b"
}
end

show_wait_cursor(3)

If you don't know how long the process will take, you can do this in another thread:

def show_wait_spinner(fps=10)
chars = %w[| / - \\]
delay = 1.0/fps
iter = 0
spinner = Thread.new do
while iter do # Keep spinning until told otherwise
print chars[(iter+=1) % chars.length]
sleep delay
print "\b"
end
end
yield.tap{ # After yielding to the block, save the return value
iter = false # Tell the thread to exit, cleaning up after itself…
spinner.join # …and wait for it to do so.
} # Use the block's return value as the method's
end

print "Doing something tricky..."
show_wait_spinner{
sleep rand(4)+2 # Simulate a task taking an unknown amount of time
}
puts "Done!"

This one outputs:

Doing something tricky...|
Doing something tricky.../
Doing something tricky...-
Doing something tricky...\
(et cetera)
Doing something tricky...done!

Print spinning cursor in a terminal running application using C

You could use the backspace character (\b) like this:

printf("processing... |");
fflush(stdout);
// do something
printf("\b/");
fflush(stdout);
// do some more
printf("\b-");
fflush(stdout);

etc. You need the fflush(stdout) because normally stdout is buffered until you output a newline.

How to create a spinning command line cursor?

Something like this, assuming your terminal handles \b

import sys
import time

def spinning_cursor():
while True:
for cursor in '|/-\\':
yield cursor

spinner = spinning_cursor()
for _ in range(50):
sys.stdout.write(next(spinner))
sys.stdout.flush()
time.sleep(0.1)
sys.stdout.write('\b')

Cooler ASCII Spinners?

Balloons...

. o O @ *

Problem with ASCII Rotating Cursor (TUI) Animation

The output effect is not guaranteed but dividing it into sections might help.

Idea:

#include <algorithm>
#include <chrono>
#include <cstddef>
#include <iostream>
#include <string>
#include <thread>

using namespace std::chrono_literals; // a cumbersome way to be able to write 100ms

// unformatted output of one char, a backspace, flushing and Zzz...
void slow_put(char ch) {
static const auto delay = 100ms;
static const char bs = '\b';
std::cout.write(&ch, 1);
std::cout.write(&bs, 1);
std::cout.flush();
std::this_thread::sleep_for(delay);
}

// newline, but erase the char under it first
void nl() {
std::cout.write(" \n", 2);
}

// proxy ... decorate stuff here (for debugging etc)
void display(char ch) {
slow_put(ch);
}

// enabler to repeat a bunch of sequences
void spinner(const std::string& sequence) {
// execute the display function for one char at a time
std::for_each(sequence.begin(), sequence.end(), display);
}

// example use of the helpers above
void spinningCursorSchema(size_t times) {
static const std::string seq = R"raw(|/-\)raw"; // the pattern to display

while(times--) spinner(seq);
// add more patterns to this schema if wanted
}

int main() {
std::cout << "Spinner: [ ]\b\b";
spinningCursorSchema(100); // run the spinningCursor 100 times
nl(); // erasing newline
}

Edit: A brief explanation:

For every group of functions you called that I felt could be named, "do_this()" or "do_that()" I put them in a function and named them accordingly.

The purpose of this exercice was not primarily for me to find the error in your code but to provide a frame of reference. It's easier to identify/talk about/fix problems when one can say "your do_this() function needs this or that..." or similar. When everything is in one big code block everyone reading it needs to start from zero. A function with a clear name (or a comment to compensate for the poor naming as I did above, "proxy") and only a few lines of code can be reviewed by everyone without much background knowledge about the bigger problem.

In short, I took your code that was specifically created to do one thing and broke it down into a few functions that I used to rebuild something similar to your original idea. By doing that I made it easier to see flaws, talk about design decisions. If a function of 2-4 lines can be reviewed by 10000 people and they don't see a problem, it's very unlikely that that function is the problem (in itself). By building blocks of small functions with a clear purpose, bughunting becomes a lot more available for everyone, not only people deeply involved in the particular problem you're solving.

I now see that I used a function template from <algorithm> that may be unfamiliar: std::for_each. In this case, it could be replaced by:

for(char ch : sequence) display(ch);

Come to think of it, that's even clearer.

If this wasn't the overview you sought, just comment and point out the parts you want to get explained and I'll try again

display multi-line python console ascii animation

Hi the problem with using '\b' is that it only clears a single character.

The following code with system('cls') function works for me on the cmd console of a windows system.
If you are on a mac or linux system use system('clear')

I tried with two sample files:

counter = 1
while True:
with open('myfile{}.txt'.format(counter), 'r', encoding="utf8") as f:
print(f.read(), sep='', end='', flush=True)
time.sleep(0.5)
if counter == 2:
counter = 1
else:
counter += 1
system('cls')

Content of one file is printed then the screen is cleared and contents of the next file is printed.

Move one character to the left in the console

It depends on the terminal type and connection, but you can usually assume ANSI cursor movement, so cursor-left is ESC + '[' + 'D':

print "The cursor should be between the arrows: -> <-\e[D\e[D\e[D"
readline

See http://ascii-table.com/ansi-escape-sequences.php for more information.

How to make a loading animation in Console Application written in C++?

You can use the backspace character ('\b') to go back and overwrite characters on the console. You'll also need to flush the output after each change, otherwise the output might stay in a buffer and not appear on the console.

Here is a simple example:

#include <iostream>
#include <unistd.h> // for sleep()

int main()
{
std::cout << '-' << std::flush;
for (;;) {
sleep(1);
std::cout << "\b\\" << std::flush;
sleep(1);
std::cout << "\b|" << std::flush;
sleep(1);
std::cout << "\b/" << std::flush;
sleep(1);
std::cout << "\b-" << std::flush;
}
}

How to make cursor rotate

I don't properly understand what you mean by How to make cursor rotate? But were you by any chance wanting to do something like this:

#include <stdio.h>  
#include <time.h>

#define mydelay 100

void delay(int m)
{
clock_t wait = m+ clock();
while (wait > clock());
}


int main()
{
while(1)
{
printf("\\\b");
delay(mydelay);
printf("|\b");
delay(mydelay);
printf("/\b");
delay(mydelay);
printf("_\b");
delay(mydelay);
}

return 0;
}


Related Topics



Leave a reply



Submit