Class: Colora::Lines

Inherits:
Object
  • Object
show all
Defined in:
lib/colora/lines.rb,
lib/colora/plugs/diff.rb,
lib/colora/plugs/markdown.rb

Overview

Lines namespace :reek:DuplicateMethodCall

Defined Under Namespace

Modules: Diff

Constant Summary collapse

@@plugins =
[]

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeLines

Returns a new instance of Lines.



35
36
37
38
39
40
41
42
43
44
# File 'lib/colora/lines.rb', line 35

def initialize
  @formatter = formatter
  @lines = get_lines
  @lexer = @orig_lexer = guess_lexer
  @tag   = @lexer.tag.to_sym
  @lines = @tag == :diff ? Data.new(@lines).lines : @lines
  @lang  = @orig_lang = Rouge::Lexer.find_fancy(Config.lang)
  # `@on` is `true` unless there is a `Config.on` condition to be met
  @on = Config.on ? false : true
end

Class Method Details

.pluginsObject



103
# File 'lib/colora/lines.rb', line 103

def self.plugins = @@plugins

Instance Method Details

#by_filters?(ary, ar0 = ary[0], dg1 = ary.dig(1, 0), dg2 = ary.dig(2, 0)) ⇒ Boolean

rubocop:disable Metrics :reek:LongParameterList :reek:UncommunicativeParameterName :reek:UtilityFunction

Returns:

  • (Boolean)


82
83
84
85
86
87
88
89
90
# File 'lib/colora/lines.rb', line 82

def by_filters?(ary, ar0 = ary[0], dg1 = ary.dig(1, 0), dg2 = ary.dig(2, 0))
  (Config.in && '-<'.include?(ar0)) ||
    (Config.out && '+>'.include?(ar0)) ||
    (Config.quiet && ((Config.code && [nil, 't'].include?(dg1)) ||
                      (Config.comment && [nil, 't'].include?(dg2)) ||
                      (Config.dupcode && dg1 == 'd') ||
                      (Config.dupcomment && dg2 == 'd'))) ||
    false
end

#diff(line) ⇒ Object

category: method :reek:TooManyStatements rubocop:disable Metrics, Layout/LineLength



34
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/colora/plugs/diff.rb', line 34

def diff(line)
  case line
  when String
    case line
    when '-', '<'
      format(line, Config.deleted)
    when '+', '>'
      format(line, Config.inserted)
    when %r{^[-+][-+][-+] ([\w./].*)$}
      reset_lang_by_filename($LAST_MATCH_INFO[1].strip.split.first)
      format(line, :bold)
    when /^\s*#!/
      reset_lang_by_source(line)
      format(line) unless Config.quiet
    when /^ /
      format(Diff.pad(line), Config.context) unless Config.quiet
    else
      format(line) unless Config.quiet
    end
  else
    # Initialized text variables
    txt = String.new
    flags = Diff.flags(line)
    code = line.dig(1, 1) || ''
    comment = line.dig(2, 1) || ''
    case line[0]
    when '-', '<'
      txt << format(flags, Config.deleted)
      txt << case line.dig(1, 0)
             when '-'
               format(code, Config.get(:comment, :deleted))
             else
               format(code, Config.get(:comment, :replaced))
             end
      unless comment.empty?
        txt << case line.dig(2, 0)
               when '-'
                 format(comment, Config.get(:code, :deleted))
               else
                 format(comment, Config.get(:code, :replaced))
               end
      end
      txt
    when '+', '>'
      txt << format(flags, Config.inserted)
      txt << case line.dig(1, 0)
             when 'd'
               format(code, Config.get(:code, :comment, :dupcomment, :duplicated))
             when '+'
               format(code, Config.get(:comment, :dupcode, :dupcomment, :inserted))
             when 'e'
               format(code, Config.get(:comment, :dupcode, :dupcomment, :edited))
             else # t
               format(code, Config.get(:code, :comment, :dupcode, :dupcomment, :touched))
             end
      unless comment.empty?
        txt << case line.dig(2, 0)
               when 'd'
                 format(comment, Config.get(:code, :comment, :dupcode, :duplicated))
               when '+'
                 format(comment, Config.get(:code, :dupcode, :dupcomment, :inserted))
               when 'e'
                 format(comment, Config.get(:code, :dupcode, :dupcomment, :edited))
               else # t
                 format(comment, Config.get(:code, :comment, :dupcode, :dupcomment, :touched))
               end
      end
      txt
    end
  end
  # rubocop:enable Metrics, Layout/LineLength
end

#eachObject

:reek:TooManyStatements



115
116
117
118
119
120
121
122
123
124
# File 'lib/colora/lines.rb', line 115

def each
  @lines.each do |line|
    next if filtered?(line)

    txt = txt_formatter(line)
    yield txt if txt
  end
  reset_lexer
  reset_lang
end

#filehandleObject

:reek:DuplicateMethodCall :reek:UtilityFunction



10
11
12
13
14
15
16
17
18
# File 'lib/colora/lines.rb', line 10

def filehandle
  if Config.git
    IO.popen("git diff #{Config.file}")
  elsif Config.file
    File.open(Config.file)
  else
    $stdin
  end
end

#filtered?(line, str = line.is_a?(String)) ⇒ Boolean

:reek:ControlParameter

Returns:

  • (Boolean)


94
95
96
97
98
99
100
# File 'lib/colora/lines.rb', line 94

def filtered?(line, str = line.is_a?(String))
  toggle(line) if str
  return true unless @on
  return false if str

  by_filters?(line)
end

#format(line, color = nil) ⇒ Object

:reek:NilCheck



145
146
147
148
149
150
151
152
153
154
# File 'lib/colora/lines.rb', line 145

def format(line, color = nil)
  case color
  when nil
    @formatter.format(@lexer.lex(line))
  when :lang
    @formatter.format(@lang.lex(line))
  else
    Paint[line, *color]
  end
end

#formatterObject

:reek:DuplicateMethodCall

Raises:



28
29
30
31
32
33
# File 'lib/colora/lines.rb', line 28

def formatter
  theme = Rouge::Theme.find(Config.theme)
  raise Error, "Unrecognized theme: #{Config.theme}" unless theme

  Rouge::Formatters::Terminal256.new(theme.new)
end

#get_lines(getter = filehandle) ⇒ Object

:reek:FeatureEnvy



21
22
23
24
25
# File 'lib/colora/lines.rb', line 21

def get_lines(getter = filehandle)
  getter.readlines.map(&:chomp)
ensure
  getter.close
end

#guess_lexerObject



46
47
48
49
50
# File 'lib/colora/lines.rb', line 46

def guess_lexer
  return Rouge::Lexers::Diff if Config.git

  guess_lexer_by_file || guess_lexer_by_source
end

#guess_lexer_by_file(file = Config.file) ⇒ Object

:reek:UtilityFunction



53
54
55
56
57
# File 'lib/colora/lines.rb', line 53

def guess_lexer_by_file(file = Config.file)
  return nil unless file && !File.extname(file).empty?

  Rouge::Lexer.guess_by_filename(file)
end

#guess_lexer_by_source(source = @lines[0]) ⇒ Object



59
60
61
62
63
64
65
66
67
68
# File 'lib/colora/lines.rb', line 59

def guess_lexer_by_source(source = @lines[0])
  case source
  when /^---( #.*)?$/, /^# /
    Rouge::Lexers::Markdown
  when %r{^--- [\w./]}
    Rouge::Lexers::Diff
  else
    Rouge::Lexer.guess_by_source(source)
  end
end

#markdown(line) ⇒ Object

Markdown plug :reek:TooManyStatements rubocop:disable Metrics



13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/colora/plugs/markdown.rb', line 13

def markdown(line)
  txt = nil
  case line
  when /^```(\w+)$/
    txt = format(line)
    reset_lexer($LAST_MATCH_INFO[1])
  when /^```$/
    reset_lexer
    txt = format(line)
  else
    txt = format(line)
  end
  txt
end

#reset_lang(lang = nil) ⇒ Object

:reek:NilCheck



136
137
138
139
140
141
142
# File 'lib/colora/lines.rb', line 136

def reset_lang(lang = nil)
  @lang  = if lang.nil?
             @orig_lang
           else
             Rouge::Lexer.find_fancy(lang) || @orig_lang
           end
end

#reset_lang_by_filename(file) ⇒ Object



156
157
158
# File 'lib/colora/lines.rb', line 156

def reset_lang_by_filename(file)
  @lang = Rouge::Lexer.guess_by_filename(file)
end

#reset_lang_by_source(source) ⇒ Object



160
161
162
# File 'lib/colora/lines.rb', line 160

def reset_lang_by_source(source)
  @lang = Rouge::Lexer.guess_by_source(source)
end

#reset_lexer(lang = nil) ⇒ Object

:reek:NilCheck



127
128
129
130
131
132
133
# File 'lib/colora/lines.rb', line 127

def reset_lexer(lang = nil)
  @lexer = if lang.nil?
             @orig_lexer
           else
             Rouge::Lexer.find_fancy(lang) || @orig_lexer
           end
end

#to_aObject



164
# File 'lib/colora/lines.rb', line 164

def to_a = @lines

#toggle(line) ⇒ Object

:reek:ControlParameter



71
72
73
74
75
76
77
# File 'lib/colora/lines.rb', line 71

def toggle(line)
  if @on
    @on = false if Config.off&.match?(line)
  elsif Config.on&.match?(line)
    @on = true
  end
end

#txt_formatter(line) ⇒ Object



105
106
107
108
109
110
111
112
# File 'lib/colora/lines.rb', line 105

def txt_formatter(line)
  # Is there a plugin for @tag? If so, use it: Else use the lexer.
  if Lines.plugins.include?(@tag)
    send(@tag, line)
  else
    @formatter.format(@lexer.lex(line))
  end
end