stackoverflow 0.1.4 → 0.1.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/stackoverflow.rb +29 -32
  2. metadata +2 -2
@@ -1,5 +1,3 @@
1
- #!/usr/bin/env ruby
2
- #
3
1
  # Copyright (C) 2012 Xavier Antoviaque <xavier@antoviaque.org>
4
2
  #
5
3
  # This software's license gives you freedom; you can copy, convey,
@@ -56,8 +54,8 @@ module StackOverflow
56
54
 
57
55
  class DB
58
56
  def initialize
59
- @db = SQLite3::Database.new(File.join(Dir.home, ".stackoverflow/stackoverflow.db"))
60
- @db_idx = SQLite3::Database.new(File.join(Dir.home, ".stackoverflow/stackoverflow_idx.db"))
57
+ @db = SQLite3::Database.new(File.join(Dir.home, ".stackoverflow/stackoverflow.db"))
58
+ @db_idx = SQLite3::Database.new(File.join(Dir.home, ".stackoverflow/stackoverflow_idx.db"))
61
59
  end
62
60
 
63
61
  def db_error_catching
@@ -300,28 +298,25 @@ module StackOverflow
300
298
  def question_viewer(question)
301
299
  answers = question['answers']
302
300
  nb = 1
303
-
304
- table = Terminal::Table.new do |t|
305
- t << ["Question", html2text(question['body'])]
306
- t.title = question['title'] + "\n\n" + question['link']
307
- t << :separator
308
- t << :separator
309
-
310
- answers.each do |answer|
311
- text = html2text(answer['body'])
312
- t << ["[#{nb}] (+#{answer['score']})", "#{text}"]
313
- t << :separator
314
- nb += 1
315
- end
301
+
302
+ man = ".TH STACKOVERFLOW \"1\" \"\" \"Stack Overflow\" \"#{question['title']}\"\n"
303
+ man += ".SH QUESTION\n#{html2text(question['body'])}\n"
304
+
305
+ answers.each do |answer|
306
+ text = html2text(answer['body'])
307
+ man += ".SH ANSWER [#{nb}] (+#{answer['score']})\n"
308
+ man += "#{text}\n"
309
+ nb += 1
316
310
  end
317
311
 
318
- IO.popen("less", "w") { |f| f.puts table }
312
+ tmp_file_path = "/tmp/.stack_overflow.#{question['id']}"
313
+ File.open(tmp_file_path, 'w+') { |f| f.write(man) }
314
+ system "man #{tmp_file_path}"
319
315
  end
320
316
 
321
317
  def html2text(html)
322
318
  doc = Nokogiri::HTML(html)
323
- doc = doc.css('body').text.squeeze(" ").squeeze("\n").gsub(/[\n]+/, "\n\n")
324
- wordwrap(doc)
319
+ doc.css('body').text.squeeze(" ").squeeze("\n").gsub(/[\n]+/, "\n\n")
325
320
  end
326
321
 
327
322
  def wordwrap(str, columns=80)
@@ -333,35 +328,37 @@ module StackOverflow
333
328
 
334
329
  class Command
335
330
  def run
336
- options = {}
331
+ options = {:run => true}
337
332
  OptionParser.new do |opts|
338
333
  opts.banner = "** Usage: so [options] <search string> [<question_id>]"
339
334
  opts.on("-h", "--help", "Help") do |v|
340
335
  help
341
- exit
336
+ options[:run] = false
342
337
  end
343
338
  opts.on("-u", "--update", "Update local database") do |v|
344
339
  DBUpdater.new.update
345
- exit
340
+ options[:run] = false
346
341
  end
347
342
  opts.on("-o", "--offline", "Offline mode") do |v|
348
- options['offline'] = true
343
+ options[:offline] = true
349
344
  end
350
345
  end.parse!
351
346
 
352
347
  if ARGV.length < 1
353
348
  help
354
- exit
349
+ options[:run] = false
355
350
  end
356
351
 
357
- # last argument is integer when user is specifing a question_nb from the results
358
- question_nb = nil
359
- if ARGV[-1] =~ /^[0-9]+$/
360
- question_nb = ARGV.pop.to_i
361
- end
352
+ if options[:run]
353
+ # last argument is integer when user is specifing a question_nb from the results
354
+ question_nb = nil
355
+ if ARGV[-1] =~ /^[0-9]+$/
356
+ question_nb = ARGV.pop.to_i
357
+ end
362
358
 
363
- search_string = ARGV.join(' ')
364
- search(search_string, question_nb, options)
359
+ search_string = ARGV.join(' ')
360
+ search(search_string, question_nb, options)
361
+ end
365
362
  end
366
363
 
367
364
  def help
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 1
8
- - 4
9
- version: 0.1.4
8
+ - 5
9
+ version: 0.1.5
10
10
  platform: ruby
11
11
  authors:
12
12
  - Xavier Antoviaque