Class: Colora::Lines

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

Instance Method Summary collapse

Constructor Details

#initializeLines

Returns a new instance of Lines.



38
39
40
41
42
43
44
45
46
47
# File 'lib/colora/lines.rb', line 38

def initialize
  @formatter = formatter
  @lines = get_lines
  @lexer = @orig_lexer = guess_lexer
  @tag   = @lexer.tag
  @lines = @tag=='diff' ? Data.new(@lines).lines : @lines
  @lang  = @orig_lang = Rouge::Lexer.find_fancy(Config.lang)
  @pad0  = '    '
  @pad1  = '  '
end

Instance Method Details

#diff(line) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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
# File 'lib/colora/plugs/diff.rb', line 3

def diff(line)
  case line
  when String
    case line
    when /^[-+][-+][-+] [ab]\/(.*)$/
      reset_lang_by_filename($~[1])
      format(line)
    when /^\s*#!/
      reset_lang_by_source(line)
      format(line) unless Config.quiet
    when /^ /
      format(pad(line), :lang) unless Config.quiet
    else
      format(line) unless Config.quiet
    end
  else
    # Initialized text variables
    txt = ''
    flags = flags(line)
    code = line.dig(1,1)||''
    comment = line.dig(2,1)||''
    # txt << flags+code
    case line[0]
    when '-', '<'
      txt << format(flags+code+comment)
      comment = '' # will skip commenting below
    when '+', '>'
      case line.dig(1,0)
      when nil, 't'
        txt << format(flags+code)
      when 'd'
        txt << format(flags, Config.dupplicated_flag)
        txt << format(code, :lang)
      when '>'
        txt << format(flags, Config.inserted_flag)
        txt << format(code, :lang)
      when 'e'
        txt << format(flags, Config.edited_flag)
        txt << format(code, :lang)
      else
        warn "Unknown code type: #{line[0]}"
      end
    else
      warn "Unknown line type: #{line[0]}"
    end
    # txt << comment
    unless comment.empty?
      case line.dig(2,0)
      when 't'
        txt << format(comment, Config.moved_comment)
      when 'd'
        txt << format(comment, Config.dupplicated_comment)
      when '>'
        txt << format(comment, Config.inserted_comment)
      when 'e'
        txt << format(comment, Config.edited_comment)
      else
        warn "Unknown comment type: #{line[0]}"
      end
    end
    txt
  end
end

#eachObject



100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/colora/lines.rb', line 100

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

    # Is there a plugin for @tag? If so, use it: Else use the lexer.
    txt = respond_to?(@tag) ? send(@tag, line) :
                              @formatter.format(@lexer.lex(line))
    yield txt if txt
  end
  reset_lexer
  reset_lang
end

#filehandleObject



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

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

#filtered?(line) ⇒ Boolean

Returns:

  • (Boolean)


51
52
53
54
55
56
57
58
59
60
61
# File 'lib/colora/lines.rb', line 51

def filtered?(line)
  return false if line.is_a?(String)

  (Config.green && '-<'.include?(line[0])) ||
    (Config.red && '+>'.include?(line[0])) ||
    (Config.code && [nil,'t'].include?(line.dig 1,0)) ||
    (Config.comment && [nil, 't'].include?(line.dig 2,0)) ||
    (Config.dupcode && line.dig(1,0)=='d') ||
    (Config.dupcomment && line.dig(2,0)=='d') ||
    false
end

#flags(line) ⇒ Object



67
68
69
# File 'lib/colora/lines.rb', line 67

def flags(line)
    line[0] + (line.dig(1,0)||'*') + (line.dig(2,0)||'*') + @pad1
end

#format(line, color = nil) ⇒ Object



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

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

Raises:



3
4
5
6
7
# File 'lib/colora/lines.rb', line 3

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(fh = filehandle) ⇒ Object



34
35
36
# File 'lib/colora/lines.rb', line 34

def get_lines(fh = filehandle)
  fh.readlines.map(&:chomp)
end

#guess_lexerObject



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

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



9
10
11
12
# File 'lib/colora/lines.rb', line 9

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



14
15
16
17
18
19
20
21
# File 'lib/colora/lines.rb', line 14

def guess_lexer_by_source(source=@lines[0])
  case source
  when /^---/, /^# /
    Rouge::Lexers::Markdown
  else
    Rouge::Lexer.guess_by_source(source)
  end
end

#markdown(line) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/colora/plugs/markdown.rb', line 3

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

#pad(line) ⇒ Object



63
64
65
# File 'lib/colora/lines.rb', line 63

def pad(line)
  @pad0+line
end

#reset_lang(lang = nil) ⇒ Object



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

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

#reset_lang_by_filename(file) ⇒ Object



86
87
88
# File 'lib/colora/lines.rb', line 86

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

#reset_lang_by_source(source) ⇒ Object



82
83
84
# File 'lib/colora/lines.rb', line 82

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

#reset_lexer(lang = nil) ⇒ Object



90
91
92
93
# File 'lib/colora/lines.rb', line 90

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

#to_aObject



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

def to_a = @lines