# Overprinting characters

**URL:** <https://www.ruby-forum.com/t/overprinting-characters/146322>\
**Category:** Ruby\
**Created:** [August 20, 2008, 12:25am UTC](https://www.ruby-forum.com/t/overprinting-characters/146322 "2008-08-20T00:25:14Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![bouldr-matt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/85e7bf/32.png) [@bouldr-matt](https://www.ruby-forum.com/u/bouldr-matt)\
**Post date:** [August 20, 2008, 12:25am UTC](https://www.ruby-forum.com/t/overprinting-characters/146322/1 "2008-08-20T00:25:14Z")

</div>

I have a script that does things to large files \> 1Gb and I want to be  
able to show the percentage through the current file.

I know exactly how I will get the percentage, but I want to know how to  
display it in the terminal.

What I would like is the name of the current file, then on the next  
line, “currentbytes/totalbytes [xx%]” and I want it to update the  
characters in place without writing hundreds of lines to the terminal.

I think I want to be able to position the cursor back to the beginning  
of the current line but I have no idea how to do this in ruby.

Grateful for any ideas

Thanks

Matt

---

<div class="post-metadata">

**Author:** ![bouldr-matt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/85e7bf/32.png) [@bouldr-matt](https://www.ruby-forum.com/u/bouldr-matt)\
**Post date:** [August 20, 2008, 1:21am UTC](https://www.ruby-forum.com/t/overprinting-characters/146322/2 "2008-08-20T01:21:26Z")

</div>

Matt H. wrote:

> I think I want to be able to position the cursor back to the beginning  
> of the current line but I have no idea how to do this in ruby.
> 
> Grateful for any ideas
> 
> Thanks
> 
> Matt

You can use curses with Ruby to do what you want. Otherwise if you can  
figure out how to get Ruby to properly handle the CR character that may  
be a simpler way. I am not sure how to get Ruby to print a character  
without interpreting it – i.e. print 0xa prints 10 instead of a  
carriage return.

---

<div class="post-metadata">

**Author:** ![bouldr-matt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/85e7bf/32.png) [@bouldr-matt](https://www.ruby-forum.com/u/bouldr-matt)\
**Post date:** [August 20, 2008, 1:23am UTC](https://www.ruby-forum.com/t/overprinting-characters/146322/3 "2008-08-20T01:23:47Z")

</div>

-------- Original-Nachricht --------

> Datum: Wed, 20 Aug 2008 07:20:58 +0900  
> Von: Matt H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> An: removed\_email\_address@domain.invalid  
> Betreff: overprinting characters

> I think I want to be able to position the cursor back to the beginning  
> of the current line but I have no idea how to do this in ruby.
> 
> Grateful for any ideas
> 
> Thanks
> 
> Matt

Dear Matt,

try this:

["puts" without line feed - Ruby - Ruby-Forum](http://www.ruby-forum.com/topic/162334#new) (I think you want the last  
post’s behaviour).

Best regards,

Axel

---

<div class="post-metadata">

**Author:** ![bouldr-matt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/85e7bf/32.png) [@bouldr-matt](https://www.ruby-forum.com/u/bouldr-matt)\
**Post date:** [August 20, 2008, 1:30am UTC](https://www.ruby-forum.com/t/overprinting-characters/146322/4 "2008-08-20T01:30:49Z")

</div>

On Aug 19, 2008, at 6:20 PM, Matt H. wrote:

> I think I want to be able to position the cursor back to the beginning  
> of the current line but I have no idea how to do this in ruby.
> 
> Grateful for any ideas
> 
> Thanks
> 
> Matt

In addition to print “\r” you might want to look at the docs for  
ProgressBar

[http://raa.ruby-lang.org/project/progressbar/](http://raa.ruby-lang.org/project/progressbar/)

I can’t remember where I used this or I’d drop an example in here, but  
it gives to a bar similar to what sftp or curl shows on downloads (it  
even gives a time to complete estimate).

-Rob

Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![bouldr-matt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/85e7bf/32.png) [@bouldr-matt](https://www.ruby-forum.com/u/bouldr-matt)\
**Post date:** [August 20, 2008, 1:50am UTC](https://www.ruby-forum.com/t/overprinting-characters/146322/5 "2008-08-20T01:50:07Z")

</div>

Rob B. wrote:

> > line, “currentbytes/totalbytes [xx%]” and I want it to update the
> 
> In addition to print “\r” you might want to look at the docs for  
> ProgressBar
> 
> [http://raa.ruby-lang.org/project/progressbar/](http://raa.ruby-lang.org/project/progressbar/)
> 
> I can’t remember where I used this or I’d drop an example in here, but  
> it gives to a bar similar to what sftp or curl shows on downloads (it  
> even gives a time to complete estimate).
> 
> -Rob

Excellent, first I managed to get it working as I said in my post,  
thanks to Axel’s link. Now I am trying the progress bar as Rob  
suggested. It’s good but I can’t work out how the format is used.

I’m trying to expand the amount of text displayed in the title so I can  
see the entire file and path.

Any extra help is appreciated but thanks for what you’ve done so far 😉

Matt

---

<div class="post-metadata">

**Author:** ![bouldr-matt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/85e7bf/32.png) [@bouldr-matt](https://www.ruby-forum.com/u/bouldr-matt)\
**Post date:** [August 20, 2008, 6:03am UTC](https://www.ruby-forum.com/t/overprinting-characters/146322/6 "2008-08-20T06:03:21Z")

</div>

On Aug 19, 2008, at 7:45 PM, Matt H. wrote:

> > -Rob  
> > far 😉
> 
> Matt

OK, here’s the context: getting CSV files from a url and saving them  
locally

begin  
File.open(csvfile, ‘w’) do |csvout|  
pbar = nil  
pbar\_options = $stdout.tty? ? {  
:content\_length\_proc =\> lambda {|t|  
if t && 0 \< t  
pbar = ProgressBar.new(csvfile, t)  
pbar.file\_transfer\_mode  
end  
},  
:progress\_proc =\> lambda {|s|  
pbar.set s if pbar  
},  
} : { }  
open(url, pbar\_options) do |csvurl|  
csvout.write csvurl.read  
end  
end # unless File.exist?(csvfile)  
rescue =\> e  
puts “\n\*\* #{e}; #{e.message}”  
puts " skipping file"  
next  
end

(I’m not boring you with all the setup to figure out the url and the  
subsequent processing of the CSV data into a database.)

-Rob

Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid
