LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 05-01-2007, 07:41 PM   #16
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 70

And then my question (and I think the question of all those wondering why you need to revert to vt escape sequences) is: What’s wrong with something like this:
Code:
for x in `seq 1 100`; do
	printf "< %3d > || [" $x
	for i in `seq 1 40`; do
		if [ $(($i * 100 / 40 )) -le $x ]; then
			printf "*"
		else
			printf " "
		fi
	done
#	printf "]\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b"
	printf "]\r"
	sleep 0.25
done
printf "\n"
As you see the row of backspaces has been replaced by a carriage return. As you can also see NO VT ESCAPE SEQUENCES.

P.S., I’m sure someone else can find a way to do all that in one line or something

Last edited by osor; 05-01-2007 at 09:40 PM. Reason: typo
 
Old 05-01-2007, 08:36 PM   #17
BlueSpirit
Member
 
Registered: Jun 2006
Distribution: Slackware
Posts: 144

Original Poster
Rep: Reputation: 15
This script is excellent too...

Only that As I said, I dont want a loop which rewrites the model each time...

Last edited by BlueSpirit; 05-01-2007 at 08:47 PM.
 
Old 05-01-2007, 08:47 PM   #18
BlueSpirit
Member
 
Registered: Jun 2006
Distribution: Slackware
Posts: 144

Original Poster
Rep: Reputation: 15
Okay, I have no more questions...

Last edited by BlueSpirit; 05-01-2007 at 08:53 PM.
 
Old 05-01-2007, 09:10 PM   #19
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 70
Quote:
Originally Posted by BlueSpirit
I dont want a loop which rewrites the model each time
I must have missed that… why not?
 
Old 05-01-2007, 09:15 PM   #20
BlueSpirit
Member
 
Registered: Jun 2006
Distribution: Slackware
Posts: 144

Original Poster
Rep: Reputation: 15
BTW too... I'm almost sure ur script won't write the last * b/c of the -lt...


Try it, see... Me I can't even paste it b/c I don't have firefox yet... But Lynx.

Last edited by BlueSpirit; 05-01-2007 at 09:17 PM.
 
Old 05-01-2007, 09:40 PM   #21
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 70
Quote:
Originally Posted by BlueSpirit
I'm almost sure ur script won't write the last * b/c of the -lt
Nice catch… changed it
 
Old 05-01-2007, 10:18 PM   #22
Zention
Member
 
Registered: Mar 2007
Posts: 119

Rep: Reputation: 16
Thumbs up

>>> Now, what I would like to know is a way to write (with printf or something else) a sort of character which is equivalent of 1 space, but that DOES NOT erease the "hello65"...

I am going to assume you want a forward space.

printf "hello\b\033[1Cthere\n"

Now, I had a little trouble tracking this down, so I will put in my thinking:

printf the program is a wrapper over printf the function.

printf works with ansi.

ascii is a subset of ansi.

so \033 is the ESC (in hexadecimal) Decimal 27 in ASCII.

