To check out this repository please hg clone the following URL, or open the URL using EasyMercurial or your preferred Mercurial client.

Statistics Download as Zip
| Branch: | Tag: | Revision:

root / .svn / pristine / 6e / 6eadc5531e55c7dfea168413bcc84110076f8d5b.svn-base @ 1297:0a574315af3e

History | View | Annotate | Download (2.09 KB)

1
module CodeRay
2
module Encoders
3
  
4
  # Makes a statistic for the given tokens.
5
  # 
6
  # Alias: +stats+
7
  class Statistic < Encoder
8
    
9
    register_for :statistic
10
    
11
    attr_reader :type_stats, :real_token_count  # :nodoc:
12
    
13
    TypeStats = Struct.new :count, :size  # :nodoc:
14
    
15
  protected
16
    
17
    def setup options
18
      super
19
      
20
      @type_stats = Hash.new { |h, k| h[k] = TypeStats.new 0, 0 }
21
      @real_token_count = 0
22
    end
23
    
24
    STATS = <<-STATS  # :nodoc:
25

    
26
Code Statistics
27

    
28
Tokens            %8d
29
  Non-Whitespace  %8d
30
Bytes Total       %8d
31

    
32
Token Types (%d):
33
  type                     count     ratio    size (average)
34
-------------------------------------------------------------
35
%s
36
    STATS
37
    
38
    TOKEN_TYPES_ROW = <<-TKR  # :nodoc:
39
  %-20s  %8d  %6.2f %%   %5.1f
40
    TKR
41
    
42
    def finish options
43
      all = @type_stats['TOTAL']
44
      all_count, all_size = all.count, all.size
45
      @type_stats.each do |type, stat|
46
        stat.size /= stat.count.to_f
47
      end
48
      types_stats = @type_stats.sort_by { |k, v| [-v.count, k.to_s] }.map do |k, v|
49
        TOKEN_TYPES_ROW % [k, v.count, 100.0 * v.count / all_count, v.size]
50
      end.join
51
      @out << STATS % [
52
        all_count, @real_token_count, all_size,
53
        @type_stats.delete_if { |k, v| k.is_a? String }.size,
54
        types_stats
55
      ]
56
      
57
      super
58
    end
59
    
60
  public
61
    
62
    def text_token text, kind
63
      @real_token_count += 1 unless kind == :space
64
      @type_stats[kind].count += 1
65
      @type_stats[kind].size += text.size
66
      @type_stats['TOTAL'].size += text.size
67
      @type_stats['TOTAL'].count += 1
68
    end
69
    
70
    # TODO Hierarchy handling
71
    def begin_group kind
72
      block_token ':begin_group', kind
73
    end
74
    
75
    def end_group kind
76
      block_token ':end_group', kind
77
    end
78
    
79
    def begin_line kind
80
      block_token ':begin_line', kind
81
    end
82
    
83
    def end_line kind
84
      block_token ':end_line', kind
85
    end
86
    
87
    def block_token action, kind
88
      @type_stats['TOTAL'].count += 1
89
      @type_stats[action].count += 1
90
      @type_stats[kind].count += 1
91
    end
92
    
93
  end
94
  
95
end
96
end