Class: BioSyntax::Highlighter

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

Overview

Stateful highlighter for one input format.

Reuse one highlighter for one logical input stream. Some formats, such as FASTQ and WIG, need line-to-line state. Call #reset before reusing the object for another stream.

Examples:

highlighter = BioSyntax[:vcf]
File.foreach("sample.vcf", chomp: false) do |line|
  print highlighter.colorize(line)
end

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(format) ⇒ Highlighter

Returns a new instance of Highlighter.

Parameters:

  • format (Format, Symbol, String, Integer)

    format object, name, alias, or native id

Raises:



287
288
289
290
# File 'lib/biosyntax.rb', line 287

def initialize(format)
  @format = BioSyntax.format(format)
  @state = Native::State.new(@format.id)
end

Instance Attribute Details

#formatFormat (readonly)

Returns format metadata for this highlighter.

Returns:

  • (Format)

    format metadata for this highlighter



283
284
285
# File 'lib/biosyntax.rb', line 283

def format
  @format
end

Instance Method Details

#colorize(line) ⇒ String Also known as: colorize_line, render_ansi, render_ansi_line

Highlight one input line and return ANSI-colored text.

Parameters:

  • line (String)

    one input line

Returns:

  • (String)

    line with ANSI SGR escape sequences



310
311
312
# File 'lib/biosyntax.rb', line 310

def colorize(line)
  @state.colorize(line)
end

#format_nameSymbol

Returns canonical format name.

Returns:

  • (Symbol)

    canonical format name



293
294
295
# File 'lib/biosyntax.rb', line 293

def format_name
  @format.name
end

#highlight(line) ⇒ Array<Span> Also known as: highlight_line

Highlight one input line and return semantic spans.

Parameters:

  • line (String)

    one input line

Returns:

  • (Array<Span>)

    highlighted spans for the line



301
302
303
# File 'lib/biosyntax.rb', line 301

def highlight(line)
  @state.highlight(line)
end

#inspectString

Returns:

  • (String)


338
339
340
# File 'lib/biosyntax.rb', line 338

def inspect
  "#<#{self.class} format=#{format_name.inspect} line_no=#{line_no}>"
end

#line_noInteger

Number of lines processed since initialization or the last reset.

Returns:

  • (Integer)


328
329
330
# File 'lib/biosyntax.rb', line 328

def line_no
  @state.line_no
end

#resetHighlighter

Reset line-oriented parser state.

Returns:



320
321
322
323
# File 'lib/biosyntax.rb', line 320

def reset
  @state.reset(@format.id)
  self
end

#stateful?Boolean

Returns true when this format depends on previous lines.

Returns:

  • (Boolean)

    true when this format depends on previous lines



333
334
335
# File 'lib/biosyntax.rb', line 333

def stateful?
  @format.stateful?
end