Class: Fuelcell::Shell
- Inherits:
-
Object
- Object
- Fuelcell::Shell
- Extended by:
- Forwardable
- Defined in:
- lib/fuelcell/shell.rb
Constant Summary collapse
- SUCCESS_EXIT_CODE =
0- ERROR_EXIT_CODE =
255- DEFAULT_TERMINAL_WIDTH =
80
Instance Attribute Summary collapse
-
#backtrace ⇒ Object
(also: #backtrace?)
readonly
Returns the value of attribute backtrace.
-
#error_stream ⇒ Object
readonly
Returns the value of attribute error_stream.
-
#input_stream ⇒ Object
readonly
Returns the value of attribute input_stream.
-
#output_stream ⇒ Object
readonly
Returns the value of attribute output_stream.
Instance Method Summary collapse
- #disable_backtrace ⇒ Object
-
#dynamic_width ⇒ Object
Determine the width using stty or tput.
-
#dynamic_width_stty ⇒ Object
credit goes to the rake project, I took this from the application.rb.
-
#dynamic_width_tput ⇒ Object
credit goes to the rake project, I took this from the application.rb.
- #enable_backtrace ⇒ Object
- #error(text) ⇒ Object
- #exception(e) ⇒ Object
- #exit(code) ⇒ Object
- #failure_exit ⇒ Object
-
#initialize(config = {}) ⇒ Shell
constructor
A new instance of Shell.
- #successful_exit ⇒ Object
- #terminal_width ⇒ Object
-
#unix? ⇒ Boolean
Determines if we are on a unix like system for determining the width of the terminal.
Constructor Details
#initialize(config = {}) ⇒ Shell
Returns a new instance of Shell.
13 14 15 16 17 18 19 20 |
# File 'lib/fuelcell/shell.rb', line 13 def initialize(config = {}) @output_stream = config[:output_stream] || $stdout @input_stream = config[:input_stream] || $stdin @error_stream = config[:error_stream] || $stderr @backtrace = config[:backtrace] || false @terminal_cols = config[:terminal_cols] @backtrace = @backtrace == true ? true : false end |
Instance Attribute Details
#backtrace ⇒ Object (readonly) Also known as: backtrace?
Returns the value of attribute backtrace.
9 10 11 |
# File 'lib/fuelcell/shell.rb', line 9 def backtrace @backtrace end |
#error_stream ⇒ Object (readonly)
Returns the value of attribute error_stream.
9 10 11 |
# File 'lib/fuelcell/shell.rb', line 9 def error_stream @error_stream end |
#input_stream ⇒ Object (readonly)
Returns the value of attribute input_stream.
9 10 11 |
# File 'lib/fuelcell/shell.rb', line 9 def input_stream @input_stream end |
#output_stream ⇒ Object (readonly)
Returns the value of attribute output_stream.
9 10 11 |
# File 'lib/fuelcell/shell.rb', line 9 def output_stream @output_stream end |
Instance Method Details
#disable_backtrace ⇒ Object
26 27 28 |
# File 'lib/fuelcell/shell.rb', line 26 def disable_backtrace @backtrace = false end |
#dynamic_width ⇒ Object
Determine the width using stty or tput
credit goes to the rake project, I took this from the application.rb
59 60 61 |
# File 'lib/fuelcell/shell.rb', line 59 def dynamic_width @dynamic_width ||= (dynamic_width_stty.nonzero? || dynamic_width_tput) end |
#dynamic_width_stty ⇒ Object
credit goes to the rake project, I took this from the application.rb
79 80 81 |
# File 'lib/fuelcell/shell.rb', line 79 def dynamic_width_stty `stty size 2>/dev/null`.split[1].to_i end |
#dynamic_width_tput ⇒ Object
credit goes to the rake project, I took this from the application.rb
86 87 88 |
# File 'lib/fuelcell/shell.rb', line 86 def dynamic_width_tput `tput cols 2>/dev/null`.to_i end |
#enable_backtrace ⇒ Object
22 23 24 |
# File 'lib/fuelcell/shell.rb', line 22 def enable_backtrace @backtrace = true end |
#error(text) ⇒ Object
30 31 32 |
# File 'lib/fuelcell/shell.rb', line 30 def error(text) error_stream.puts text end |
#exception(e) ⇒ Object
34 35 36 37 38 39 |
# File 'lib/fuelcell/shell.rb', line 34 def exception(e) = e. << "\n" << e.backtrace.join("\n") if backtrace? error() failure_exit end |
#exit(code) ⇒ Object
98 99 100 |
# File 'lib/fuelcell/shell.rb', line 98 def exit(code) Kernel.exit(code) end |
#failure_exit ⇒ Object
94 95 96 |
# File 'lib/fuelcell/shell.rb', line 94 def failure_exit exit(ERROR_EXIT_CODE) end |
#successful_exit ⇒ Object
90 91 92 |
# File 'lib/fuelcell/shell.rb', line 90 def successful_exit exit(SUCCESS_EXIT_CODE) end |
#terminal_width ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/fuelcell/shell.rb', line 41 def terminal_width if ENV['FUELCELL_COLUMNS'] result = ENV['FUELCELL_COLUMNS'].to_i elsif @terminal_cols result = @terminal_cols.to_i else result = unix? ? dynamic_width : DEFAULT_TERMINAL_WIDTH end result < 10 ? DEFAULT_TERMINAL_WIDTH : result rescue DEFAULT_TERMINAL_WIDTH end |
#unix? ⇒ Boolean
Determines if we are on a unix like system for determining the width of the terminal
credit goes to the rake project, I took this from the application.rb
69 70 71 72 73 74 |
# File 'lib/fuelcell/shell.rb', line 69 def unix? !( RUBY_PLATFORM =~ /(aix|darwin|linux|(net|free|open)bsd|cygwin|solaris|irix|hpux)/i ).nil? end |