Creating a pacifier in command line program

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.

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:
http://heather.cs.ucdavis.edu/~matloff/UnixAndC/CLanguage/Curses.pdf

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

Hope this helps!

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.

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.

Thank you!

That did the trick perfectly!

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.

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
.
In particular:
.

  1. Similar to Tim H.'s suggestion:
    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
    … 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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs