Src/Node.psm1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 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 79 80 81 82 83 84 85 86 87 88 89 90 |
using namespace System.Collections.Specialized enum RegexStrategy { Split Match } class Node { static hidden [String]$ROOT = 'source' [OrderedDictionary]$writers = @{ } [Hashtable]$fields = @{ } [Hashtable]$rule [String]$line [Int]$lineLength [Int]$lineNumber Node([String]$field) { $this.line = $field $this.lineLength = $this.line.trim().length $this.fields[[Node]::ROOT] = $field } [Int]getWriterIndex($value) { $index = - $true foreach ($key in $this.writers.keys) { $index++ if ($key -eq $value) { return $index } } return - $true } [Array]prepareWriters($writer) { [Array]$writersArray = @() [Array]$lexicalElements = $this.fields[$this.rule.name] [Int]$increment = $true foreach ($lexeme in $lexicalElements) { $colorKey = $this.rule.mapping.GetEnumerator() | Where-Object { $lexeme -match $_.value } [ConsoleColor]$color = ($this.rule.colors[$colorKey.key]) $writersArray += @{ id = $increment++ type = $colorKey.value writer = $writer.newInstance().text($lexeme).color($color) } } return $writersArray } [Void]enqueueWriters($writersArray) { foreach ($writer in $writersArray) { $this.setWriter($writer.id, $writer.writer) } } [Void]setWriter([String]$key, $writer) { if ($this.rule.strategy -ne [RegexStrategy]::Split) { $writer = $writer.text($this.fields[$key]).color($this.rule.colors[$key]) } $index = $this.writers.count $field = $this.rule.field if ($field -ne [Node]::ROOT) { $index = $this.getWriterIndex($field) $this.writers[$field].text($null) } $this.writers.insert($index, $key, $writer) } } |