Class: TexWarning

Inherits:
Object
  • Object
show all
Defined in:
lib/trex.rb

Overview

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, pattern, printPattern = /.*/, limit = 10, additionaInputLines = 0) ⇒ TexWarning

Returns a new instance of TexWarning.



571
572
573
574
575
576
577
578
579
580
# File 'lib/trex.rb', line 571

def initialize(name, pattern, printPattern=/.*/, limit=10,
               additionaInputLines=0)
    @name                     = name
    @pattern                  = pattern
    @printPattern             = printPattern
    @errors                   = []
    @limit                    = limit
    @maxLinesWidth            = 0
    @additionaInputLines      = additionaInputLines
end

Instance Attribute Details

#filestateObject

Returns the value of attribute filestate.



569
570
571
# File 'lib/trex.rb', line 569

def filestate
  @filestate
end

#limitObject (readonly)

Returns the value of attribute limit.



568
569
570
# File 'lib/trex.rb', line 568

def limit
  @limit
end

Instance Method Details

#add_error(line, string) ⇒ Object



613
614
615
616
617
618
# File 'lib/trex.rb', line 613

def add_error(line, string)
    return if self.has_error?(line, string) # avoid duplicate errors
    line = self.render_line(line) unless line == "-"
    @errors.push([line, string.to_s])
    @maxLinesWidth = [@maxLinesWidth, line.size].max
end

#empty?Boolean

Returns:

  • (Boolean)


674
675
676
# File 'lib/trex.rb', line 674

def empty?
    self.size == 0
end

#expand_input(string, index, lines) ⇒ Object



592
593
594
595
596
# File 'lib/trex.rb', line 592

def expand_input(string, index, lines)
    return string if @additionaInputLines == 0
    to = [index + @additionaInputLines, lines.size].min
    return lines[index..to].join("\n") + "\n"
end

#extract_line(string, index, lines) ⇒ Object



598
599
600
601
602
603
604
605
606
607
# File 'lib/trex.rb', line 598

def extract_line(string, index, lines)
    # force 'line ..' first
    if /line(?:s)? (?<line>[0-9\-]+)/ =~ string
        return line
    end
    if  /(?:line(?:s)? |l[^0-9])(?<line>[0-9\-]+)/ =~ string
        return line
    end
    return "-"
end

#format_error(line, string) ⇒ Object



645
646
647
648
# File 'lib/trex.rb', line 645

def format_error(line, string)
    message = self.format_warning_message(line, string)
    "#{line.ljust(@maxLinesWidth).yellow} #{message.to_s.chomp}"
end

#format_warning_message(line, string) ⇒ Object



650
651
652
653
654
655
656
657
658
# File 'lib/trex.rb', line 650

def format_warning_message(line, string)
    if @printPattern.instance_of? Proc
        puts self.class, @name
        return @printPattern.call(line, string)
    end
    matched = @printPattern.match string
    return string if not matched
    return matched
end

#handle(string, index = nil, lines = nil) ⇒ Object



582
583
584
585
586
587
588
589
590
# File 'lib/trex.rb', line 582

def handle(string, index=nil, lines=nil)
    if not string.match(@pattern) or string.empty?
        return false
    end
    string = self.expand_input(string, index, lines)
    line   = self.extract_line(string, index, lines)
    self.add_error(line, string)
    true
end

#has_error?(line, string) ⇒ Boolean

Returns:

  • (Boolean)


620
621
622
623
624
625
# File 'lib/trex.rb', line 620

def has_error?(line, string)
    @errors.each { |e|
        return true if e[0] == line and e[1] == string
    }
    false
end

#render_line(line) ⇒ Object



609
610
611
# File 'lib/trex.rb', line 609

def render_line(line)
    (@filestate and @filestate.state or "") + line.to_s
end

#sizeObject



670
671
672
# File 'lib/trex.rb', line 670

def size
    @errors.size
end

#sortObject



660
661
662
663
664
665
666
667
668
# File 'lib/trex.rb', line 660

def sort
    # magic from
    # http://www.bofh.org.uk/2007/12/16/comprehensible-sorting-in-ruby
    # for number sensible sorting
    @errors.sort_by {|k|
        k.to_s.split(/((?:(?:^|\s)[-+])?(?:\.\d+|\d+(?:\.\d+?(?:[eE]\d+)?(?:$|(?![eE\.])))?))/ms).
            map {|v| Float(v) rescue v.downcase}
    }
end

#to_sObject



627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
# File 'lib/trex.rb', line 627

def to_s
    if self.empty?
        return ""
    end
    self.sort
    limit = [@limit, @errors.size].min
    str   = @name.red.bold + " [#{@errors.size}]: "
    str   += "\n" #if self.size > 1 and limit > 0
    limit.times { |i|
        str += "    #{self.format_error(@errors[i][0], @errors[i][1])}\n"
    }
    if @limit < @errors.size and @limit != 0
        str += "    #{"...".ljust(@maxLinesWidth)} ...\n"
    end
    #str += "\n"
    str
end