Class: Fuelcell::Shell

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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

#backtraceObject (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_streamObject (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_streamObject (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_streamObject (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_backtraceObject



26
27
28
# File 'lib/fuelcell/shell.rb', line 26

def disable_backtrace
  @backtrace = false
end

#dynamic_widthObject

Determine the width using stty or tput

credit goes to the rake project, I took this from the application.rb

Returns:

  • Integer



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_sttyObject

credit goes to the rake project, I took this from the application.rb

Returns:

  • Integer



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_tputObject

credit goes to the rake project, I took this from the application.rb

Returns:

  • Integer



86
87
88
# File 'lib/fuelcell/shell.rb', line 86

def dynamic_width_tput
  `tput cols 2>/dev/null`.to_i
end

#enable_backtraceObject



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)
  message = e.message
  message << "\n" << e.backtrace.join("\n") if backtrace?
  error(message)
  failure_exit
end

#exit(code) ⇒ Object



98
99
100
# File 'lib/fuelcell/shell.rb', line 98

def exit(code)
  Kernel.exit(code)
end

#failure_exitObject



94
95
96
# File 'lib/fuelcell/shell.rb', line 94

def failure_exit
  exit(ERROR_EXIT_CODE)
end

#successful_exitObject



90
91
92
# File 'lib/fuelcell/shell.rb', line 90

def successful_exit
  exit(SUCCESS_EXIT_CODE)
end

#terminal_widthObject



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

Returns:

  • (Boolean)

    Boolean



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