in_our_time 0.6.0 → 0.7.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (5) hide show
  1. checksums.yaml +4 -4
  2. data/VERSION +1 -1
  3. data/lib/iot/iot.rb +130 -102
  4. data/lib/iot/keyboard_events.rb +13 -13
  5. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 5ebc3b3c85e4482305e7c6126b149228e277165a
4
- data.tar.gz: eff2a53fb96a38076a283e2c668e09c4d169f090
3
+ metadata.gz: 956e4e9773aad8f71897640c04d397bd566f512d
4
+ data.tar.gz: 39f0098a0bb42d7f4a16d04fb518f68bfb2fb61a
5
5
  SHA512:
6
- metadata.gz: 418b56bec73a51dae797340183fc2f7b7a9e7a98b7378e7ba82dcb6be441a887e90a6cfcced2a156725816348d4abba73d04ab5b7dec333ea9f7be32d18f4a91
7
- data.tar.gz: afd484a3f52597e77c3b2a0b731a86da0f1c89b05b96e534b6751e4f92c1078eca1717cb71acf527cb9076b1f1304203f5d14ddd840af220b40f65e9434635a0
6
+ metadata.gz: 62af2485c95313e901ac884a6eb111a8cc2d407aafa64a1feda55e27c48ac85c1c3183781d4cc0d8c5f672aeeddf11a6bffc693aaafbde34d957e21d04605d30
7
+ data.tar.gz: b6bb8e7f15b7f92ce50fe922fc04f418c7058506dbd78b09ce069bd1b8ad77b13a18064dbdf1dbc578df45e379c947f849ae1d294271e28bcd9f0acaa17980cd
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.6.0
1
+ 0.7.0
data/lib/iot/iot.rb CHANGED
@@ -74,13 +74,14 @@ class InOurTime
74
74
  end
75
75
 
76
76
  def quit code = 0
77
+ @key.kill if @key
78
+ @tic.kill if @tic
79
+ sleep 0.5
77
80
  STDIN.echo = true
78
81
  STDIN.cooked!
79
- clear
82
+ puts "\n\n#{@error_msg}" if @error_msg
80
83
  puts 'Quitting...'
81
- clear
82
- @key.kill
83
- @tic.kill
84
+ sleep 0.5
84
85
  exit code
85
86
  end
86
87
 
@@ -89,9 +90,15 @@ class InOurTime
89
90
  do_events while Time.now - @start_time < 1.5
90
91
  end
91
92
 
92
- def iot_print x, col = @text_colour
93
- @content << x.colorize(col) if @config[:colour]
94
- @content << x unless @config[:colour]
93
+ def iot_print x, col = @text_colour, now = false
94
+ content = ''
95
+ content << x.colorize(col) if @config[:colour]
96
+ content << x unless @config[:colour]
97
+ unless now
98
+ @content << content
99
+ else
100
+ $stdout << content
101
+ end
95
102
  end
96
103
 
97
104
  def iot_puts x = '', col = @text_colour
@@ -281,8 +288,9 @@ class InOurTime
281
288
  case res
282
289
  when Net::HTTPOK
283
290
  File.open(filename_from_title(program[:title]) , 'wb') do |f|
284
- iot_print "writing #{filename_from_title(program[:title])}...", @system_colour
291
+ iot_puts "writing #{File.basename(filename_from_title(program[:title]))}", @system_colour
285
292
  render
293
+ sleep 0.2
286
294
  f.print(res.body)
287
295
  iot_puts " written.", @system_colour
288
296
  render
@@ -306,10 +314,10 @@ class InOurTime
306
314
 
307
315
  def update
308
316
  clear_content
309
- iot_print "Checking rss feeds ", @system_colour
317
+ clear
318
+ iot_print "Checking rss feeds ", @system_colour, :now
310
319
  local_rss.length.times do |count|
311
- iot_print '.', @system_colour
312
- render
320
+ iot_print '.', @system_colour, :now
313
321
  fetch_uri rss_addresses[count], rss_files[count]
314
322
  end
315
323
  @config[:last_update] = now
@@ -384,20 +392,28 @@ class InOurTime
384
392
  @sorted_titles.sort_by!(&:downcase) unless @sort == :age
385
393
  end
386
394
 
