Class: ScripTTY::ScreenPattern
- Inherits:
-
Object
- Object
- ScripTTY::ScreenPattern
- Defined in:
- lib/scriptty/screen_pattern/generator.rb,
lib/scriptty/screen_pattern.rb
Overview
reopen
Defined Under Namespace
Classes: Generator
Instance Attribute Summary collapse
-
#cursor_pos ⇒ Object
The [row, column] of the cursor position (or nil if unspecified).
-
#name ⇒ Object
The name given to this pattern.
Class Method Summary collapse
- .from_term(term, opts = {}) ⇒ Object
- .from_text(text, opts = {}) ⇒ Object
-
.parse(s, filename = nil, lineno = nil) ⇒ Object
Parse a pattern file and return an array of ScreenPattern objects.
Instance Method Summary collapse
- #generate ⇒ Object
-
#initialize(name, properties) ⇒ ScreenPattern
constructor
:nodoc:.
- #inspect ⇒ Object
-
#match_term(term) ⇒ Object
Match this pattern against a Term object.
Constructor Details
#initialize(name, properties) ⇒ ScreenPattern
:nodoc:
59 60 61 62 63 64 65 66 |
# File 'lib/scriptty/screen_pattern.rb', line 59 def initialize(name, properties) # :nodoc: @name = name @position = properties["position"] @size = properties["size"] @cursor_pos = properties["cursor_pos"] @field_ranges = properties["fields"] # Hash of "field_name" => [row, col1..col2] ranges @matches = properties["matches"] # Array of [[row,col], string] records to match end |
Instance Attribute Details
#cursor_pos ⇒ Object
The [row, column] of the cursor position (or nil if unspecified)
57 58 59 |
# File 'lib/scriptty/screen_pattern.rb', line 57 def cursor_pos @cursor_pos end |
#name ⇒ Object
The name given to this pattern
54 55 56 |
# File 'lib/scriptty/screen_pattern.rb', line 54 def name @name end |
Class Method Details
.from_term(term, opts = {}) ⇒ Object
31 32 33 |
# File 'lib/scriptty/screen_pattern.rb', line 31 def from_term(term, opts={}) from_text(term.text, {:cursor_pos => term.cursor_pos}.merge(opts)) end |
.from_text(text, opts = {}) ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/scriptty/screen_pattern.rb', line 35 def from_text(text, opts={}) text = text.split(/\r?\n/n) if text.is_a?(String) name ||= opts[:name] || "untitled" width = text.map{|line| line.chars.to_a.length}.max height = text.length properties = {} properties['cursor_pos'] = opts[:cursor_pos] properties['size'] = [height, width] properties['matches'] = [] text.each_with_index{|line, i| properties['matches'] << [[i, 0], line] } new(name, properties) end |
.parse(s, filename = nil, lineno = nil) ⇒ Object
Parse a pattern file and return an array of ScreenPattern objects
23 24 25 26 27 28 29 |
# File 'lib/scriptty/screen_pattern.rb', line 23 def parse(s, filename=nil, lineno=nil) retval = [] Parser.parse(s, filename, lineno) do |spec| retval << new(spec[:name], spec[:properties]) end retval end |
Instance Method Details
#generate ⇒ Object
97 98 99 |
# File 'lib/scriptty/screen_pattern.rb', line 97 def generate Generator.generate(@name, :cursor_pos => @cursor_pos, :matches => @matches, :fields => @field_ranges, :position => @position, :size => @size) end |
#inspect ⇒ Object
68 69 70 |
# File 'lib/scriptty/screen_pattern.rb', line 68 def inspect "#<#{self.class.name}:#{sprintf("0x%x", object_id)} name=#{@name}>" end |
#match_term(term) ⇒ Object
Match this pattern against a Term object. If the match succeeds, return the Hash of fields extracted from the screen. Otherwise, return nil.
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/scriptty/screen_pattern.rb', line 74 def match_term(term) return nil if @cursor_pos and @cursor_pos != term.cursor_pos # XXX UNICODE if @matches text = term.text @matches.each do |pos, str| row, col = pos col_range = col..col+str.length-1 return nil unless text[row][col_range] == str end end fields = {} if @field_ranges @field_ranges.each_pair do |k, range| row, col_range = range fields[k] = text[row][col_range] end end fields end |