lib/tty/terminal.rb in tty-0.0.11 vs lib/tty/terminal.rb in tty-0.1.0
- old
+ new
@@ -1,10 +1,9 @@
-# -*- encoding: utf-8 -*-
+# encoding: utf-8
module TTY
class Terminal
-
# Return default width of terminal
#
# @example
# default_width = TTY::Terminal.default_width
#
@@ -21,79 +20,66 @@
# @return [Integer]
#
# @api public
attr_reader :default_height
+ # Return access to color terminal
+ #
+ # @return [TTY::Terminal::Color]
+ #
# @api public
attr_reader :color
# Output pager
#
- # @return [Pager]
+ # @return [TTY::Terminal::Pager]
#
# @api public
attr_reader :pager
- def initialize
+ # Initialize a Terminal
+ #
+ # @api public
+ def initialize(options = {})
@color = TTY::Terminal::Color.new(self.color?)
@echo = TTY::Terminal::Echo.new
@pager = TTY::Terminal::Pager
- @default_width = 80
- @default_height = 24
+ @home = Home.new
+ @default_width = options.fetch(:default_width) { 80 }
+ @default_height = options.fetch(:default_height) { 24 }
end
- # Set default width of terminal
- #
- # @param [Integer] width
- #
- # @return [Integer]
- #
- # @api public
- def default_width=(width)
- @default_width = width
- end
-
- # Set default height of terminal
- #
- # @example
- # default_height = TTY::Terminal.default_height
- #
- # @return [Integer]
- #
- # @api public
- def default_height=(height)
- @default_height = height
- end
-
# Determine current width
#
# @return [Integer] width
#
# @api width
def width
env_tty_columns = ENV['TTY_COLUMNS']
if env_tty_columns =~ /^\d+$/
- result = env_tty_columns.to_i
+ env_tty_columns.to_i
else
- result = TTY::System.unix? ? dynamic_width : default_width
+ TTY::System.unix? ? dynamic_width : default_width
end
rescue
default_width
end
# Determine current height
#
+ # @return [Integer] height
+ #
# @api public
def height
env_tty_lines = ENV['TTY_LINES']
if env_tty_lines =~ /^\d+$/
- result = env_tty_lines.to_i
+ env_tty_lines.to_i
else
- result = TTY::System.unix? ? dynamic_height : self.default_height
+ TTY::System.unix? ? dynamic_height : default_height
end
rescue
- self.default_height
+ default_height
end
# Calculate dynamic width of the terminal
#
# @return [Integer] width
@@ -116,47 +102,47 @@
#
# @return [Integer] width
#
# @api public
def dynamic_width_stty
- %x{stty size 2>/dev/null}.split[1].to_i
+ %x(tty size 2>/dev/null).split[1].to_i
end
# Detect terminal height with stty utility
#
# @return [Integer] height
#
# @api public
def dynamic_height_stty
- %x{stty size 2>/dev/null}.split[0].to_i
+ %x(tty size 2>/dev/null).split[0].to_i
end
# Detect terminal width with tput utility
#
# @return [Integer] width
#
# @api public
def dynamic_width_tput
- %x{tput cols 2>/dev/null}.to_i
+ %x(tput cols 2>/dev/null).to_i
end
# Detect terminal height with tput utility
#
# @return [Integer] height
#
# @api public
def dynamic_height_tput
- %x{tput lines 2>/dev/null}.to_i
+ %x(tput lines 2>/dev/null).to_i
end
# Check if terminal supports color
#
# @return [Boolean]
#
# @api public
def color?
- %x{tput colors 2>/dev/null}.to_i > 2
+ %x(tput colors 2>/dev/null).to_i > 2
end
# Switch echo on
#
# @api public
@@ -174,21 +160,20 @@
# Echo given block
#
# @param [Boolean] is_on
#
# @api public
- def echo(is_on=true, &block)
+ def echo(is_on = true, &block)
@echo.echo(is_on, &block)
end
# Find user home directory
#
# @return [String]
#
# @api public
def home
- @home ||= Home.new
@home.home
end
# Run text through a dynamically chosen pager
#
@@ -197,8 +182,7 @@
#
# @api public
def page(text)
@pager.page(text)
end
-
end # Terminal
end # TTY