Class: Machinery::Ui

Inherits:
Object show all
Defined in:
lib/ui.rb,
lib/hint.rb,
lib/renderer.rb,
lib/diff_widget.rb

Defined Under Namespace

Classes: DiffWidget, Hint, Renderer

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



43
44
45
# File 'lib/ui.rb', line 43

def close_pager
  @pager.close if @pager
end

.error(s) ⇒ Object



101
102
103
# File 'lib/ui.rb', line 101

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

.kill_pagerObject



47
48
49
# File 'lib/ui.rb', line 47

def kill_pager
  Process.kill("TERM", @pager_pid) if @pager_pid
end


51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/ui.rb', line 51

def print(output)
  reset_line if progress_visible?

  if !use_pager || !$stdout.tty?
    begin
      STDOUT.print 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.print 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

.progress(output) ⇒ Object



84
85
86
87
88
89
90
91
# File 'lib/ui.rb', line 84

def progress(output)
  return unless progress_enabled?

  reset_line if progress_visible?
  print output

  @progress_visible = true
end

.puts(output) ⇒ Object



93
94
95
# File 'lib/ui.rb', line 93

def puts(output)
  print output + "\n"
end

.warn(s) ⇒ Object



97
98
99
# File 'lib/ui.rb', line 97

def warn(s)
  STDERR.puts s
end

.write_output_to_pager(output) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/ui.rb', line 28

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

  # cache the pid because it can no longer be retrieved when the
  # stream is closed (and we sometimes have to kill the process after
  # it was closed)
  @pager_pid = @pager.pid

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