Class: Machinery::Ui

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

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.use_pagerObject

Returns the value of attribute use_pager.



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

def use_pager
  @use_pager
end

Class Method Details

.close_pagerObject



38
39
40
# File 'lib/ui.rb', line 38

def close_pager
  @pager.close if @pager
end

.error(s) ⇒ Object



77
78
79
# File 'lib/ui.rb', line 77

def error(s)
  STDERR.puts s
end

.internal_scope_list_to_string(scopes) ⇒ Object



23
24
25
26
# File 'lib/ui.rb', line 23

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

.puts(output) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/ui.rb', line 42

def puts(output)
  if !use_pager || !$stdout.tty?
    begin
      STDOUT.puts output
    rescue Errno::EPIPE
      # We just ignore broken pipes.
    end
  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
        STDOUT.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

.warn(s) ⇒ Object



73
74
75
# File 'lib/ui.rb', line 73

def warn(s)
  STDERR.puts s
end

.write_output_to_pager(output) ⇒ Object



28
29
30
31
32
33
34
35
36
# File 'lib/ui.rb', line 28

def write_output_to_pager(output)
  @pager ||= IO.popen("$PAGER", "w")

  begin
    @pager.puts output
  rescue Errno::EPIPE
    # We just ignore broken pipes.
  end
end