Clio::Terminal

Termninal

ConsoleUtils provides methods that are generally useful in the context of creating console output.

Public Instance Methods

ask(question, answers=nil) click to toggle source

Convenient method to get simple console reply.

# File lib/clio/consoleutils.rb, line 15
def ask(question, answers=nil)
  print "#{question}"
  print " [#{answers}] " if answers
  until inp = $stdin.gets ; sleep 1 ; end
  inp
end
password(msg=nil) click to toggle source

Ask for a password. (FIXME: only for unix so far)

# File lib/clio/consoleutils.rb, line 32
def password(msg=nil)
  msg ||= "Enter Password: "
  inp = ''

  $stdout << msg

  begin
    system "stty -echo"
    inp = gets.chomp
  ensure
    system "stty echo"
  end

  return inp
end
screen_width(out=STDERR) click to toggle source

Console screen width (taken from progress bar)

TODO: Don't know how portable screen_width is.

# File lib/clio/consoleutils.rb, line 52
def screen_width(out=STDERR)
  default_width = ENV['COLUMNS'] || 80
  begin
    tiocgwinsz = 0x5413
    data = [0, 0, 0, 0].pack("SSSS")
    if out.ioctl(tiocgwinsz, data) >= 0 then
      rows, cols, xpixels, ypixels = data.unpack("SSSS")
      if cols >= 0 then cols else default_width end
    else
      default_width
    end
  rescue Exception
    default_width
  end
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.