booker 0.3.2 → 0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (5) hide show
  1. checksums.yaml +4 -4
  2. data/lib/booker.rb +35 -27
  3. data/lib/bookmarks.rb +4 -8
  4. data/lib/config.rb +9 -8
  5. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 94d50219879478c9d6762be1e50de304c6cbc5d0
4
- data.tar.gz: 768a969566680d92447cc156d5f06df8421590ca
3
+ metadata.gz: ede393f061fc758e36d7ab8a09d414dbee51559c
4
+ data.tar.gz: 3390c07e38358019217b79fa4d73560a0e7941de
5
5
  SHA512:
6
- metadata.gz: 38be1cdef260f686f731ee9f912b6c16bbd09e50aa9dbeb8ca72d5536faac4ae2c3ec5161c066a99f8a8ef91b3af6ef945b1e1cb449bfbabf63b54467a8feb3b
7
- data.tar.gz: 06dd6ff3e6fd121728c7eee4a979fdad519c08e3820dea374d4ea0fcdc6ae581d659d4a8ce6f750b65d020be652eeadb3680ab46dc0050e58f434bc8a62be0fb
6
+ metadata.gz: 299fda2e34de05e4ce9501f6dcb62fc748563e639ba775465bb6f686048063b3dabd28070876b445a3071c0677c08a9d8b55600dff8c36bf2922ab317fecaa56
7
+ data.tar.gz: 2eb7dfde1026069de99d1069c6e1eb112d02bb6318396b70b27ce215ceae451104c36f7b0b71224d99aca449de9f5f279f67333909264392767a7be80e9e3dd9
@@ -1,13 +1,14 @@
1
1
  # parse web's command line args
2
2
 
3
3
 
4
- VERSION = "0.3.2"
4
+ VERSION = "0.4"
5
5
 
6
6
 
7
7
  require 'yaml'
8
8
  require 'find'
9
9
  require 'json'
10
10
  require 'terminfo'
11
+ require 'shellwords'
11
12
  require_relative 'bookmarks'
12
13
  require_relative 'config'
13
14
  require_relative 'consts'
@@ -29,6 +30,10 @@ class Booker
29
30
  pexit HELP_BANNER, 0
30
31
  end
31
32
 
33
+ def openweb(url)
34
+ system(browse + wrap(url))
35
+ end
36
+
32
37
  def parse(args)
33
38
  # no args given, show help
34
39
  helper if args.none?
@@ -36,26 +41,23 @@ class Booker
36
41
  # if arg starts with hyphen, parse option
37
42
  parse_opt args if /^-.*/.match(args[0])
38
43
 
39
- # interpret
44
+ # interpret command
40
45
  browsearg = args[0]
41
46
 
42
47
  if browsearg.match(/^[0-9]/) # bookmark
43
48
  bm = Bookmarks.new('')
44
49
  url = bm.bookmark_url(browsearg)
45
50
  pexit "Failure:".red + " bookmark #{browsearg} not found", 1 if url.nil?
46
- puts 'opening ' + url + '...'
47
- system browse << wrap(url)
48
-
51
+ puts 'opening bookmark ' + url + '...'
52
+ openweb(wrap(url))
49
53
  elsif domain.match(browsearg) # website
50
- puts 'opening ' + browsearg + '...'
51
- system browse << wrap(prep(browsearg))
52
-
54
+ puts 'opening website ' + browsearg + '...'
55
+ openweb(wrap(prep(browsearg)))
53
56
  else
54
57
  allargs = wrap(args.join(' '))
55
58
  puts 'searching ' + allargs + '...'
56
59
  search = BConfig.new.searcher
57
- system browse << wrap(search + allargs)
58
-
60
+ openweb(Shellwords.escape(search + allargs))
59
61
  end
60
62
  end
61
63
 
@@ -68,13 +70,13 @@ class Booker
68
70
  pexit errormsg, 1 if ! (valid_opts.include? nextarg)
69
71
 
70
72
  # doing forced bookmarking
71
- if args[0] == "--bookmark" or args[0] == "-b"
73
+ if args[0] == "--bookmark" || args[0] == "-b"
72
74
  bm = Bookmarks.new('')
73
75
  id = args[1]
74
76
  if id
75
77
  url = bm.bookmark_url(id)
76
78
  puts 'opening ' + url + '...'
77
- system browse << wrap(url)
79
+ system(browse + wrap(url))
78
80
  exit 0
79
81
  else
80
82
  pexit 'Error: '.red +
@@ -83,7 +85,7 @@ class Booker
83
85
  end
84
86
 
85
87
  # doing autocompletion
86
- if args[0] == "--complete" or args[0] == "-c"
88
+ if args[0] == "--complete" || args[0] == "-c"
87
89
  args.shift # remove flag
88
90
  allargs = args.join(' ')
89
91
  bm = Bookmarks.new(allargs)
@@ -92,7 +94,7 @@ class Booker
92
94
  end
93
95
 
94
96
  # doing installation
95
- if args[0] == "--install" or args[0] == "-i"
97
+ if args[0] == "--install" || args[0] == "-i"
96
98
  args.shift # remove flag
97
99
  if args.length > 0
98
100
  install(args)
@@ -103,12 +105,12 @@ class Booker
103
105
  end
104
106
 
105
107
  # needs some help
106
- if args[0] == "--help" or args[0] == "-h"
108
+ if args[0] == "--help" || args[0] == "-h"
107
109
  helper
108
110
  end
109
111
 
110
112
  # doing forced searching
111
- if args[0] == "--search" or args[0] == "-s"
113
+ if args[0] == "--search" || args[0] == "-s"
112
114
  args.shift # remove flag
113
115
  allargs = args.join(' ')
114
116
  if allargs == ""
@@ -116,13 +118,13 @@ class Booker
116
118
  else
