Class: Booker

Inherits:
Object
  • Object
show all
Includes:
Browser
Defined in:
lib/booker.rb

Overview

get web opening command

Instance Method Summary collapse

Methods included from Browser

#browse, #domain, #prep, #wrap

Methods included from OS

linux?, mac?, windows?

Constructor Details

#initialize(args) ⇒ Booker



19
20
21
# File 'lib/booker.rb', line 19

def initialize(args)
  parse args
end

Instance Method Details

#helperObject



28
29
30
# File 'lib/booker.rb', line 28

def helper
  pexit HELP_BANNER, 0
end

#install(args) ⇒ Object



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/booker.rb', line 129

def install(args)
  target = args.shift
  exit 0 if target.nil?

  if /comp/i.match(target) # completion installation
    install_completion

  elsif /book/i.match(target) # bookmarks installation
    install_bookmarks

  elsif /conf/i.match(target) # default config file generation
    install_config

  else # unknown argument passed into install
    pexit "Failure: ".red + "unknown installation option (#{target})", 1
  end

  install(args) # recurse til done
end

#install_bookmarksObject



173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/booker.rb', line 173

def install_bookmarks
  # locate bookmarks file, show user, write to config?
  puts 'searching for chrome bookmarks... (takes some time)'
  begin
    bms = []
    Find.find(ENV["HOME"]) do |path|
      bms << path if /chrom.*bookmarks/i.match path
    end
    puts 'select your 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"
  rescue
    pexit "Failure: ".red +
      "could not add bookmarks to config file ~/.booker", 1
  end
end

#install_completionObject



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/booker.rb', line 149

def install_completion
  # check if zsh is even installed for this user
  begin
    fpath = `zsh -c 'echo $fpath'`.split(' ')
  rescue
    pexit "Failure: ".red +
      "zsh is probably not installed, could not find $fpath", 1
  end

  # determine where to install completion function
  fpath.each do |fp|
    begin
      File.open(fp + "/_web", 'w') {|f| f.write(COMPLETION) }
      system "zsh -c 'autoload -U _web'"
      puts "Success: ".grn +
        "installed zsh autocompletion in #{fp}"
      break # if this works, don't try anymore
    rescue
      puts "Failure: ".red +
        "could not write ZSH completion _web script to $fpath (#{fp})"
    end
  end
end

#install_configObject



194
195
196
197
198
199
200
201
202
203
# File 'lib/booker.rb', line 194

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

#parse(args) ⇒ Object



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
# File 'lib/booker.rb', line 32

def parse(args)
  # no args given, show help
  helper if args.none?

  # if arg starts with hyphen, parse option
  parse_opt args if /^-.*/.match(args[0])

  # interpret
  allargs = wrap(args.join(' '))
  browsearg = args.shift

  if /[0-9]/.match(browsearg[0]) # bookmark
    bm = Bookmarks.new('')
    url = bm.bookmark_url(browsearg)
    puts 'opening ' + url + '...'
    system browse, wrap(url)

  elsif domain.match(browsearg) # website
    puts 'opening ' + browsearg + '...'
    system browse, wrap(prep(browsearg))

  else # just search for these arguments
    puts 'searching ' + allargs + '...'
    search = BConfig.new.searcher
    system browse, search, allargs

  end
end

#parse_opt(args) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
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
125
126
127
# File 'lib/booker.rb', line 61

def parse_opt(args)
  valid_opts = %w{--version -v --install -i --help -h
  --complete -c --bookmark -b --search -s}

  nextarg = args[0]
  errormsg = "Error: ".red + "unrecognized option #{nextarg}"
  pexit errormsg, 1 if ! (valid_opts.include? nextarg)

  # doing forced bookmarking
  if args[0] == "--bookmark" or args[0] == "-b"
    bm = Bookmarks.new('')
    id = args[1]
    if id
      url = bm.bookmark_url(id)
      puts 'opening ' + url + '...'
      system browse << wrap(url)
      exit 0
    else
      pexit 'Error: '.red +
        'web --bookmark expects bookmark id', 1
    end
  end

  # doing autocompletion
  if args[0] == "--complete" or args[0] == "-c"
    args.shift # remove flag
    allargs = args.join(' ')
    bm = Bookmarks.new(allargs)
    bm.autocomplete
    exit 0
  end

  # doing installation
  if args[0] == "--install" or args[0] == "-i"
    args.shift # remove flag
    if args.length > 0
      install(args)
    else
      pexit 'Error: '.red +
        "web --install expects arguments: [completion, bookmarks, config]", 1
    end
  end

  # needs some help
  if args[0] == "--help" or args[0] == "-h"
    helper
  end

  # doing forced searching
  if args[0] == "--search" or args[0] == "-s"
    args.shift # remove flag
    allargs = args.join(' ')
    if allargs == ""
      pexit "--search requires an argument", 1
    else
      puts 'searching ' + allargs + '...'
      search = BConfig.new.searcher
      system browse << search << allargs
      exit 0
    end
  end

  # print version information
  if args[0] == "--version" or args[0] == "-v"
    pexit VERSION, 0
  end
end

#pexit(msg, sig) ⇒ Object



23
24
25
26
# File 'lib/booker.rb', line 23

def pexit(msg, sig)
  puts msg
  exit sig
end