395
+ def get_line_count idx
396
+ idx += 1
397
+ while idx % @page_height != 0
398
+ idx += 1
399
+ end
400
+ idx
401
+ end
402
+
387
403
  def sort_selected title
388
- @sorted_titles.each_with_index do |st, idx|
404
+ @sorted_titles.each_with_index do |st, sel|
389
405
  if st == title
390
- selected = idx
391
- idx += 1
392
- while idx % @page_height != 0
393
- idx += 1
394
- end
395
- return selected, idx
406
+ return sel, get_line_count(sel)
396
407
  end
397
408
  end
398
409
  end
399
410
 
400
- def list_selected title
411
+ def draw_selected
412
+ @line_count = get_line_count(@selected)
413
+ redraw
414
+ end
415
+
416
+ def draw_by_title title
401
417
  @selected, @line_count = sort_selected(title)
402
418
  redraw
403
419
  end
@@ -405,7 +421,7 @@ class InOurTime
405
421
  def list_key
406
422
  title = @playing ? @playing : (@sorted_titles[@last_selected || 0])
407
423
  if top_or_end?
408
- list_selected title
424
+ draw_by_title title
409
425
  else
410
426
  @last_selected = @selected
411
427
  list_top_or_end
@@ -427,20 +443,20 @@ class InOurTime
427
443
 
428
444
  def list_top
429
445
  @last_selected = @selected
430
- title = @sorted_titles.first
431
- list_selected title
446
+ @selected = 0
447
+ draw_selected
432
448
  end
433
449
 
434
450
  def list_end
435
451
  @last_selected = @selected
436
- title = @sorted_titles.last
437
- list_selected title
452
+ @selected = @titles_count - 1
453
+ draw_selected
438
454
  end
439
455
 
440
456
  def sort_key
441
457
  title = @sorted_titles[@selected]
442
458
  toggle_sort
443
- list_selected title
459
+ draw_by_title title
444
460
  end
445
461
 
446
462
  def toggle_sort
@@ -451,7 +467,8 @@ class InOurTime
451
467
  end
452
468
 
453
469
  def redraw
454
- display_list :same_page
470
+ @line_count -= @page_height
471
+ draw_page
455
472
  end
456
473
 
457
474
  def date
@@ -473,11 +490,16 @@ class InOurTime
473
490
  @config[:mpg_player] == :mpg123
474
491
  end
475
492
 
493
+ def get_player
494
+ return 'afplay' if @config[:mpg_player] == :afplay
495
+ @config[:mpg_player].to_s
496
+ end
497
+
476
498
  def player_cmd
477
499
  if use_mpg123?
478
500
  "mpg123 --remote-err -Cqk#{pre_delay}"
479
501
  else
480
- "afplay"
502
+ get_player
481
503
  end
482
504
  end
483
505
 
@@ -525,7 +547,7 @@ class InOurTime
525
547
  @key = KeyboardEvents.new
526
548
  begin
527
549
  if choice.to_i > 0 && choice.to_i < 6
528
- list_selected results[choice.to_i - 1][0]
550
+ draw_by_title(results[choice.to_i - 1][0])
529
551
  return
530
552
  end
531
553
  end
@@ -568,48 +590,73 @@ class InOurTime
568
590
  display_search_choice results, choice
569
591
  end
570
592
 
571
- def run_program prg
572
- unless prg[:have_locally]
573
- retries = 0
574
- clear_content
575
- iot_puts "Fetching #{prg[:title]}", @system_colour
576
- render
577
- 10.times do
578
- begin
579
- res = Net::HTTP.get_response(URI.parse(prg[:link]))
580
- rescue SocketError => e
581
- print_error_and_delay "Error: Failed to connect to Internet! (#{e.class})"
582
- render
583
- @no_play = true
584
- break
585
- end
586
- case res
587
- when Net::HTTPFound
588
- iot_puts 'redirecting...', @system_colour
589
- render
590
- @doc = Oga.parse_xml(res.body)
591
- redirect = @doc.css("body p a").text
592
- break if download_audio(prg, redirect)
593
- sleep 2
594
- else
595
- print_error_and_delay 'Error! Failed to be redirected!'
596
- render
597
- @no_play = true
598
- end
599
- retries += 1
593
+ def download prg
594
+ return if prg[:have_locally]
595
+ retries = 0
596
+ clear_content
597
+ iot_puts "Fetching #{prg[:title]}", @system_colour
598
+ render
599
+ 10.times do
600
+ begin
601
+ res = Net::HTTP.get_response(URI.parse(prg[:link]))
602
+ rescue SocketError => e
603
+ print_error_and_delay "Error: Failed to connect to Internet! (#{e.class})"
604
+ render
605
+ @no_play = true
606
+ break
600
607
  end
