-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathgd
executable file
·75 lines (62 loc) · 2.39 KB
/
gd
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
#!/usr/bin/ruby
# frozen_string_literal: true
require 'json'
require 'sqlite3'
Dir_only = ARGV[0] == 'dir_only' ? 'isdir = 1 AND' : ''
Suggest_website = ARGV[1] == 'suggest_website'
Typed_query = ARGV[2]
Query = Typed_query.split(' ').map { |w| "%#{w.unicode_normalize}%" }
Limit = ENV['result_limit'].to_i <= 0 ? 50 : ENV['result_limit'].to_i
Tmp_file = File.join(ENV['alfred_workflow_cache'], 'tmp.db')
Cache_file = File.join(ENV['alfred_workflow_cache'], 'cache.db')
# If cache file does not exist
unless File.exist?(Cache_file)
# Check if cache is being built
require 'open3'
if Open3.capture2('ps', 'A').first.split("\n").any? { |l| l.match(/ruby.*Alfred.*rebuild_cache/) }
puts({ items: [{
title: 'Cache is being built…',
subtitle: 'Please be patient. Depending on how many files you have, it can take a while.',
'valid': false
}] }.to_json)
exit 0
end
puts({ items: [{
variables: { action: 'build_cache' },
title: 'Build cache',
subtitle: 'It will take a few minutes to finish, depending on how many files you have.',
'arg': 'build_cache'
}] }.to_json)
exit 0
end
# Filter paths
db = SQLite3::Database.new(Cache_file)
Results = db.execute("SELECT fullpath FROM main WHERE #{Dir_only} #{Array.new(Query.length, 'basename LIKE ?').join(' AND ')} ORDER BY accesstime DESC LIMIT ?;", Query, Limit).flatten
Init_script_filter_items = Results.each_with_object([]) { |path, array|
short_path = path.sub(%r{^/Users/.+?/}, '~/').sub(%r{^~/Library/CloudStorage/}, '')
basename = File.basename(path)
array.push(
uid: path,
type: 'file',
title: basename,
subtitle: short_path,
icon: { path: path, type: 'fileicon' },
arg: path,
mods: {
opt: { variables: { action: 'reveal_in_finder' }, subtitle: 'Reveal in Finder' },
shift: { variables: { action: 'search_website' }, subtitle: 'Search on Google Drive’s website', arg: basename },
'cmd+alt+ctrl': { variables: { action: 'build_cache' }, subtitle: 'Force Cache Update' }
}
)
}
Script_filter_items = lambda {
return Init_script_filter_items unless Suggest_website
# Append item for website search
return Init_script_filter_items.push({
variables: { action: 'search_website' },
title: "Search “#{Typed_query}” on Google Drive’s website",
icon: { path: 'icon.png' },
arg: Typed_query
})
}.call
puts({ items: Script_filter_items }.to_json)