-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjiayou
executable file
·57 lines (47 loc) · 991 Bytes
/
jiayou
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
#!/usr/bin/env ruby
require 'uri'
require 'toml'
# open String
class String
# open browser for url
def browser(foreground = false)
cmd = open_cmd
return unless cmd
cline = ("#{cmd} #{self} > /dev/null 2>&1" + (foreground ? '' : ' &'))
system cline
sleep(1.3) if foreground
end
def valid_url?
URI.parse(self)
true
rescue _
false
end
private
def open_cmd
os = RbConfig::CONFIG['host_os']
case os
when /linux|bsd/
# 'xdg-open'
'x-www-browser'
when /mswin|mingw|cygwin/
'start'
when /darwin/
'open'
end
end
end
if $PROGRAM_NAME == __FILE__
dir = File.dirname(__FILE__)
toml_file = File.join(dir, 'jiayou.toml')
puts "load from #{toml_file}..."
jiayou = TOML.load_file(toml_file)
foreground = ARGV.any? { |e| e.start_with? '--f' }
jiayou['urls']
.select(&:valid_url?)
.reverse
.map { |url| %("#{url}") }
.each do |url|
url.browser foreground
end
end