require 'facets/consoleutils' module Ratch # = Ratch IO # # The IO class is used to cleanly separate out the # basic input/output "dialog" between user and script. # class IO # attr :runmode # def initialize(runmode) @runmode = runmode end def force? ; runmode.force? ; end def quiet? ; runmode.quiet? ; end def trace? ; runmode.trace? ; end def debug? ; runmode.debug? ; end def dryrun? ; runmode.dryrun? ; end def noharm? ; runmode.noharm? ; end # Internal status report. # # Only output if dryrun or trace mode. # def status(message) if runmode.dryrun? or runmode.trace? puts message end end # Convenient method to get simple console reply. # def ask(question, answers=nil) print "#{question}" print " [#{answers}] " if answers until inp = $stdin.gets ; sleep 1 ; end inp.strip end # Ask for a password. (FIXME: only for unix so far) # def password(prompt=nil) msg ||= "Enter Password: " inp = '' print "#{prompt} " begin #system "stty -echo" #inp = gets.chomp until inp = $stdin.gets sleep 1 end ensure #system "stty echo" end return inp.chomp end def print(str) super(str) unless quiet? end def puts(str) super(str) unless quiet? end # # def printline(left, right='', options={}) return if runmode.quiet? separator = options[:seperator] || options[:sep] || ' ' padding = options[:padding] || options[:pad] || 0 left, right = left.to_s, right.to_s left_size = left.size right_size = right.size left = colorize(left) right = colorize(right) l = padding r = -(right_size + padding + 1) line = separator * screen_width line[l, left_size] = left if left_size != 0 line[r, right_size] = right if right_size != 0 puts line end # def colorize(text) return text unless text.color if PLATFORM =~ /win/ text.to_s else ANSICode.send(text.color){ text.to_s } end end # def screen_width ConsoleUtils.screen_width end end end