Class: Booker
Overview
get booker opening command
Class Attribute Summary collapse
Instance Method Summary
collapse
Methods included from Browser
#browse, #domain, #prep, #wrap
Methods included from OS
linux?, mac?, windows?
Constructor Details
#initialize(args) ⇒ Booker
Returns a new instance of Booker.
19
20
21
|
# File 'lib/booker.rb', line 19
def initialize(args)
parse args
end
|
Class Attribute Details
.version ⇒ Object
Returns the value of attribute version.
15
16
17
|
# File 'lib/booker.rb', line 15
def version
@version
end
|
Instance Method Details
#helper ⇒ Object
48
49
50
|
# File 'lib/booker.rb', line 48
def helper
pexit HELP_BANNER, 0
end
|
#install(args) ⇒ Object
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
|
# File 'lib/booker.rb', line 126
def install(args)
target = args.shift
exit 0 if target.nil?
if /comp/i.match(target) install_completion
elsif /book/i.match(target) install_bookmarks
elsif /conf/i.match(target) install_config
else pexit "Failure: ".red + "unknown installation option (#{target})", 1
end
install(args) end
|
#install_bookmarks ⇒ Object
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
|
# File 'lib/booker.rb', line 164
def install_bookmarks
puts 'searching for chrome bookmarks...'
begin
bms = [] [ '/Library/Application Support/Google/Chrome',
'/AppData/Local/Google/Chrome/User Data/Default',
'/.config/chromium/Default/',
'/.config/google-chrome/Default/',
].each do |f|
home = File.join(ENV['HOME'], f)
next if !FileTest.directory?(home)
Find.find(home) do |file|
bms << file if /chrom.*bookmarks/i.match file
end
end
if bms.empty? puts "Failure: ".red + 'bookmarks file could not be found.'
raise
else puts 'select bookmarks file: '
bms.each_with_index {|bm, i| puts i.to_s.grn + " - " + bm }
selected = bms[gets.chomp.to_i]
puts 'Selected: '.yel + selected
BConfig.new.write(:bookmarks, selected)
puts "Success: ".grn + "config file updated with your bookmarks"
end
rescue StandardError => e
puts e.message
pexit "Failure: ".red + "could not add bookmarks to config file ~/.booker", 1
end
end
|
#install_completion ⇒ Object
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
|
# File 'lib/booker.rb', line 143
def install_completion
begin
fpath = `zsh -c 'echo $fpath'`.split(' ')
rescue
pexit "Failure: ".red + "zsh is probably not installed, could not find $fpath", 1
end
fpath.each do |fp|
begin
File.open(fp + "/_booker", 'w') {|f| f.write(COMPLETION) }
system "zsh -c 'autoload -U _booker'"
puts "Success: ".grn + "installed zsh autocompletion in #{fp}"
break rescue
puts "Failure: ".red + "could not write ZSH completion _booker script to $fpath (#{fp})"
end
end
end
|
#install_config ⇒ Object
198
199
200
201
202
203
204
205
|
# File 'lib/booker.rb', line 198
def install_config
begin
BConfig.new.write
puts "Success: ".grn + "example config file written to ~/.booker"
rescue
pexit "Failure: ".red + "could not write example config file to ~/.booker", 1
end
end
|
#open_bookmark(bm) ⇒ Object
an array of ints, as bookmark ids
61
62
63
64
65
66
67
68
|
# File 'lib/booker.rb', line 61
def open_bookmark(bm)
id = bm.shift
url = Bookmarks.new.bookmark_url(id)
pexit "Failure:".red + " bookmark #{id} not found", 1 if url.nil?
puts 'opening bookmark ' + url + '...'
openweb(wrap(url))
open_bookmark bm unless bm.empty?
end
|
#open_search(term) ⇒ Object
70
71
72
73
74
|
# File 'lib/booker.rb', line 70
def open_search(term)
puts 'searching ' + term + '...'
search = BConfig.new.searcher
openweb(search + Shellwords.escape(term))
end
|
#openweb(url) ⇒ Object
56
57
58
|
# File 'lib/booker.rb', line 56
def openweb(url)
system(browse + wrap(url))
end
|
#parse(args) ⇒ Object
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
# File 'lib/booker.rb', line 23
def parse(args)
helper if args.none?
parse_opt args if /^-.*/.match(args.first)
browsearg = args.first
if browsearg.match(/^[0-9]/) open_bookmark args
elsif domain.match(browsearg) puts 'opening website: ' + browsearg
openweb(prep(browsearg))
else
open_search(args.join(' '))
end
end
|
#parse_opt(args) ⇒ Object
parse and execute any command line options
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
|
# File 'lib/booker.rb', line 77
def parse_opt(args)
valid_opts = %w{--version -v --install -i --help -h
--complete -c --bookmark -b --search -s}
nextarg = args.shift
errormsg = 'Error: '.red + "unrecognized option #{nextarg}"
pexit errormsg, 1 if ! (valid_opts.include? nextarg)
if nextarg == '--bookmark' || nextarg == '-b'
if args.first.nil?
pexit 'Error: '.red + 'booker --bookmark expects bookmark id', 1
else
open_bookmark args
end
end
if nextarg == '--complete' || nextarg == '-c'
allargs = args.join(' ')
bm = Bookmarks.new(allargs)
bm.autocomplete
end
if nextarg == '--install' || nextarg == '-i'
if !args.empty?
install(args)
else install(%w{completion config bookmarks})
end
end
if nextarg == '--search' || nextarg == '-s'
pexit '--search requires an argument', 1 if args.empty?
allargs = args.join(' ')
open_search allargs
end
version if nextarg == '--version' || nextarg == '-v'
helper if nextarg == '--help' || nextarg == '-h'
exit 0 end
|
#pexit(msg, sig) ⇒ Object
43
44
45
46
|
# File 'lib/booker.rb', line 43
def pexit(msg, sig)
puts msg
exit sig
end
|
#version ⇒ Object
52
53
54
|
# File 'lib/booker.rb', line 52
def version
pexit @version, 0
end
|