Module: Terminal

Defined in:
lib/rubyhacks.rb

Defined Under Namespace

Classes: TerminalError

Constant Summary collapse

RESET =
"\033[0;00m"
CLEAR_LINE =
"\033[K"
CYAN =
"\033[2;36m"
LIGHT_GREEN =
"\033[1;32m"
WHITE =
"\033[1;37m"
RED =
"\033[1;31m"
BACKGROUND_BLACK =
"\033[40m"

Class Method Summary collapse

Class Method Details

.default_colourObject



588
589
590
# File 'lib/rubyhacks.rb', line 588

def self.default_colour
	"\033[00m"
end

.erewind(nlines) ⇒ Object



579
580
581
# File 'lib/rubyhacks.rb', line 579

def self.erewind(nlines)
	eprint "\033[#{nlines}A"
end

.resetObject



584
585
586
# File 'lib/rubyhacks.rb', line 584

def self.reset
	print "\033[0;00m"
end

.rewind(nlines) ⇒ Object



575
576
577
# File 'lib/rubyhacks.rb', line 575

def self.rewind(nlines)
	print "\033[#{nlines}A"
end

.terminal_sizeObject

Raises:



556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
# File 'lib/rubyhacks.rb', line 556

def self.terminal_size
	return [ENV['ROWS'].to_i, ENV['COLS'].to_i] if ENV['ROWS'] and ENV['COLS']
	raise TerminalError.new("Tried to determine terminal size but this process is not being run from within a terminal (may be a sub process or called within another script). Set ENV['ROWS'] ($ROWS in shell) and ENV['COLS'] ($COLS in shell) to avoid this warning") unless STDOUT.tty?
	system_code = TIOCGWINSZ
	begin
		rows, cols = 25, 80
		buf = [0, 0, 0, 0].pack("SSSS")
		if STDOUT.ioctl(system_code, buf) >= 0 then
			rows, cols, row_pixels, row_pixels, col_pixels =  
		buf.unpack("SSSS")[0..1]
		end
		return [rows, cols]
	rescue
		#puts "Warning: TIOCGWINSZ switched in Terminal.terminal_size"
		system_code = OTHER_TIOCGWINSZ 	
	retry
	end
end