-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogfile.rb
More file actions
74 lines (61 loc) · 1.67 KB
/
logfile.rb
File metadata and controls
74 lines (61 loc) · 1.67 KB
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
require "colorize"
require_relative "ndex"
class Logfile
attr_accessor :path
attr_accessor :parent_session
def initialize(path, parent_session=nil)
@path = path
@parent_session = parent_session
end
def html
require "nokogiri"
@html = @html || File.open(path) { |f| Nokogiri::HTML(f) }
end
def find_lines(pattern)
matches = []
if extension == ".html" || extension == ".htm"
html.css(Ndex.message_selector).each do |line|
content = line.content
if Ndex.pattern_matches_regex?(content, pattern)
unless Ndex.search_exclusions.any? { |e| content.include?(e) }
matches << content
end
end
end
elsif extension == ".txt"
File.open(path, "r") do |file|
file.each_line do |line|
if Ndex.pattern_matches_regex?(line, pattern)
unless Ndex.search_exclusions.any? { |e| line.include?(e) }
matches << line
end
end
end
end
else
puts "Unrecognized filetype for #{path}"
[]
end
matches
end
def filename
File.basename(path, File.extname(path))
end
def extension
File.extname(path)
end
def match_name?(pattern)
path.downcase.include?(pattern.downcase)
end
def pp
session_str = "(#{parent_session.subfolder_id})".ljust(5).cyan.on_black
name = filename.ljust(24).capitalize.light_white.on_cyan
ls = `ls -Clh \"#{path}\"`.split(" ")
date = "(Date: #{ls[5]} #{ls[6]} #{ls[7]})".ljust(20).cyan.on_black
size = "(Size: #{ls[4]})".ljust(13).light_white.on_cyan
"#{session_str} #{name} #{date} #{size} Path: \"#{path}\""
end
def to_s
path
end
end