Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Take over console output using ruby

When I run vim or top from a console they are able to take over rendering the whole console. When I quit I'm then returned to the console.

Is it possible to do this from ruby? As a simple example, how would I do the following

# Rakefile
task :clock do
  loop do
    console.render Time.now
    sleep 1
  end
end

when I run this the console would be cleared and the first line would show the time. When I quit I'd then continue the console session as it was before I ran rake clock.


Update

Having checked the tictactoe example for ruby curses here's an implementation of the clock example. I've shown the clock on random lines to demonstrate refreshing the whole console.

#!/usr/bin/env ruby
require 'curses'

loop do
  Curses.clear  
  Curses.setpos(rand * 10, 0)  
  Curses.addstr(Time.now.to_s);
  Curses.refresh
  sleep 1
end
like image 247
opsb Avatar asked Aug 10 '11 13:08

opsb


1 Answers

You're looking for the Ruby curses library which gives you full control over the screen: positioning, color, &c.

It's not a well document library, but a Stackoverflow search for "[ruby] curses" will give you links to examples.

like image 134
Wayne Conrad Avatar answered Sep 19 '22 18:09

Wayne Conrad