# Creating a pacifier in command line program

**URL:** <https://www.ruby-forum.com/t/creating-a-pacifier-in-command-line-program/176837>\
**Category:** Ruby\
**Created:** [October 6, 2009, 10:21pm UTC](https://www.ruby-forum.com/t/creating-a-pacifier-in-command-line-program/176837 "2009-10-06T22:21:03Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![jhero](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/c57346/32.png) [@jhero](https://www.ruby-forum.com/u/jhero)\
**Post date:** [October 6, 2009, 10:21pm UTC](https://www.ruby-forum.com/t/creating-a-pacifier-in-command-line-program/176837/1 "2009-10-06T22:21:03Z")

</div>

I am trying to create a program that, at one part, replaces already  
displayed on the line with another.

For example:

Lets say the program is recursing directories.

It should display:

checking c:\Program Files\

then when it is done with that directory replace that line with the  
next, not create another below it.

I have been unable to locate information about this, though I don’t  
really know what to call it.

Thank you.

---

<div class="post-metadata">

**Author:** ![jhero](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/c57346/32.png) [@jhero](https://www.ruby-forum.com/u/jhero)\
**Post date:** [October 6, 2009, 11:36pm UTC](https://www.ruby-forum.com/t/creating-a-pacifier-in-command-line-program/176837/2 "2009-10-06T23:36:29Z")

</div>

Ruby comes pre-packaged with a curses library that (supposedly) works  
with both Linux and Windows. It is completely undocumented (that I  
can find), but the C Curses library may help. Have a look at:

> **[Curses.pdf](https://heather.cs.ucdavis.edu/~matloff/UnixAndC/CLanguage/Curses.pdf)**
>
> 95.28 KB

The ruby curses lack-of-documentation:  
[http://ruby-doc.org/stdlib/libdoc/curses/rdoc/index.html](http://ruby-doc.org/stdlib/libdoc/curses/rdoc/index.html)

Hope this helps!

---

<div class="post-metadata">

**Author:** ![jhero](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/c57346/32.png) [@jhero](https://www.ruby-forum.com/u/jhero)\
**Post date:** [October 6, 2009, 11:56pm UTC](https://www.ruby-forum.com/t/creating-a-pacifier-in-command-line-program/176837/3 "2009-10-06T23:56:59Z")

</div>

Jerry Mr wrote:

> then when it is done with that directory replace that line with the  
> next, not create another below it.
> 
> I have been unable to locate information about this, though I don’t  
> really know what to call it.
> 
> Thank you.

I think this will work in Windows but I don’t have it to test. It works  
in OS X and Linux terminal windows.

Instead of terminating the output line with a \n (or allowing it to  
default to a \n), use \r instead. “\r” is a carriage return, so the  
cursor will return to the start of the same line and overprint it.

For example,

10.times { |n| printf(“Hello %d\r”, n);sleep 1 }

Will print

Hello 0  
Hello 1  
Hello 2

etc., except each new “Hello” line overprints the previous one so you  
only see 1 line.

---

<div class="post-metadata">

**Author:** ![jhero](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/c57346/32.png) [@jhero](https://www.ruby-forum.com/u/jhero)\
**Post date:** [October 7, 2009, 3:18am UTC](https://www.ruby-forum.com/t/creating-a-pacifier-in-command-line-program/176837/4 "2009-10-07T03:18:40Z")

</div>

Well, now I am trying to impliment this into a program.

Example:

def spinner spin\_trigger  
while (spin\_trigger == true)  
print “\\r”  
sleep 1  
print “|\r”  
sleep 1  
print “/\r”  
sleep 1  
print “-\r”  
sleep 1  
end  
end

spin = true  
Thread.new do  
spinner spin  
end  
files = Dir.glob("c/\*\*/\*.txt)  
spin = false

All I am getting is “” until the Dir command is complete.  
The method works perfectly when not running inside another program.

It must have something to do with the new thread I am creating, but I  
cannot figure out what…hrm.

---

<div class="post-metadata">

**Author:** ![jhero](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/c57346/32.png) [@jhero](https://www.ruby-forum.com/u/jhero)\
**Post date:** [October 7, 2009, 12:45am UTC](https://www.ruby-forum.com/t/creating-a-pacifier-in-command-line-program/176837/5 "2009-10-07T00:45:02Z")

</div>

Thank you!

That did the trick perfectly!

---

<div class="post-metadata">

**Author:** ![jhero](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/c57346/32.png) [@jhero](https://www.ruby-forum.com/u/jhero)\
**Post date:** [October 7, 2009, 3:22am UTC](https://www.ruby-forum.com/t/creating-a-pacifier-in-command-line-program/176837/6 "2009-10-07T03:22:19Z")

</div>

> files = Dir.glob(“c/**/\*.txt)  
> –\> files = Dir.glob("c/**/\*.txt”)  
> Yes, I realize there is a typo after the .txt.  
> That typo is not in the program.

---

<div class="post-metadata">

**Author:** ![jhero](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/c57346/32.png) [@jhero](https://www.ruby-forum.com/u/jhero)\
**Post date:** [October 7, 2009, 8:06am UTC](https://www.ruby-forum.com/t/creating-a-pacifier-in-command-line-program/176837/7 "2009-10-07T08:06:57Z")

</div>

Further to the other responses, there is a thread here which has similar  
issues.  
[http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/336718](http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/336718)  
.  
In particular:  
.

1. Similar to Tim H.'s suggestion:  
[http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/336734](http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/336734)  
… to show progress in a “console” box I just repeatedly use (every  
second):  
print 13.chr + “text to show progress”  
This - and presumably Tim H.'s code - runs under Linux and Windows.  
Actually I think I prefer Tim H.'s formulation. (It’s OK for me to  
say  
that because the post just above was from me!)  
.
2. If you want “multi line” changes this post uses curses and -  
apparently -  
works on both Linux and Windows.  
[http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/336742](http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/336742)  
… This is based on the “hello.rb” example in the following place on my  
Win32 Ruby installation:  
C:/ruby/src/ruby-1.8.6-p111/ext/curses

require “curses”  
include Curses

init\_screen  
puts ‘##### screen size(?): lines, cols:’, lines, cols  
puts ‘about to display text’  
sleep 2

begin  
crmode  
setpos( 8, 13 )  
addstr(“display text which we will partly over-write later”)  
refresh  
sleep 2  
setpos( 3, 40 ) ; addstr(“display some text”) ; refresh  
setpos( 8, 40 ) ; addstr(“PARTLY”) ; refresh  
setpos( 8, 32 ) ; addstr(“W”) ; refresh  
20000.times do | k |  
setpos( 8, 13 ) ; addstr( k.to\_s + " ") ; refresh  
end  
ensure  
close\_screen  
end
