Class: Brakeman::Pager

Inherits:
Object
  • Object
show all
Defined in:
lib/brakeman/report/pager.rb

Instance Method Summary collapse

Constructor Details

#initialize(tracker, pager = :less, output = $stdout) ⇒ Pager

Returns a new instance of Pager.



3
4
5
6
7
8
# File 'lib/brakeman/report/pager.rb', line 3

def initialize tracker, pager = :less, output = $stdout
  @tracker = tracker
  @pager = pager
  @output = output
  @less_available = @less_options = nil
end

Instance Method Details

#in_ci?Boolean

Returns:

  • (Boolean)


71
72
73
74
75
# File 'lib/brakeman/report/pager.rb', line 71

def in_ci?
  ci = ENV["CI"]

  ci.is_a? String and ci.downcase == "true"
end

#less_available?Boolean

Returns:

  • (Boolean)


77
78
79
80
81
# File 'lib/brakeman/report/pager.rb', line 77

def less_available?
  return @less_available unless @less_available.nil?

  @less_available = system("which less > /dev/null")
end

#less_optionsObject



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/brakeman/report/pager.rb', line 83

def less_options
  # -R show colors
  # -F exit if output fits on one screen
  # -X do not clear screen after less exits

  return @less_options if @less_options

  @less_options = []

  if system("which less > /dev/null")
    less_help = `less -?`

    ["-R ", "-F ", "-X "].each do |opt|
      if less_help.include? opt
        @less_options << opt
      end
    end
  end

  @less_options
end

#no_pager(text) ⇒ Object



41
42
43
# File 'lib/brakeman/report/pager.rb', line 41

def no_pager text
  @output.puts text
end

#page_output(text) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/brakeman/report/pager.rb', line 24

def page_output text
  case @pager
  when :none
    no_pager text
  when :highline
    page_via_highline text
  when :less
    if less_available?
      page_via_less text
    else
      page_via_highline text
    end
  else
    no_pager text
  end
end

#page_report(report, format) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/brakeman/report/pager.rb', line 10

def page_report report, format
  if @pager == :less
    set_color
  end

  text = report.format(format)

  if in_ci?
    no_pager text
  else
    page_output text
  end
end

#page_via_highline(text) ⇒ Object



45
46
47
48
49
50
# File 'lib/brakeman/report/pager.rb', line 45

def page_via_highline text
  Brakeman.load_brakeman_dependency 'highline'
  h = ::HighLine.new($stdin, @output)
  h.page_at = :auto
  h.say text
end

#page_via_less(text) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/brakeman/report/pager.rb', line 52

def page_via_less text
  # Adapted from https://github.com/piotrmurach/tty-pager/

  write_io = open("|less #{less_options.join}", 'w')
  pid = write_io.pid

  write_io.write(text)
  write_io.close

  Process.waitpid2(pid, Process::WNOHANG)
rescue Errno::ECHILD
  # on jruby 9x waiting on pid raises (per tty-pager)
  true
rescue => e
  warn "[Error] #{e}"
  warn "[Error] Could not use pager. Set --no-pager to avoid this issue."
  no_pager text
end

#set_colorObject



105
106
107
108
109
110
111
# File 'lib/brakeman/report/pager.rb', line 105

def set_color
  return unless @tracker

  unless less_options.include? "-R " or @tracker.options[:output_color] == :force
    @tracker.options[:output_color] = false
  end
end