473,387 Members | 1,549 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

cursor positioning

Dear All,

I am writing a database import script in python and I would like to
print the percentage of the process to the last line. I would like to
update the last line at every percent. You know what I mean.

How can the cursor be positioned to the last line or somewhere else on
the screen? Curses starts with clearing the whole screen and it is
overkill. Many modules are on the net but I would like to resolve this
simply task with native python.

I tried:

for something:
print chr(8)*20+mystring,

but it is nasty and didn't work well.

Mage

Jul 21 '05 #1
4 1798
While not "curses" based this class will update screen as you
want. You could use it as a basis upon which to do a curses
version with your cursor positioning.

http://aspn.activestate.com/ASPN/Coo.../Recipe/299207

-Larry Bates

Mage wrote:
Dear All,

I am writing a database import script in python and I would like to
print the percentage of the process to the last line. I would like to
update the last line at every percent. You know what I mean.

How can the cursor be positioned to the last line or somewhere else on
the screen? Curses starts with clearing the whole screen and it is
overkill. Many modules are on the net but I would like to resolve this
simply task with native python.

I tried:

for something:
print chr(8)*20+mystring,

but it is nasty and didn't work well.

Mage

Jul 21 '05 #2
Larry Bates wrote:
While not "curses" based this class will update screen as you
want. You could use it as a basis upon which to do a curses
version with your cursor positioning.

http://aspn.activestate.com/ASPN/Coo.../Recipe/299207

Thank you. This is good for displaying the percentage.
However it fails to display this:
100 files read
200 files read
300 files read
.....

of course all in the same line and not under the last line.

Mage
Jul 21 '05 #3
Hi,

On Mon, 11 Jul 2005 15:29:41 +0200, Mage wrote:
Dear All,

I am writing a database import script in python and I would like to
print the percentage of the process to the last line. I would like to
update the last line at every percent. You know what I mean.

How can the cursor be positioned to the last line or somewhere else on
the screen? Curses starts with clearing the whole screen and it is
overkill. Many modules are on the net but I would like to resolve this
simply task with native python.

I tried:

for something:
print chr(8)*20+mystring,

but it is nasty and didn't work well.

Mage


If you only want to support ansi terminals (which is questionable, but
possible), then there are escape codes that are very helpful (searching
for ansi escape codes or something in google should help you find the
remainder):

the general syntax is

ESC[<parameter><action>

action usually is the first letter in the sequence, hence parameters are
usually numbers (duh :))

ESC is chr(27) (ascii 27, octal \033)

actions are
H cursor go home (top left corner usually)
C go right <parameter> times
D go left <parameter> times
A go up <parameter> times
B go down <parameter> times
K clear to end of line
2J clear screen (yes, to every rule there are exceptions :), note that
this does not make the cursor go home)
m set color/highlight/formatting flags

Examples
ESC[2JESC[H same as "clear", clear screen, go home
\rESC[Kprogress %d probably what you want :)

The downside of this is that determining the size of the screen is pretty
hard to do right.

Process is usually, read TERM environment variable, read /etc/termcap
(deprecated) "co" attribute (columns), "li" attribute (rows). That has
been deprecated because of all those resizeable terminals out there.

Now its something like outputting some magical stuff to make the terminal
emulator send back the current sizes immediately once, and whenever they
change.
I'm not totally clear how that works since I'm too lazy to care :)

What you want is probably

prc = 0
for prc in range(100):
sys.stderr.write("\r\033[KProgress %d%% ... " % prc)
sys.stderr.flush()
time.sleep(0.5)

though :)

Note that this can wreck havoc onscreen if the terminal is smaller than what is
needed to print that horizontally, so technically its not totally clean code.

Hope that helps

cheers,
Danny

Jul 21 '05 #4
Danny Milosavljevic wrote:
Hi,

Examples
ESC[2JESC[H same as "clear", clear screen, go home
\rESC[Kprogress %d probably what you want :)

Well, like the good old Commodore times :)
Thank you.

Mage
Jul 21 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Geoff Soper | last post by:
Can someone suggest how I can either position the cursor at the beginning of a specified line in a textarea or select the whole of a specified line in a textarea? Thanks, Geoff
25
by: JeffS | last post by:
Honest, I scoured the comp.lang.c.faq for this but found nothing. :) Is there a library function for placing the cursor position in the console? Or is it something that can only be done with a...
1
by: Dmitry Karneyev | last post by:
How to position cursor in Textbox after symbol I need?
3
by: Al-Burak | last post by:
I hope everybody is having a fantastic X-Mas, I will hate you for that since I have to be at the computer trying to keep my current job, grrr. :) OK, folks, jokes aside I have a question that...
2
by: paulsilver | last post by:
Hi, can someone please help me, I'd like to position a div under the mouse pointer when a link is clicked. I have something which'll do this in Firefox (see...
1
by: jobs | last post by:
I'm trying to understand this and don't get it... I found some code that looks like it's going to work if I can make sense of how to position the div. The code displays a div with some data...
12
by: whytwelve13 | last post by:
Is there a way to find all elements that are "under" some point on the screen in JavaScript? That is, given (x, y) find all elements of size (w1, h1) whose absolute position compared to the...
4
by: Billy | last post by:
Hi all, I'm building a text file from a database table using the ASP Write Method and would like to position the cursor in a specific column position before writing the fields. As I loop through...
1
by: MarcinD | last post by:
Hi all, DB2 on AS/390. I read from table using cursor: DECLARE SAMPLCUR1 CURSOR WITH ROWSET POSITIONING FOR SELECT COL1, COL2, COL3 FROM TABLENAME WHERE
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.