117
119
  puts 'searching ' + allargs + '...'
118
120
  search = BConfig.new.searcher
119
- system browse << search << allargs
121
+ openweb(Shellwords.escape(search + allargs))
120
122
  exit 0
121
123
  end
122
124
  end
123
125
 
124
126
  # print version information
125
- if args[0] == "--version" or args[0] == "-v"
127
+ if args[0] == "--version" || args[0] == "-v"
126
128
  pexit VERSION, 0
127
129
  end
128
130
  end # parse opt
@@ -172,18 +174,24 @@ class Booker
172
174
  # locate bookmarks file, show user, write to config?
173
175
  puts 'searching for chrome bookmarks... (takes some time)'
174
176
  begin
175
- bms = []
177
+ bms = [] # look for bookmarks
176
178
  Find.find(ENV["HOME"]) do |path|
177
179
  bms << path if /chrom.*bookmarks/i.match path
178
180
  end
179
- puts 'select your bookmarks file: '
180
- bms.each_with_index{|bm, i| puts i.to_s.grn + " - " + bm }
181
- selected = bms[gets.chomp.to_i]
182
- puts 'Selected: '.yel + selected
183
- BConfig.new.write('bookmarks', selected)
184
- puts "Success: ".grn +
185
- "config file updated with your bookmarks"
186
- rescue
181
+
182
+ if bms.empty? # no bookmarks found
183
+ puts "Failure: ".red + 'bookmarks file could not be found.'
184
+ raise
185
+ else # have user select a file
186
+ puts 'select your bookmarks file: '
187
+ bms.each_with_index{|bm, i| puts i.to_s.grn + " - " + bm }
188
+ selected = bms[gets.chomp.to_i]
189
+ puts 'Selected: '.yel + selected
190
+ BConfig.new.write('bookmarks', selected)
191
+ puts "Success: ".grn +
192
+ "config file updated with your bookmarks"
193
+ end
194
+ rescue # catch all errors
187
195
  pexit "Failure: ".red +
188
196
  "could not add bookmarks to config file ~/.booker", 1
189
197
  end
@@ -31,8 +31,8 @@ class String
31
31
  end
32
32
 
33
33
 
34
+ # try to read bookmarks
34
35
  class Bookmarks
35
- # try to read bookmarks
36
36
  def initialize(search_term)
37
37
  @conf = BConfig.new
38
38
  begin
@@ -120,28 +120,24 @@ end # close bookmarks class
120
120
  # for recursively parsing bookmarks
121
121
  class Folder
122
122
  include Enumerable
123
+ attr_reader :json, :title
123
124
  def initialize(title='|', json)
124
125
  @title = title.gsub(/[:,'"]/, '-').downcase
125
126
  @json = json
126
127
  end
127
128
 
128
- def json() @json end
129
- def title() @title end
129
+ # needed for Enumerable
130
130
  def each() @json.each end
131
131
  end
132
132
 
133
133
 
134
134
  # clean bookmark title, set attrs
135
135
  class Bookmark
136
+ attr_reader :title, :folder, :url, :id
136
137
  def initialize(f, t, u, id)
137
138
  @title = t.gsub(/[:'"+]/, ' ').downcase
138
139
  @folder = f
139
140
  @url = u
140
141
  @id = id
141
142
  end
142
-
143
- def folder() @folder end
144
- def title() @title end
145
- def url() @url end
146
- def id() @id end
147
143
  end
@@ -22,7 +22,7 @@ module Browser
22
22
  extend OS
23
23
  def browse
24
24
  if OS.windows?
25
- # alternatively, start seems to work
25
+ # alternatively, start seems to work - probably check if powershell v cygwin?
26
26
  '/cygdrive/c/Program\ Files\ \(x86\)/Google/Chrome/Application/chrome.exe '
27
27
  elsif OS.mac?
28
28
  'open -a "Google Chrome" '
@@ -40,17 +40,18 @@ module Browser
40
40
  if /^http/.match(url)
41
41
  url
42
42
  else
43
- 'http://' << url
43
+ 'http://' + url
44
44
  end
45
45
  end
46
46
 
47
47
  def wrap(url)
48
- "'" + url + "'"
48
+ #"'" + url + "'"
49
+ url
49
50
  end
50
51
  end
51
52
 
52
53
 
53
- # high level configuration
54
+ # configuration
54
55
  class BConfig
55
56
  VALID = [:searcher, :bookmarks, :browser]
56
57
  HOME = ENV['HOME'].nil?? '/usr/local/' : ENV['HOME']
@@ -84,8 +85,7 @@ class BConfig
84
85
  rescue Errno::ENOENT
85
86
  puts "Warning: ".yel +
86
87
  "YAML configuration file couldn't be found. Using defaults."
87
- puts "Suggest: ".grn +
88
- "web --install config"
88
+ puts "Suggest: ".grn + "web --install config"
89
89
  return false
90
90
  rescue Psych::SyntaxError
91
91
  puts "Warning: ".red +
@@ -95,11 +95,12 @@ class BConfig
95
95
  config
96
96
  end
97
97
 
98
+ # used for creating and updating the default configuration
98
99
  def write(k=nil, v=nil)
99
- if k.nil? || v.nil?
100
+ if k.nil? && v.nil?
100
101
  File.open(YAMLCONF, 'w') {|f| f.write(@config.to_yaml) }
101
102
  else
102
- @config[k] = v
103
+ @config[k] = v # update users yaml config file
103
104
  File.open(YAMLCONF, 'w+') {|f| f.write(@config.to_yaml) }
104
105
  end
105
106
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: booker
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.2
4
+ version: '0.4'
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jeremy Warner
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-11-17 00:00:00.000000000 Z
11
+ date: 2015-11-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: ruby-terminfo