Class: Gloo::App::Platform

Inherits:
Object
  • Object
show all
Defined in:
lib/gloo/app/platform.rb

Constant Summary collapse

DEFAULT_TMP_FILE =
'tmp.txt'.freeze
RETURN =
"\n".freeze
DEFAULT_LINES =
24
DEFAULT_COLS =
80
PAGER_CMD =
'less -R -F -X'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializePlatform

Set up Platform.



25
26
27
28
# File 'lib/gloo/app/platform.rb', line 25

def initialize
  @prompt = Gloo::App::Prompt.new( self )
  @table = Gloo::App::Table.new( self )
end

Instance Attribute Details

#promptObject (readonly)

Returns the value of attribute prompt.



20
21
22
# File 'lib/gloo/app/platform.rb', line 20

def prompt
  @prompt
end

#tableObject (readonly)

Returns the value of attribute table.



20
21
22
# File 'lib/gloo/app/platform.rb', line 20

def table
  @table
end

Instance Method Details

#clear_screenObject

Clear the screen.



56
57
58
# File 'lib/gloo/app/platform.rb', line 56

def clear_screen
  puts "\e[H\e[2J"
end

#colsObject

Get the number of horizontal columns on screen. Falls back to a default when stdout has no real screen behind it (piped/captured output, no controlling TTY, etc).



101
102
103
104
105
106
# File 'lib/gloo/app/platform.rb', line 101

def cols
  _rows, columns = $stdout.winsize
  return columns
rescue StandardError
  return DEFAULT_COLS
end

#get_colorized_string(str, color) ⇒ Object

Get colorized string.



75
76
77
78
# File 'lib/gloo/app/platform.rb', line 75

def get_colorized_string( str, color )
  colorized = ColorizedString[ str.to_s ].colorize( color )
  return colorized.to_s
end

#get_file_mech(engine) ⇒ Object

Get the file mechanism for this platform.



63
64
65
# File 'lib/gloo/app/platform.rb', line 63

def get_file_mech( engine )
  return Gloo::Persist::DiscMech.new( engine )
end

#linesObject


Sceen helpers

Get the number of vertical lines on screen. Falls back to a default when stdout has no real screen behind it (piped/captured output, no controlling TTY, etc).



89
90
91
92
93
94
# File 'lib/gloo/app/platform.rb', line 89

def lines
  rows, _columns = $stdout.winsize
  return rows
rescue StandardError
  return DEFAULT_LINES
end

#page(msg) ⇒ Object

Show a message in a pager (less), for long content. Falls back to a plain puts when there is no real terminal to page in (piped output, captured test output, etc) or when less isn't available on the system.



43
44
45
46
47
48
49
50
51
# File 'lib/gloo/app/platform.rb', line 43

def page( msg )
  return show( msg ) unless $stdout.tty?

  IO.popen( PAGER_CMD, 'w' ) { |less| less.puts msg }
rescue Errno::ENOENT
  show( msg )
rescue Errno::EPIPE
  # The user quit the pager early. Nothing more to do.
end

#show(msg) ⇒ Object

Show a message.



33
34
35
# File 'lib/gloo/app/platform.rb', line 33

def show( msg )
  puts msg
end