-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlseq
executable file
·86 lines (71 loc) · 2.21 KB
/
lseq
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
#!/usr/bin/env ruby
# Search for and rank pages in logseq graph by number of chars or lines
#
# Usage: lseq [-h] [-m] [-l] [-i] [<page-name-substring>]
require 'optparse'
def count_entities(filepath, mode)
content = File.read(filepath)
mode == 'lines' ? content.lines.size : content.size
end
def print_help
puts <<~HELP
Usage: lseq [-h] [-m] [-l] [-i] [<page-name-substring>]
Options:
-h, --help Show this help message and exit
-m Count characters (default)
-l Count lines
-i Ignore files starting with 'ARCHIVE'
HELP
end
def main
unless ENV['LOGSEQ_GRAPH']
puts "Error: LOGSEQ_GRAPH environment variable must be defined."
exit 1
end
directory = "#{ENV['LOGSEQ_GRAPH']}/pages"
mode = 'chars'
ignore_archive = false
substring = nil
options = {}
OptionParser.new do |opts|
opts.banner = "Usage: lseq [options] [<page-name-substring>]"
opts.on('-h', '--help', 'Show this help message and exit') do
print_help
exit
end
opts.on('-m', 'Count characters (default)') do
mode = 'chars'
end
opts.on('-l', 'Count lines') do
mode = 'lines'
end
opts.on('-i', 'Ignore files starting with ARCHIVE') do
ignore_archive = true
end
end.parse!
substring = ARGV.pop if ARGV.any?
matching_files = Dir.entries(directory).select do |filename|
next if filename == '.' || filename == '..'
next if ignore_archive && filename.start_with?('ARCHIVE')
if substring
substring_with_percent2f = substring.gsub('/', '%2F')
substring_with_dot = substring_with_percent2f.gsub('%2F', '.')
filename.include?(substring_with_percent2f) || filename.include?(substring_with_dot)
else
true
end
end.compact.map do |filename|
filepath = File.join(directory, filename)
entity_count = count_entities(filepath, mode)
[filename, entity_count]
end
matching_files.sort_by! { |_, count| count }
entity_name = mode == 'lines' ? 'lines' : 'characters'
matching_files.each do |filename, entity_count|
base = File.basename(filename, '.*').gsub('%2F', ' / ').gsub('.', ' / ')
puts "#{entity_count}:\t #{base}"
end
end
if __FILE__ == $0
main
end