601
- if retries >= 10
602
- print_error_and_delay "Max retries downloading #{prg[:title]}"
608
+ case res
609
+ when Net::HTTPFound
610
+ iot_puts 'redirecting...', @system_colour
611
+ render
612
+ @doc = Oga.parse_xml(res.body)
613
+ redirect = @doc.css("body p a").text
614
+ break if download_audio(prg, redirect)
615
+ sleep 2
616
+ else
617
+ print_error_and_delay 'Error! Failed to be redirected!'
603
618
  render
604
619
  @no_play = true
605
620
  end
621
+ retries += 1
606
622
  end
623
+ if retries >= 10
624
+ print_error_and_delay "Max retries downloading #{prg[:title]}"
625
+ render
626
+ @no_play = true
627
+ end
628
+ end
629
+
630
+ # Cross-platform way of finding an executable in the $PATH.
631
+ #
632
+ # which('ruby') #=> /usr/bin/ruby
633
+
634
+ def which(cmd)
635
+ exts = ENV['PATHEXT'] ? ENV['PATHEXT'].split(';') : ['']
636
+ ENV['PATH'].split(File::PATH_SEPARATOR).each do |path|
637
+ exts.each { |ext|
638
+ exe = File.join(path, "#{cmd}#{ext}")
639
+ return exe if File.executable?(exe) && !File.directory?(exe)
640
+ }
641
+ end
642
+ return nil
643
+ end
644
+
645
+ def unknown_player cmd
646
+ @error_msg = "Error: Unknown MPG Player: #{cmd}\r"
647
+ quit 1
648
+ end
649
+
650
+ def run_program prg
651
+ download prg
607
652
  unless @no_play
608
653
  @playing = prg[:title]
654
+ player = player_cmd.split(' ').first
655
+ unknown_player(player) unless which(player)
609
656
  window_title prg[:title]
610
- @cmd = player_cmd + ' ' + filename_from_title(@playing)
657
+ cmd = player_cmd + ' ' + filename_from_title(@playing)
611
658
  @messages = []
612
- @p_out, @p_in, @pid = PTY.spawn(@cmd)
659
+ @p_out, @p_in, @pid = PTY.spawn(cmd)
613
660
  end
614
661
  @no_play = nil
615
662
  end
@@ -716,24 +763,6 @@ class InOurTime
716
763
  render
717
764
  end
718
765
 
719
- def display_list action
720
- case action
721
- when :next_page
722
- draw_page
723
- when :previous_page
724
- if @line_count > 0
725
- @line_count -= (@page_height * 2)
726
- else
727
- @line_count = @titles_count
728
- @selected = @line_count
729
- end
730
- draw_page
731
- when :same_page
732
- @line_count -= @page_height
733
- draw_page
734
- end
735
- end
736
-
737
766
  def help_option?
738
767
  ARGV[0] == '-h' || ARGV[0] == '--help' || ARGV[0] == '-?'
739
768
  end
@@ -757,6 +786,7 @@ class InOurTime
757
786
  " Theme Toggle - T " <<
758
787
  " List Top/End - L " <<
759
788
  " Update - U " <<
789
+ " Download - D " <<
760
790
  " Info - I " <<
761
791
  " Help - H " <<
762
792
  " Quit - Q " <<
@@ -770,11 +800,11 @@ class InOurTime
770
800
 
771
801
  def help_partial x,y; help_screen[x..y] end
772
802
  def help_title; help_partial(0, 1) end
773
- def help_main; help_partial(2, 12) end
774
- def help_cfg; help_partial(17, -1) end
803
+ def help_main; help_partial(2, 13) end
804
+ def help_cfg; help_partial(18, -1) end
775
805
 
776
806
  def help_mpg
