lib/bbiff/executable.rb in bbiff-0.3.5 vs lib/bbiff/executable.rb in bbiff-0.4.0

- old
+ new

@@ -17,11 +17,11 @@ raise_if_closed! clear if str[-1] == "\n" if str.rindex("\n") != str.size-1 || str.index("\n") < str.rindex("\n") - raise 'multiline' + raise 'multiline' end @out.print str @width = 0 else @@ -97,10 +97,15 @@ STDERR.puts "Warning: 以下の場所から掲示板の設定が取得できませんでした。(#{e.response.message})" STDERR.puts board.settings_url return {'BBS_TITLE'=>'<不明>'} end + def show(title, post) + notify_send = ENV['BBIFF_NOTIFY_SEND'] || (`which notify-send` != "" ? 'notify-send' : 'echo') + system("#{notify_send} #{Shellwords.escape(title)} #{Shellwords.escape(render_post(post))}") + end + RETRY_INTERVAL_SECONDS = 3 def start_polling(thread, start_no) out = LineIndicator.new begin @@ -112,29 +117,32 @@ puts " #{@settings.current['thread_url']}" loop do out.set_line "#{thread.title}(#{thread.last}) 新着レス確認中" - thread.posts(parse_range("#{start_no}-")).each do |post| + posts = thread.posts(parse_range("#{start_no}-"), + { long_polling: @settings.current['long_polling'] }) + t = Time.now + posts.each do |post| out.puts "-----" - puts render_post(post) + unless @settings.current['no_render'] + puts render_post(post) + end - system(@settings.current['bbiff_show'], - thread.title, post.to_s) - - sleep 1 + show(thread.title, post) end start_no = thread.last + 1 if start_no > thread_stop out.puts "スレッドストップ" - break + break end - delay.times do |i| + d = [delay-(Time.now-t), 0].max.round + d.times do |i| j = i + 1 - out.set_line "#{thread.title}(#{thread.last}) 待機中 [#{'.'*j}#{' '*(delay - j)}]" + out.set_line "#{thread.title}(#{thread.last}) 待機中 [#{'.'*j}#{' '*(d - j)}]" sleep 1 end end ensure out.close @@ -145,34 +153,62 @@ STDERR.print "Error: " STDERR.puts e.message STDERR.puts e.backtrace if $DEBUG STDERR.puts "#{RETRY_INTERVAL_SECONDS}秒後にリトライ" sleep RETRY_INTERVAL_SECONDS + thread = Bbs::create_thread(@settings.current['thread_url']) start_polling(thread, start_no) end def usage - STDERR.puts "Usage: bbiff [http://jbbs.shitaraba.net/bbs/read.cgi/CATEGORY/BOARD_ID/THREAD_ID/] [START_NUMBER]" - + STDERR.puts "Usage: bbiff [OPTIONS] [http://jbbs.shitaraba.net/bbs/read.cgi/CATEGORY/BOARD_ID/THREAD_ID/] [START_NUMBER]" + STDERR.puts <<"EOD" Bbiff version #{Bbiff::VERSION} Copyright © 2016-2019 Yoteichi + + -h, --help + --no-render + --debug + --long-polling (for Genkai) + --delay-seconds=N EOD end def main - if ARGV.include?('-h') || ARGV.include?('--help') - raise UsageError + args = [] + ARGV.each do |arg| + case arg + when '-h', '--help' + raise UsageError + when '--no-render' + @settings.current['no_render'] = true + when '--debug' + $DEBUG = true + when '--long-polling' + @settings.current['long_polling'] = true + when /\A--delay-seconds=(.+)\z/ + s = $1 + if s =~ /\A\d+\z/ + @settings.current['delay_seconds'] = s.to_i + else + STDERR.puts "delay-seconds must be a non-negative integer" + raise UsageError + end + when /\A-/ + STDERR.puts "invalid option #{arg.inspect}" + raise UsageError + else + args << arg + end end - if ARGV.size < 1 && !@settings.current['thread_url'] + if args.size < 1 raise UsageError - elsif ARGV.size < 1 - url = @settings.current['thread_url'] else - url = ARGV[0] + url = args[0] end begin thread = Bbs::create_thread(url) @settings.current['thread_url'] = url @@ -186,16 +222,14 @@ if thread.nil? STDERR.puts "スレッドのURLとして解釈できませんでした。(#{url})" exit 1 end - start_no = ARGV[1] ? ARGV[1].to_i : thread.last + 1 + start_no = args[1] ? args[1].to_i : thread.last + 1 start_polling(thread, start_no) rescue UsageError usage exit 1 - ensure - @settings.save end end end