Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>A while ago I created a class to be a status text on which you can change part of the content of the text within the line. It might be useful to you.</p> <p>The class with an example use are:</p> <pre><code>class StatusText def initialize(parms={}) @previous_size = 0 @stream = parms[:stream]==nil ? $stdout : parms[:stream] @parms = parms @parms[:verbose] = true if parms[:verbose] == nil @header = [] @onChange = nil pushHeader(@parms[:base]) if @parms[:base] end def setText(complement) text = "#{@header.join(" ")}#{@parms[:before]}#{complement}#{@parms[:after]}" printText(text) end def cleanAll printText("") end def cleanContent printText "#{@parms[:base]}" end def nextLine(text=nil) if @parms[:verbose] @previous_size = 0 @stream.print "\n" end if text!=nil line(text) end end def line(text) printText(text) nextLine end #Callback in the case the status text changes #might be useful to log the status changes #The callback function receives the new text def onChange(&amp;block) @on_change = block end def pushHeader(head) @header.push(head) end def popHeader @header.pop end def setParm(parm, value) @parms[parm] = value if parm == :base @header.last = value end end private def printText(text) #If not verbose leave without printing if @parms[:verbose] if @previous_size &gt; 0 #go back @stream.print "\033[#{@previous_size}D" #clean @stream.print(" " * @previous_size) #go back again @stream.print "\033[#{@previous_size}D" end #print @stream.print text @stream.flush #store size @previous_size = text.gsub(/\e\[\d+m/,"").size end #Call callback if existent @on_change.call(text) if @on_change end end a = StatusText.new(:before =&gt; "Evolution (", :after =&gt; ")") (1..100).each {|i| a.setText(i.to_s); sleep(1)} a.nextLine </code></pre> <p>Just copy, paste in a ruby file and try it out. I use escape sequences to reposition the cursor.</p> <p>The class has lots of features I needed at the time (like piling up elements in the status bar) that you can use to complement your solution, or you can just clean it up to its core.</p> <p>I hope it helps.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload