Class: NanDoc::DiffProxy::Diff

Inherits:
Object
  • Object
show all
Defined in:
lib/nandoc/support/diff-proxy.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(out, err, args) ⇒ Diff

Returns a new instance of Diff.



80
81
82
83
84
# File 'lib/nandoc/support/diff-proxy.rb', line 80

def initialize out, err, args
  @out = out
  @error = err
  @args = args
end

Instance Attribute Details

#errorObject (readonly)

Returns the value of attribute error.



85
86
87
# File 'lib/nandoc/support/diff-proxy.rb', line 85

def error
  @error
end

Class Method Details

.default_diff_stylesheetObject



37
38
39
40
41
42
43
44
45
# File 'lib/nandoc/support/diff-proxy.rb', line 37

def default_diff_stylesheet
  @default_diff_stylesheet ||= {
    :header => [:bold, :yellow],
    :add    => [:bold, :green],
    :remove => [:bold, :red],
    :range  => [:bold, :magenta],
    :trailing_whitespace => [:background, :red]
  }
end

.stream_colorizer_prototypeObject



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
# File 'lib/nandoc/support/diff-proxy.rb', line 46

def stream_colorizer_prototype
  @stream_colorizer_prototype ||= begin
    require File.dirname(__FILE__)+'/stream-colorizer.rb'
    NanDoc::StreamColorizer.new do |sc|
      sc.stylesheet_merge(default_diff_stylesheet)
      sc.when %r(\Adiff ), :state=>:header
      sc.when(:header) do |o|
        o.style :header
        o.when %r(\A@@), :state=>:range
      end
      sc.when(:range) do |o|
        o.style :range
        o.when_not %r(\A@@), :state=>:plain
      end
      sc.when(:plain) do |o|
        o.style nil
        o.when %r(\Adiff ), :state=>:header
        o.when %r(\A\+), :state=>:add
        o.when %r(\A\-), :state=>:remove
      end
      sc.when(:add) do |o|
        o.style :add
        o.trailing_whitespace_style :trailing_whitespace
        o.when_not %r(\A\+), :state=>:plain
      end
      sc.when(:remove) do |o|
        o.style :remove
        o.trailing_whitespace_style :trailing_whitespace
        o.when_not %r(\A\-), :state=>:plain
      end
    end
  end
end

Instance Method Details

#colorize(out, opts = {}) ⇒ Object



97
98
99
100
101
102
103
# File 'lib/nandoc/support/diff-proxy.rb', line 97

def colorize out, opts={}
  colorizer = self.class.stream_colorizer_prototype.spawn do |c|
    c.stylesheet_merge(opts[:styles] || {})
  end
  colorizer.filter(to_s, out)
  nil
end

#commandObject



86
87
88
# File 'lib/nandoc/support/diff-proxy.rb', line 86

def command
  Shellwords.join(@args)
end

#error?Boolean

Returns:

  • (Boolean)


89
# File 'lib/nandoc/support/diff-proxy.rb', line 89

def error?; ! @error.empty? end

#full_error_messageObject



90
91
92
# File 'lib/nandoc/support/diff-proxy.rb', line 90

def full_error_message
  "diff failed: #{command}\ngot error: #{error}"
end

#ok?Boolean

Returns:

  • (Boolean)


93
# File 'lib/nandoc/support/diff-proxy.rb', line 93

def ok?; ! error? end

#to_sObject



94
95
96
# File 'lib/nandoc/support/diff-proxy.rb', line 94

def to_s
  @out
end