ESC[1C is the ANSI escape sequence for right 1 column.

so \033[1C is your forward space character.

Of course the terminal has to be ansi compliant.

If you want to be more compliant, then you need to maintain the state in some way.

so:

Code:
StrMult() {
    # print a line of characters

    for (( k = 0; k < ${1}; ++k ))
    do
        printf "%s" $2
    done
}
#-----------------------------------------------------------

BS() {
    # backspace number of characters

    for (( k = 0; k < ${1}; ++k ))
    do
        printf "\b"
    done
}
#-----------------------------------------------------------

Main() {
    # entry point

    msg="A Message "

    printf "%s" "$msg"

    toggle=1
    for (( c = 0; c < 40; ++c ))
    do
        if [[ toggle -eq 1 ]]
        then
            StrMult $c "+"
        else
            StrMult $c "-"
        fi
        (( toggle *= -1 ))

        sleep 0.1

        BS $c
    done

    echo
}

Main
#-----------------------------------------------------------
So, that is a sort of flavour you can play with.

Last edited by Zention; 05-01-2007 at 10:25 PM.
 
Old 05-01-2007, 11:48 PM   #23
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 369Reputation: 369Reputation: 369Reputation: 369
I'm glad you got it working.

Some advice to avoid problems in getting help in the future.

Don't make the people trying to help you fight to get information from you. Why did you not come out immediately and say:
Quote:
Originally Posted by BlueSpirit
Well, now some ppl won't stop asking this thing so I will answer them : I want to make something that will act LIKE a loading...

Here is the complete model :

Quote:
< 100% > || [**************************************]
That's a normal thing to want to do. I've tried to do that in the past. Just say what you're trying to accomplish. Not only will you get answers more quickly, you may get suggestions on how to do it better.



Also:
Quote:
Originally Posted by BlueSpirit
I did a lil search on ESC sequences and for this it is OK. No problem. But I will still use my tabspacecharf() function, b/c as I read, ESC sequences are not universal...
I brought up that point earlier (in reply #6 of this thread):
Quote:
Originally Posted by Dark_Helmet
With terminal control sequences, you are limiting portability to one (or a handful) of terminals that support the code(s) you decide to use.


And lastly:
Quote:
Originally Posted by BlueSpirit
Two -> This is NOT a C program, only a BASH shell, and b/c BASH is an easy language, im not sure they give hws at university for that... C is NOT very difficult too, as soon as I will buy a book...
Nobody has suggested a C program (from my reading of the responses). You might have been referring to the use of printf. There is a printf() function in C, yes, but there is also a printf command to execute from a command prompt. The printf command functions in much the same way as the C printf(), but because it's a command, it can be used in shell scripts.




I'm sure you probably feel like I'm being a jerk...maybe I am. But whenever I see a post from a user asking for help but the user refuses to explain why something must be the way they say, it makes me assume someone else (i.e. an instructor) has imposed that requirement.

Just be open about your question. Don't play "hide the ball" if you get a question from someone trying to help. Then you won't run into people like me that will stop short of giving you the final answer because they don't know if it's a homework assignment or not.

Last edited by Dark_Helmet; 05-01-2007 at 11:50 PM.
 
Old 05-02-2007, 02:25 AM   #24
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,282

Rep: Reputation: 172Reputation: 172
did you not try what's in my last post?
that should be all you need.
 
Old 05-02-2007, 11:49 AM   #25
BlueSpirit
Member
 
Registered: Jun 2006
Distribution: Slackware
Posts: 144

Original Poster
Rep: Reputation: 15
Well, I'm sorry... No, you didn't act like a jerk Dark_Hamlet, only that I thought if I would have writed the reason why I want to do this, ppl would just have said : This is totally useless to do this... Or, you are ridiculous to make a prog like that...


But I admit i was wrong...


Also, YES bigbearsbilly, that's your tutorial about ESC sequences that I read. Before, I could not figure what the hell was the ESC in ESC[nC, but I read later that it was the ascii num utilised to do different category of things.
 
Old 05-02-2007, 12:37 PM   #26
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 70
Quote:
Originally Posted by BlueSpirit
I dont want a loop which rewrites the model each time
I still don’t see what’s wrong with that.

As an aside, this method (i.e., using a carriage return rather than vt escape sequences) is used for the progress bars found in wget, e2fsck, bar, pv, Term-ProgressBar, and ruby-progressbar. Of course none of these are written in “bash”, but four of them in C, one in perl, and one in ruby. In fact, I was looking for a progress bar in a “real, live program” which did use escape sequences or ncurses but was unable to find one (NOTE, this doesn’t mean such a program doesn’t exist). The closest thing I could find was bar which uses escape sequences for ANSI colors.
 
Old 05-02-2007, 05:17 PM   #27
cfaj
Member
 
Registered: Dec 2003
Location: Toronto, Canada
Distribution: Mint, Mandriva
Posts: 221

Rep: Reputation: 31
Quote:
Originally Posted by BlueSpirit
Code:
printf "%5s" hello65
printf "\b\b\b\b\b"
printf "%15s" world77
printf "\n"
...
Now, what I would like to know is a way to write (with printf or something else) a sort of character which is equivalent of 1 space, but that DOES NOT erease the "hello65"...

Code:
var1=hello65
var2=world77
printf "%s\r" "$var1"
## sleep 2 ## uncomment to see what happens
printf "\e[%dC%s\r" $(( 15 - ${#var2} )) "$var2"
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
"printf" slow down benchmark result syseeker Programming 1 06-07-2006 05:55 AM
serial port I/O? what is "\r" in printf blackzone Programming 2 08-16-2004 05:24 AM
Couldn't display "SMB:///" , No host "(Null)" could been Found?????? munkey Linux - Newbie 3 05-03-2004 04:30 PM
Couldn't display "SMB:///" , No host "(Null)" could been Found?????? help munkey Fedora 6 04-30-2004 06:04 PM
RH "null" beta system freezes right after printing "INIT" markus1982 Linux - Hardware 0 09-09-2002 03:31 PM


All times are GMT -5. The time now is 09:37 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration