booker 0.4 → 1.0.0

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: ede393f061fc758e36d7ab8a09d414dbee51559c
4
- data.tar.gz: 3390c07e38358019217b79fa4d73560a0e7941de
2
+ SHA256:
3
+ metadata.gz: 1e80629e0fd1558e7f1a34c2e9536fef061f80d7bc6994c9568499967f909848
4
+ data.tar.gz: a6434b1920cec9dd572e681fc4ab574672dcd91d601b115668d16b766509d2b0
5
5
  SHA512:
6
- metadata.gz: 299fda2e34de05e4ce9501f6dcb62fc748563e639ba775465bb6f686048063b3dabd28070876b445a3071c0677c08a9d8b55600dff8c36bf2922ab317fecaa56
7
- data.tar.gz: 2eb7dfde1026069de99d1069c6e1eb112d02bb6318396b70b27ce215ceae451104c36f7b0b71224d99aca449de9f5f279f67333909264392767a7be80e9e3dd9
6
+ metadata.gz: 3e6ec73b48945713ec46e63cdfba5d116fb21e82894b714bdca17eccd52e11ce3e4c1303c1c227fffcec677bc12431f84bdd364ee0edcaf0ae0bdb8ca6cf5613
7
+ data.tar.gz: 1dab0b803e4e5e11c2c76cd0ef9eaf76e9054e5cc1a7cd100121b4003f315d2db8732e2852c790f724093f4e0fe107426aff01bce19fc3674d1ce71f2ab0b349
data/bin/{web → booker} RENAMED
@@ -1,12 +1,9 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
-
4
3
  require_relative "../lib/booker"
5
4
 
6
-
7
- # web by jeremy warner
5
+ # booker by jeremy warner
8
6
  # open up a website or search in google chrome from cli
9
7
  # also show and/or open up google chrome bookmarks
10
8
 
11
-
12
9
  Booker.new(ARGV)
data/lib/booker.rb CHANGED
@@ -1,26 +1,45 @@
1
- # parse web's command line args
2
-
3
-
4
- VERSION = "0.4"
5
-
6
-
1
+ # parse booker's command line args
7
2
  require 'yaml'
8
3
  require 'find'
9
4
  require 'json'
10
- require 'terminfo'
11
5
  require 'shellwords'
12
6
  require_relative 'bookmarks'
13
7
  require_relative 'config'
14
8
  require_relative 'consts'
15
9
 
16
10
 
17
- # get web opening command
11
+ # get booker opening command
18
12
  class Booker
13
+ @version = "1.0.0"
14
+ class << self
15
+ attr_reader :version
16
+ end
17
+
19
18
  include Browser
20
19
  def initialize(args)
21
20
  parse args
22
21
  end
23
22
 
23
+ def parse(args)
24
+ # no args given, show help
25
+ helper if args.none?
26
+
27
+ # if arg starts with hyphen, parse option
28
+ parse_opt args if /^-.*/.match(args.first)
29
+
30
+ # interpret command
31
+ browsearg = args.first
32
+
33
+ if browsearg.match(/^[0-9]/) # bookmark
34
+ open_bookmark args
35
+ elsif domain.match(browsearg) # website
36
+ puts 'opening website: ' + browsearg
37
+ openweb(prep(browsearg))
38
+ else
39
+ open_search(args.join(' '))
40
+ end
41
+ end
42
+
24
43
  def pexit(msg, sig)
25
44
  puts msg
26
45
  exit sig
@@ -30,104 +49,79 @@ class Booker
30
49
  pexit HELP_BANNER, 0
31
50
  end
32
51
 
52
+ def version
53
+ pexit @version, 0
54
+ end
55
+
33
56
  def openweb(url)
34
57
  system(browse + wrap(url))
35
58
  end
36
59
 
37
- def parse(args)
38
- # no args given, show help
39
- helper if args.none?
40
-
41
- # if arg starts with hyphen, parse option
42
- parse_opt args if /^-.*/.match(args[0])
43
-
44
- # interpret command
45
- browsearg = args[0]
60
+ # an array of ints, as bookmark ids
61
+ def open_bookmark(bm)
62
+ id = bm.shift
63
+ url = Bookmarks.new.bookmark_url(id)
64
+ pexit "Failure:".red + " bookmark #{id} not found", 1 if url.nil?
65
+ puts 'opening bookmark ' + url + '...'
66
+ openweb(wrap(url))
67
+ open_bookmark bm unless bm.empty?
68
+ end
46
69
 
47
- if browsearg.match(/^[0-9]/) # bookmark
48
- bm = Bookmarks.new('')
49
- url = bm.bookmark_url(browsearg)
50
- pexit "Failure:".red + " bookmark #{browsearg} not found", 1 if url.nil?
51
- puts 'opening bookmark ' + url + '...'
52
- openweb(wrap(url))
53
- elsif domain.match(browsearg) # website
54
- puts 'opening website ' + browsearg + '...'
55
- openweb(wrap(prep(browsearg)))
56
- else
57
- allargs = wrap(args.join(' '))
58
- puts 'searching ' + allargs + '...'
59
- search = BConfig.new.searcher
60
- openweb(Shellwords.escape(search + allargs))
61
- end
70
+ def open_search(term)
71
+ puts 'searching ' + term + '...'
72
+ search = BConfig.new.searcher
73
+ openweb(search + Shellwords.escape(term))
62
74
  end
63
75
 
76
+ # parse and execute any command line options
64
77
  def parse_opt(args)
65
78
  valid_opts = %w{--version -v --install -i --help -h
66
79
  --complete -c --bookmark -b --search -s}
67
80
 
68
- nextarg = args[0]
69
- errormsg = "Error: ".red + "unrecognized option #{nextarg}"
81
+ nextarg = args.shift
82
+ errormsg = 'Error: '.red + "unrecognized option #{nextarg}"
70
83
  pexit errormsg, 1 if ! (valid_opts.include? nextarg)
71
84
 
72
- # doing forced bookmarking
73
- if args[0] == "--bookmark" || args[0] == "-b"
74
- bm = Bookmarks.new('')
75
- id = args[1]
76
- if id
77
- url = bm.bookmark_url(id)
78
- puts 'opening ' + url + '...'
79
- system(browse + wrap(url))
80
- exit 0
85
+ # forced bookmarking
86
+ if nextarg == '--bookmark' || nextarg == '-b'
87
+ if args.first.nil?
88
+ pexit 'Error: '.red + 'booker --bookmark expects bookmark id', 1
81
89
  else
82
- pexit 'Error: '.red +
83
- 'web --bookmark expects bookmark id', 1
90
+ open_bookmark args
84
91
  end
85
92
  end
86
93
 
87
- # doing autocompletion
88
- if args[0] == "--complete" || args[0] == "-c"
89
- args.shift # remove flag
94
+ # autocompletion
95
+ if nextarg == '--complete' || nextarg == '-c'
90
96
  allargs = args.join(' ')
91
97
  bm = Bookmarks.new(allargs)
92
98
  bm.autocomplete
93
- exit 0
94
99
  end
95
100
 
96
- # doing installation
97
- if args[0] == "--install" || args[0] == "-i"
98
- args.shift # remove flag
99
- if args.length > 0
101
+ # installation
102
+ if nextarg == '--install' || nextarg == '-i'
103
+ if !args.empty?
100
104
  install(args)
101
- else
102
- pexit 'Error: '.red +
103
- "web --install expects arguments: [completion, bookmarks, config]", 1
105
+ else # do everything
106
+ install(%w{completion config bookmarks})
104
107
  end
105
108
  end
106
109
 
107
- # needs some help
108
- if args[0] == "--help" || args[0] == "-h"
109
- helper
110
- end
111
-
112
- # doing forced searching
113
- if args[0] == "--search" || args[0] == "-s"
114
- args.shift # remove flag
110
+ # forced searching
111
+ if nextarg == '--search' || nextarg == '-s'
112
+ pexit '--search requires an argument', 1 if args.empty?
115
113
  allargs = args.join(' ')
116
- if allargs == ""
117
- pexit "--search requires an argument", 1
118
- else
119
- puts 'searching ' + allargs + '...'
120
- search = BConfig.new.searcher
121
- openweb(Shellwords.escape(search + allargs))
122
- exit 0
123
- end
114
+ open_search allargs
124
115
  end
125
116
 
126
117
  # print version information
127
- if args[0] == "--version" || args[0] == "-v"
128
- pexit VERSION, 0
129
- end
130
- end # parse opt
118
+ version if nextarg == '--version' || nextarg == '-v'
119
+
120
+ # needs some help
121
+ helper if nextarg == '--help' || nextarg == '-h'
122
+
123
+ exit 0 # dont parse_arg
124
+ end # parse_opt
131
125
 
132
126
  def install(args)
133
127
  target = args.shift
@@ -151,60 +145,62 @@ class Booker
151
145
  begin
152
146
  fpath = `zsh -c 'echo $fpath'`.split(' ')
153
147
  rescue
154
- pexit "Failure: ".red +
155
- "zsh is probably not installed, could not find $fpath", 1
148
+ pexit "Failure: ".red + "zsh is probably not installed, could not find $fpath", 1
156
149
  end
157
150
 
158
151
  # determine where to install completion function
159
152
  fpath.each do |fp|
160
153
  begin
161
- File.open(fp + "/_web", 'w') {|f| f.write(COMPLETION) }
162
- system "zsh -c 'autoload -U _web'"
163
- puts "Success: ".grn +
164
- "installed zsh autocompletion in #{fp}"
154
+ File.open(fp + "/_booker", 'w') {|f| f.write(COMPLETION) }
155
+ system "zsh -c 'autoload -U _booker'"
156
+ puts "Success: ".grn + "installed zsh autocompletion in #{fp}"
165
157
  break # if this works, don't try anymore
166
158
  rescue
167
- puts "Failure: ".red +
168
- "could not write ZSH completion _web script to $fpath (#{fp})"
159
+ puts "Failure: ".red + "could not write ZSH completion _booker script to $fpath (#{fp})"
169
160
  end
170
161
  end
171
162
  end
172
163
 
173
164
  def install_bookmarks
174
165
  # locate bookmarks file, show user, write to config?
175
- puts 'searching for chrome bookmarks... (takes some time)'
166
+ puts 'searching for chrome bookmarks...'
176
167
  begin
177
168
  bms = [] # look for bookmarks
178
- Find.find(ENV["HOME"]) do |path|
179
- bms << path if /chrom.*bookmarks/i.match path
169
+ [ '/Library/Application Support/Google/Chrome',
170
+ '/AppData/Local/Google/Chrome/User Data/Default',
171
+ '/.config/chromium/Default/',
172
+ '/.config/google-chrome/Default/',
173
+ ].each do |f|
174
+ home = File.join(ENV['HOME'], f)
175
+ next if !FileTest.directory?(home)
176
+ Find.find(home) do |file|
177
+ bms << file if /chrom.*bookmarks/i.match file
178
+ end
180
179
  end
181
180
 
182
181
  if bms.empty? # no bookmarks found
183
182
  puts "Failure: ".red + 'bookmarks file could not be found.'
184
183
  raise
185
184
  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 }
185
+ puts 'select bookmarks file: '
186
+ bms.each_with_index {|bm, i| puts i.to_s.grn + " - " + bm }
188
187
  selected = bms[gets.chomp.to_i]
189
188
  puts 'Selected: '.yel + selected
190
- BConfig.new.write('bookmarks', selected)
191
- puts "Success: ".grn +
192
- "config file updated with your bookmarks"
189
+ BConfig.new.write(:bookmarks, selected)
190
+ puts "Success: ".grn + "config file updated with your bookmarks"
193
191
  end
194
- rescue # catch all errors
195
- pexit "Failure: ".red +
196
- "could not add bookmarks to config file ~/.booker", 1
192
+ rescue StandardError => e
193
+ puts e.message
194
+ pexit "Failure: ".red + "could not add bookmarks to config file ~/.booker", 1
197
195
  end
198
196
  end
199
197
 
200
198
  def install_config
201
199
  begin
202
200
  BConfig.new.write
203
- puts "Success: ".grn +
204
- "example config file written to ~/.booker"
201
+ puts "Success: ".grn + "example config file written to ~/.booker"
205
202
  rescue
206
- pexit "Failure: ".red +
207
- "could not write example config file to ~/.booker", 1
203
+ pexit "Failure: ".red + "could not write example config file to ~/.booker", 1
208
204
  end
209
205
  end
210
206
  end
data/lib/bookmarks.rb CHANGED
@@ -1,8 +1,11 @@
1
1
  # grab/parse bookmarks from json file on computer
2
2
 
3
-
4
3
  # get int number of columns in half of screen
5
- TERMWIDTH = (TermInfo.screen_size[1]/2).floor
4
+ def terminal_width
5
+ guess = `tput cols`.to_i
6
+ guess == 0 ? 80 : guess
7
+ end
8
+ TERMWIDTH = terminal_width
6
9
 
7
10
  # compl. color codes space
8
11
  CODEWIDTH = 16
@@ -18,7 +21,6 @@ class String
18
21
  end
19
22
  end
20
23
 
21
- # thx danhassin
22
24
  def colorize(color, mod)
23
25
  "\033[#{mod};#{color};49m#{self}\033[0;0m"
24
26
  end
@@ -33,7 +35,7 @@ end
33
35
 
34
36
  # try to read bookmarks
35
37
  class Bookmarks
36
- def initialize(search_term)
38
+ def initialize(search_term = '')
37
39
  @conf = BConfig.new
38
40
  begin
39
41
  local_bookmarks = JSON.parse(open(@conf.bookmarks).read)
@@ -42,7 +44,7 @@ class Bookmarks
42
44
  puts "Warning: ".yel +
43
45
  "Chrome JSON Bookmarks not found."
44
46
  puts "Suggest: ".grn +
45
- "web --install bookmarks"
47
+ "booker --install bookmarks"
46
48
  @chrome_bookmarks = {}
47
49
  end
48
50
 
data/lib/config.rb CHANGED
@@ -25,14 +25,14 @@ module Browser
25
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
- 'open -a "Google Chrome" '
28
+ 'open '
29
29
  elsif OS.linux?
30
- 'xdg-open ' # completely guessing here
30
+ 'xdg-open '
31
31
  end
32
32
  end
33
33
 
34
34
  def domain
35
- /.*(io|com|web|net|org|gov|edu)(\/.*)?$/i
35
+ /.*(io|com|web|net|org|gov|edu|xyz)(\/.*)?$/i
36
36
  end
37
37
 
38
38
  # helper methods
@@ -45,8 +45,7 @@ module Browser
45
45
  end
46
46
 
47
47
  def wrap(url)
48
- #"'" + url + "'"
49
- url
48
+ "\"#{url}\""
50
49
  end
51
50
  end
52
51
 
@@ -62,7 +61,7 @@ class BConfig
62
61
  readyaml = read(YAMLCONF)
63
62
  default_config = {
64
63
  :browser => 'open ',
65
- :searcher => "https://duckduckgo.com/?q=",
64
+ :searcher => "https://google.com/?q=",
66
65
  :bookmarks => HOME +
67
66
  "/Library/Application Support/Google/Chrome/Profile 1/Bookmarks",
68
67
  }
@@ -85,7 +84,7 @@ class BConfig
85
84
  rescue Errno::ENOENT
86
85
  puts "Warning: ".yel +
87
86
  "YAML configuration file couldn't be found. Using defaults."
88
- puts "Suggest: ".grn + "web --install config"
87
+ puts "Suggest: ".grn + "booker --install config"
89
88
  return false
90
89
  rescue Psych::SyntaxError
91
90
  puts "Warning: ".red +
data/lib/consts.rb CHANGED
@@ -1,9 +1,10 @@
1
1
  # file for large constant strings
2
2
 
3
+ # todo - wrap in module
3
4
 
4
5
  HELP_BANNER = <<-EOS
5
6
  Open browser:
6
- $ web [option] [arguments]
7
+ $ booker [option] [arguments]
7
8
 
8
9
  Main options:
9
10
  --bookmark, -b: explicity open bookmark
@@ -18,17 +19,18 @@ EOS
18
19
 
19
20
 
20
21
  DEF_CONFIG = <<-EOS
21
- bookmarks: null
22
- searcher: https://duckduckgo.com/?q=
22
+ ---
23
+ :searcher: https://google.com/?q=
24
+ :bookmarks: null
23
25
  EOS
24
26
 
25
27
 
26
28
  COMPLETION = <<-EOS
27
- #compdef web
29
+ #compdef booker
28
30
  #autoload
29
31
 
30
32
 
31
- _web_bookmarks() {
33
+ _booker_bookmarks() {
32
34
  #http://zsh.sourceforge.net/Guide/zshguide06.html#l147
33
35
  setopt glob bareglobqual nullglob rcexpandparam extendedglob automenu
34
36
  unsetopt allexport aliases errexit octalzeroes ksharrays cshnullglob
@@ -36,9 +38,9 @@ _web_bookmarks() {
36
38
  #get chrome bookmarks
37
39
  local -a bookmarks sites search
38
40
 
39
- #grab all CLI args, remove 'web' from start
40
- search=`echo $@ | sed -e '/^web /d'`
41
- sites=`web --complete $search`
41
+ #grab all CLI args, remove 'booker' from start
42
+ search=`echo $@ | sed -e '/^booker /d'`
43
+ sites=`booker --complete $search`
42
44
  bookmarks=("${(f)${sites}}")
43
45
 
44
46
  #terminal colors
@@ -60,7 +62,7 @@ _web_bookmarks() {
60
62
  }
61
63
 
62
64
 
63
- _web() {
65
+ _booker() {
64
66
  compstate[insert]=menu
65
67
  local curcontext state line
66
68
  typeset -A opt_args
@@ -73,29 +75,29 @@ _web() {
73
75
  '(--install)--install[perform installations (bookmarks, completion, config)]'\
74
76
  '(-s)-s[search google for...]'\
75
77
  '(--search)--search[search google for...]'\
76
- '(-h)-h[show web help]'\
77
- '(--help)--help[show web help]'\
78
+ '(-h)-h[show booker help]'\
79
+ '(--help)--help[show booker help]'\
78
80
  '(-v)-v[show version]'\
79
81
  '(--version)--version[show version]'\
80
82
  '*::bookmarks:->bookmarks' && return 0
81
83
 
82
- _web_bookmarks $words
84
+ _booker_bookmarks $words
83
85
  }
84
86
 
85
87
 
86
- _web
88
+ _booker
87
89
  EOS
88
90
 
89
91
 
90
92
  ZSH_SCRIPT = <<-EOS
91
- # load zsh web completions
93
+ # load zsh booker completions
92
94
  install_loc=~/.oh-my-zsh/completions
93
- script=_web
95
+ script=_booker
94
96
 
95
97
  install:
96
- mkdir -vp $(install_loc)
97
- cp -v $(script) $(install_loc)/
98
+ mkdir -vp $(install_loc)
99
+ cp -v $(script) $(install_loc)/
98
100
 
99
101
  uninstall:
100
- rm -v $(install_loc)/$(script)
102
+ rm -v $(install_loc)/$(script)
101
103
  EOS
metadata CHANGED
@@ -1,88 +1,56 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: booker
3
3
  version: !ruby/object:Gem::Version
4
- version: '0.4'
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jeremy Warner
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-11-20 00:00:00.000000000 Z
11
+ date: 2021-06-24 00:00:00.000000000 Z
12
12
  dependencies:
13
- - !ruby/object:Gem::Dependency
14
- name: ruby-terminfo
15
- requirement: !ruby/object:Gem::Requirement
16
- requirements:
17
- - - "~>"
18
- - !ruby/object:Gem::Version
19
- version: '0.1'
20
- - - ">="
21
- - !ruby/object:Gem::Version
22
- version: 0.1.1
23
- type: :runtime
24
- prerelease: false
25
- version_requirements: !ruby/object:Gem::Requirement
26
- requirements:
27
- - - "~>"
28
- - !ruby/object:Gem::Version
29
- version: '0.1'
30
- - - ">="
31
- - !ruby/object:Gem::Version
32
- version: 0.1.1
33
13
  - !ruby/object:Gem::Dependency
34
14
  name: json
35
15
  requirement: !ruby/object:Gem::Requirement
36
16
  requirements:
37
- - - "~>"
38
- - !ruby/object:Gem::Version
39
- version: '1.8'
40
17
  - - ">="
41
18
  - !ruby/object:Gem::Version
42
- version: 1.8.1
19
+ version: '0'
43
20
  type: :runtime
44
21
  prerelease: false
45
22
  version_requirements: !ruby/object:Gem::Requirement
46
23
  requirements:
47
- - - "~>"
48
- - !ruby/object:Gem::Version
49
- version: '1.8'
50
24
  - - ">="
51
25
  - !ruby/object:Gem::Version
52
- version: 1.8.1
26
+ version: '0'
53
27
  - !ruby/object:Gem::Dependency
54
28
  name: rspec
55
29
  requirement: !ruby/object:Gem::Requirement
56
30
  requirements:
57
- - - "~>"
58
- - !ruby/object:Gem::Version
59
- version: '3.3'
60
31
  - - ">="
61
32
  - !ruby/object:Gem::Version
62
- version: 3.3.2
33
+ version: '0'
63
34
  type: :development
64
35
  prerelease: false
65
36
  version_requirements: !ruby/object:Gem::Requirement
66
37
  requirements:
67
- - - "~>"
68
- - !ruby/object:Gem::Version
69
- version: '3.3'
70
38
  - - ">="
71
39
  - !ruby/object:Gem::Version
72
- version: 3.3.2
40
+ version: '0'
73
41
  description: |2
74
42
  Select your bookmarks from the command line, by their bookmark id number.
75
43
  ZSH users will be able to tab complete through their matches, similar to
76
- how the autocompletion for `kill` works. If no bookmark is provided, web
77
- will check if the argument is a website and visit it, or search duckduckgo
78
- for the string argument.
44
+ how the autocompletion for `kill` works. If no bookmark is provided, booker
45
+ will check if the argument is a website and visit it, or go to google
46
+ (or your preferred search engine) with the string argument.
79
47
  email: jeremywrnr@gmail.com
80
48
  executables:
81
- - web
49
+ - booker
82
50
  extensions: []
83
51
  extra_rdoc_files: []
84
52
  files:
85
- - bin/web
53
+ - bin/booker
86
54
  - lib/booker.rb
87
55
  - lib/bookmarks.rb
88
56
  - lib/config.rb
@@ -91,11 +59,7 @@ homepage: http://github.com/jeremywrnr/booker
91
59
  licenses:
92
60
  - MIT
93
61
  metadata: {}
94
- post_install_message: |2
95
- Thank you for installing booker!
96
- To find your bookmarks, run `web -i bookmarks`
97
- To add zsh completion, run `web -i completion`
98
- To see more help, run `web --help`
62
+ post_install_message: 'To add zsh completion run: booker --install bookmarks'
99
63
  rdoc_options: []
100
64
  require_paths:
101
65
  - lib
@@ -110,9 +74,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
110
74
  - !ruby/object:Gem::Version
111
75
  version: '0'
112
76
  requirements: []
113
- rubyforge_project:
114
- rubygems_version: 2.4.8
115
- signing_key:
77
+ rubygems_version: 3.2.15
78
+ signing_key:
116
79
  specification_version: 4
117
80
  summary: CLI parser/selector for google chrome bookmarks
118
81
  test_files: []