Class: Machinery::Ui

Inherits:
Object
  • Object
show all
Defined in:
lib/ui.rb

Class Method Summary collapse

Class Method Details

.error(s) ⇒ Object



70
71
72
# File 'lib/ui.rb', line 70

def self.error(s)
  STDERR.puts s
end

.internal_scope_list_to_string(scopes) ⇒ Object



20
21
22
23
# File 'lib/ui.rb', line 20

def self.internal_scope_list_to_string(scopes)
  list = Array(scopes)
  list.map{ |e| e.tr("_", "-") }.join(",")
end


35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/ui.rb', line 35

def self.print_output(output, options = {})
  if options[:no_pager] || !$stdout.tty?
    Machinery::Ui.puts output
  else
    if !ENV['PAGER'] || ENV['PAGER'] == ''
      ENV['PAGER'] = 'less'
      ENV['LESS'] = 'FSRX'
      begin
        LocalSystem.validate_existence_of_package("less")
        write_output_to_pager(output)
      rescue Machinery::Errors::MissingRequirement
        Machinery::Ui.puts output
      end
    else
      IO.popen("$PAGER &>/dev/null", "w") { |f| f.close }
      if $?.success?
        write_output_to_pager(output)
      else
        raise(Machinery::Errors::InvalidPager.new("'#{ENV['PAGER']}' could not " \
          "be executed. Use the --no-pager option or modify your $PAGER " \
          "bash environment variable to display output.")
        )
      end
    end
  end
end

.puts(s) ⇒ Object



62
63
64
# File 'lib/ui.rb', line 62

def self.puts(s)
  STDOUT.puts s
end

.warn(s) ⇒ Object



66
67
68
# File 'lib/ui.rb', line 66

def self.warn(s)
  STDERR.puts s
end

.write_output_to_pager(output) ⇒ Object



25
26
27
28
29
30
31
32
33
# File 'lib/ui.rb', line 25

def self.write_output_to_pager(output)
  IO.popen("$PAGER", "w") do |f|
    begin
      f.puts output
    rescue Errno::EPIPE
      # We just ignore broken pipes.
    end
  end
end