How do I get my command line utility to update one line instead of printing multiple lines?


franksort

I'm writing a Ruby command line utility and I'd like it to update one line in the console with new data instead of printing multiple lines of output. The effect I'm going for it similar to a console progress bar you might see when using wget.

For instance:

app.rb

#!/usr/bin/ruby env

data = %w[this is some data]
data.each {|s| puts s}

When I run this app it will output the following which spans 4 lines:

$ ruby app.rb
this
is
some
data
$

I would like the area between [ ] in the following example to show the output, one word at a time.

$ ruby app.rb
[ this ]
$

Any ideas?

falsettu

Use \r (carriage return) to move the cursor at the start of the line:

data = %w[this is some data]
data.each { |s|
  sleep(0.5)
  print "\r%-20s" % s  # `print` instead of `put` to avoid newline.
  # Additional spaces to overwrite remaining characters of previous output
}
puts

See Demo.

Related


How to update in one line instead of multiple lines?

Keith M. Simple, I just want a code (instead of printing out a separate line of text in the console) to update a line of text with the new value, or "replace" it. Current code: //this code was expanded into a more configurable version //original author is zeng

I only get one weird line instead of 2 normal lines

Top Kenzo The problem is that I get a weird announcement instead of 2 normal announcements (like my database). How it looks now (screenshot): https://prnt.sc/q5q8ju Code: <?PHP $query = "SELECT * FROM `thbs_announcements` ORDER BY `anno_date` DESC";

How do I get C:/ on the command line

username I am on Windows 10. I'm trying to load Adobe Dreamweaver extension via command line on Windows. I want to navigate to C:\Program Files\Adobe\Adobe Extension Manager CSx\ I am following the instructions here . If I select command prompt (admin), I getC

How do I get C:/ on the command line

username I am on Windows 10. I'm trying to load Adobe Dreamweaver extension via command line on Windows. I want to navigate to C:\Program Files\Adobe\Adobe Extension Manager CSx\ I am following the instructions here . If I select command prompt (admin), I getC

How do I make one line of coffee script code into multiple lines?

Roger I tried to use coffee scriptrecently , but I have some code like this if userAgent.match(/iPad/i) or userAgent.match(/iPhone/i) or userAgent.match(/iPoid/i) or userAgent.match(/Android/i) or userAgent.match(/IEMobile/i) or userAgent.match(/BlackBerry/i)

How do I make one line of coffee script code into multiple lines?

Roger I tried to use coffee scriptrecently , but I have some code like this if userAgent.match(/iPad/i) or userAgent.match(/iPhone/i) or userAgent.match(/iPoid/i) or userAgent.match(/Android/i) or userAgent.match(/IEMobile/i) or userAgent.match(/BlackBerry/i)

Update previous line instead of printing new line

Kumar Here is a simple program in c. It takes two integers and adds them together. In this code, I want to update the previous line by wrapping it instead of wrapping a new line, any body can help me on this topic. #include <stdio.h> #include <stdlib.h> int m

Get one line from multiple lines

Mamata I have data like tableid name status uuid date 1 a none 1 2019-12-02 1 a none 2 2019-12-02 1 a done 4 2019-12-02 2 b none 6 2019-12-02 2 b done 7 2019-12-02 3

Get one line from multiple lines

Mamata I have data like tableid name status uuid date 1 a none 1 2019-12-02 1 a none 2 2019-12-02 1 a done 4 2019-12-02 2 b none 6 2019-12-02 2 b done 7 2019-12-02 3

Get one line from multiple lines

Mamata I have data like tableid name status uuid date 1 a none 1 2019-12-02 1 a none 2 2019-12-02 1 a done 4 2019-12-02 2 b none 6 2019-12-02 2 b done 7 2019-12-02 3

Get one line from multiple lines

Mamata I have data like tableid name status uuid date 1 a none 1 2019-12-02 1 a none 2 2019-12-02 1 a done 4 2019-12-02 2 b none 6 2019-12-02 2 b done 7 2019-12-02 3

Get one line from multiple lines

Mamata I have data like tableid name status uuid date 1 a none 1 2019-12-02 1 a none 2 2019-12-02 1 a done 4 2019-12-02 2 b none 6 2019-12-02 2 b done 7 2019-12-02 3

Where can I get the SQLpackage command line utility?

nail polish I'm trying to use the SQL Package command line utility (sqlpackage.exe) on SQL Server, but can't find it anywhere online. Can I download it as a standalone utility, or is it part of some larger package that I have to install? ᴇcʜιᴇ007 A standalone

Where can I get the SQLpackage command line utility?

nail polish I'm trying to use the SQL Package command line utility (sqlpackage.exe) on SQL Server, but can't find it anywhere online. Can I download it as a standalone utility, or is it part of some larger package that I have to install? ᴇcʜιᴇ007 A standalone

In VIM, how do I split a very long line into multiple lines?

Jordan Palmer Suppose I have an extra long line (say about 300+ characters) in the VIM editor. How can I break this up into multiple lines so that the word boundaries are roughly 80 characters long? example: This is a really long line This is a really long lin

In VIM, how do I split a very long line into multiple lines?

Jordan Palmer Suppose I have an extra long line (say about 300+ characters) in the VIM editor. How can I break this up into multiple lines so that the word boundaries are roughly 80 characters long? example: This is a really long line This is a really long lin

In VIM, how do I split a very long line into multiple lines?

Jordan Palmer Suppose I have an extra long line (say about 300+ characters) in the VIM editor. How can I break this up into multiple lines so that the word boundaries are roughly 80 characters long? example: This is a really long line This is a really long lin