777
- scr = help_partial(13,16)
807
+ scr = help_partial(14,17)
778
808
  scr[0].rstrip! << ' (enabled) ' if use_mpg123?
779
809
  scr[0].rstrip! << ' (disabled) ' unless use_mpg123?
780
810
  scr
@@ -954,44 +984,36 @@ class InOurTime
954
984
  def page_forward
955
985
  return unless @line_count < @titles_count
956
986
  @selected = @line_count
957
- display_list :next_page
987
+ draw_selected
958
988
  end
959
989
 
960
990
  def page_back
961
991
  @selected = @line_count - @page_height * 2
962
992
  @selected = @selected < 0 ? 0 : @selected
963
- list_selected @sorted_titles[@selected]
993
+ draw_selected
964
994
  end
965
995
 
966
996
  def previous
967
- @selected -= 1 if @selected > 0
968
- if @selected >= @line_count -
969
- @page_height
970
- redraw
971
- else
972
- display_list :previous_page
973
- end
997
+ return if @selected <= 0
998
+ @selected -= 1
999
+ draw_selected
974
1000
  end
975
1001
 
976
1002
  def next
977
1003
  return if @selected >= (@titles_count - 1)
978
1004
  @selected += 1
979
- if @selected <= @line_count - 1
980
- redraw
981
- else
982
- display_list :next_page
983
- end
1005
+ draw_selected
984
1006
  end
985
1007
 
986
1008
  def play
987
- if @playing
988
- kill_audio
989
- else
1009
+ if(@playing != @sorted_titles[@selected]) || (! @playing)
990
1010
  kill_audio
991
1011
  title = @sorted_titles[@selected]
992
1012
  pr = select_program title
993
1013
  run_program pr
994
1014
  redraw
1015
+ else
1016
+ kill_audio
995
1017
  end
996
1018
  end
997
1019
 
@@ -999,9 +1021,15 @@ class InOurTime
999
1021
  update
1000
1022
  parse_rss
1001
1023
  sort_titles
1002
- @line_count = 0
1003
1024
  @selected = 0
1004
- display_list :next_page
1025
+ draw_selected
1026
+ end
1027
+
1028
+ def download_key
1029
+ title = @sorted_titles[@selected]
1030
+ pr = select_program title
1031
+ download pr
1032
+ draw_selected
1005
1033
  end
1006
1034
 
1007
1035
  def quit_key
@@ -1013,7 +1041,7 @@ class InOurTime
1013
1041
  case ip
1014
1042
  when :pause, :forward, :rewind, :list_key, :page_forward, :page_back,
1015
1043
  :previous, :next, :play, :sort_key, :theme_toggle, :update_key,
1016
- :info, :help, :quit_key, :search
1044
+ :info, :help, :quit_key, :search, :download_key
1017
1045
  self.send ip
1018
1046
  end
1019
1047
  end
@@ -72,30 +72,30 @@ class KeyboardEvents
72
72
  when "\e"
73
73
  @mode = :escape
74
74
  :no_event
75
- when "l",'L'
75
+ when 'd', 'D'
76
+ :download_key
77
+ when 'f', 'F'
78
+ :forward
79
+ when 'h', 'H'
80
+ :help
81
+ when 'i', 'I'
82
+ :info
83
+ when 'l', 'L'
76
84
  :list_key
77
- when "u",'U'
78
- :update_key
79
- when ' '
85
+ when 'p', 'P', ' '
80
86
  :pause
81
- when "q",'Q', "\u0003", "\u0004"
87
+ when 'q', 'Q', "\u0003", "\u0004"
82
88
  :quit_key
83
- when 'p', 'P'
84
- :pause
85
- when 'f', 'F'
86
- :forward
87
89
  when 'r', 'R'
88
90
  :rewind
89
91
  when 's', 'S'
90
92
  :sort_key
91
93
  when 't', 'T'
92
94
  :theme_toggle
95
+ when 'u', 'U'
96
+ :update_key
93
97
  when 'x', 'X', "\r"
94
98
  :play
95
- when 'i', 'I'
96
- :info
97
- when 'h', 'H'
98
- :help
99
99
  when '?'
100
100
  :search
101
101
  else
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: in_our_time
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.0
4
+ version: 0.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Martyn Jago