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
# File 'lib/brakeman/report/pager.rb', line 3

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

Instance Method Details

#in_ci?Boolean

Returns:

  • (Boolean)


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

def in_ci?
  ci = ENV["CI"]

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

#less_available?Boolean

Returns:

  • (Boolean)


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

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

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

#less_optionsObject



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

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



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

def no_pager text
  @output.puts text
end

#page_output(text) ⇒ Object



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

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



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

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



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

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



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

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



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

def set_color
  unless @tracker and less_options.include? "-R "
    @tracker.options[:output_color] = false
  end
end