Class: Icersplicer::FileProcessor
- Inherits:
-
Object
- Object
- Icersplicer::FileProcessor
- Includes:
- OutputFile
- Defined in:
- lib/icersplicer.rb
Constant Summary collapse
- COLOURS =
{0 => "black", 1 => "red", 2 => "green", 3 => "yellow", 4 => "blue", 5 => "purple", 6 => "cyan", 7 => "white"}
Instance Attribute Summary collapse
-
#debug ⇒ Object
writeonly
Sets the attribute debug.
-
#keywordsfile ⇒ Object
writeonly
Sets the attribute keywordsfile.
-
#nohighlighter ⇒ Object
writeonly
Sets the attribute nohighlighter.
-
#nolinenumbers ⇒ Object
writeonly
Sets the attribute nolinenumbers.
-
#skip_lines ⇒ Object
writeonly
Sets the attribute skip_lines.
Instance Method Summary collapse
- #countlines(inputfile) ⇒ Object
- #filterlinestats(filterlines) ⇒ Object
- #followtail(filename, number) ⇒ Object
-
#initialize ⇒ FileProcessor
constructor
A new instance of FileProcessor.
- #load_keywords(file) ⇒ Object
- #print_to_screen(linenum, text, quiet) ⇒ Object
- #reset_screen ⇒ Object
- #skip(line) ⇒ Object
- #skip_processor(filter) ⇒ Object
- #stats(inputfile, outputfile) ⇒ Object
- #text_highlighter(text) ⇒ Object
- #text_processor(data) ⇒ Object
Methods included from OutputFile
#close, #open, #processdata, #write
Constructor Details
#initialize ⇒ FileProcessor
Returns a new instance of FileProcessor.
64 65 66 67 68 |
# File 'lib/icersplicer.rb', line 64 def initialize @keywordsfile = "keywords.ice" @debug = 0 @nolinenumbers = false end |
Instance Attribute Details
#debug=(value) ⇒ Object (writeonly)
Sets the attribute debug
51 52 53 |
# File 'lib/icersplicer.rb', line 51 def debug=(value) @debug = value end |
#keywordsfile=(value) ⇒ Object (writeonly)
Sets the attribute keywordsfile
51 52 53 |
# File 'lib/icersplicer.rb', line 51 def keywordsfile=(value) @keywordsfile = value end |
#nohighlighter=(value) ⇒ Object (writeonly)
Sets the attribute nohighlighter
51 52 53 |
# File 'lib/icersplicer.rb', line 51 def nohighlighter=(value) @nohighlighter = value end |
#nolinenumbers=(value) ⇒ Object (writeonly)
Sets the attribute nolinenumbers
51 52 53 |
# File 'lib/icersplicer.rb', line 51 def nolinenumbers=(value) @nolinenumbers = value end |
#skip_lines=(value) ⇒ Object (writeonly)
Sets the attribute skip_lines
51 52 53 |
# File 'lib/icersplicer.rb', line 51 def skip_lines=(value) @skip_lines = value end |
Instance Method Details
#countlines(inputfile) ⇒ Object
140 141 142 143 144 145 146 147 148 149 150 151 152 153 |
# File 'lib/icersplicer.rb', line 140 def countlines(inputfile) lines = 0 unless inputfile == nil if File.exist?(inputfile) File.open(inputfile) {|n| n.each_line { lines += 1 } } puts "Filename: #{inputfile} Total Line Count: #{lines}" end end return lines end |
#filterlinestats(filterlines) ⇒ Object
74 75 76 |
# File 'lib/icersplicer.rb', line 74 def filterlinestats(filterlines) puts "\nLines Displayed by Filter: #{filterlines}" end |
#followtail(filename, number) ⇒ Object
78 79 80 81 82 83 84 85 |
# File 'lib/icersplicer.rb', line 78 def followtail(filename, number) File::Tail::Logfile.open(filename) do |log| log.interval = 3 log.backward(10) log.backward(number).tail { |line| puts line } end exit end |
#load_keywords(file) ⇒ Object
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/icersplicer.rb', line 87 def load_keywords(file) keys = Hash.new linenum = 0 unless Dir.exist?("#{Dir.home}/.icersplicer") Dir.mkdir("#{Dir.home}/.icersplicer") end if File.exists?("#{Dir.home}/.icersplicer/#{file}") File.open("#{Dir.home}/.icersplicer/#{file}") {|n| n.each_line {|l| keys.update({linenum => "#{l.strip}"}) unless l.strip == "" puts "L: #{l.strip}" if @debug >= 1 linenum += 1 } } return keys else return false end end |
#print_to_screen(linenum, text, quiet) ⇒ Object
192 193 194 195 196 197 |
# File 'lib/icersplicer.rb', line 192 def print_to_screen(linenum, text, quiet) unless @nolinenumbers == true or quiet == true print Rainbow.new.wrap("Ln: #{linenum}:").yellow end print "#{text}" unless quiet == true end |
#reset_screen ⇒ Object
70 71 72 |
# File 'lib/icersplicer.rb', line 70 def reset_screen puts "\e[0m\ " end |
#skip(line) ⇒ Object
178 179 180 181 182 183 184 185 186 187 188 189 190 |
# File 'lib/icersplicer.rb', line 178 def skip(line) begin if instance_variable_defined?("@skip_lines") line_element = @skip_lines.key(line) if line_element != nil skiper = @skip_lines[line_element] end end rescue NoMethodError return nil end return skiper end |
#skip_processor(filter) ⇒ Object
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 |
# File 'lib/icersplicer.rb', line 155 def skip_processor(filter) skip_lines = Hash.new skipcounter = 0 filter.to_s.split(",").each {|n| skip_lines.update({skipcounter => n.to_i}) # Allow line ranges min = n.split("-")[0].to_i max = n.split("-")[1].to_i puts "Min: #{min} Max: #{max}" if @debug >= 2 unless n.split("-")[1] == nil if min > max raise RangeError, "Range Error: Minimun value can't be more than Maxiumun Range value" end min.upto(max) {|s| skip_lines.update({skipcounter => s}) unless skip_lines[skip_lines.size - 1] == s skipcounter += 1 } end skipcounter += 1 } return skip_lines end |
#stats(inputfile, outputfile) ⇒ Object
199 200 201 202 203 |
# File 'lib/icersplicer.rb', line 199 def stats(inputfile, outputfile) puts "Skip Lines #{@skip_lines}" if @debug >= 1 print "Inputfile lines: " countlines(inputfile) end |
#text_highlighter(text) ⇒ Object
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/icersplicer.rb', line 116 def text_highlighter(text) keys ||= load_keywords("#{@keywordsfile}") unless keys.class == Hash keys = {0 => "Ln:", 1 => "SELECT", 2 => "CREATE TABLE", 3 => "UPDATE", 4 => "DELETE", 5 => "INSERT"} end cpicker = [2,3,4,1,7,5,6] # Just a selection of colours keys.each {|n| if n[1].split("##")[1] == nil name = COLOURS[rand(COLOURS.size - 1)] text.gsub!("#{n[1]}", Rainbow.new.wrap("#{n[1]}").send(name.to_sym)) else nval = n[1].split("##")[0]; puts "Value: #{nval}" if @debug >= 3 name = n[1].split("##")[1].split("=")[1]; puts "Value: #{name}" if @debug >= 3 text.gsub!("#{nval}", Rainbow.new.wrap("#{nval}").send(name.to_sym)) end } return text end |
#text_processor(data) ⇒ Object
107 108 109 110 111 112 113 114 |
# File 'lib/icersplicer.rb', line 107 def text_processor(data) unless @nohighlighter == "OFF" data = text_highlighter(data) return data else return data end end |