in_our_time 0.3.1 → 0.3.2

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.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/VERSION +1 -1
  3. data/lib/iot/iot.rb +217 -110
  4. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: ed0b5b221412272a4878b2d8d4292a55785cc1c1
4
- data.tar.gz: 3381834d2955875c617f2d5ad97fe5ec89cabba5
3
+ metadata.gz: cf0d05644a0136dc6303c04a2f5337f9f0120b40
4
+ data.tar.gz: 164058d08b3fa8ed5c712b93b907a209afb00790
5
5
  SHA512:
6
- metadata.gz: 718ce0b3cc69e71799797625f28d101d2e030aa2818a5b85e2dd07521703a4619597ae9527c3ea0bfbbc22f1ad76be8b460f613c007f2dd81fd9f3a9e2f6584e
7
- data.tar.gz: 94eccc73c95354135426ab82920b27c03fc9240ca4ea2eb9a2acd6aa6d4d61c8895ceb139fba4caf81f3b2859020d2eb35879993507ef0deb6501e50c8609b22
6
+ metadata.gz: 8090701ea1243be0213807a7b10540b6d6b1e5c5a28a3a73e069d4f99f62c560f40c74c05fb0064870b03b57b2163c8f77be5c650667bc6a0928a246249d0290
7
+ data.tar.gz: 070414c04c8b5722e226e41e0b179107ac97fd0defe7424a9b9a19077055c76dc30c4ce11082c3b569e0ee869b9a4375792609d5eb65dc9383ae36dc88e34d27
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.3.1
1
+ 0.3.2
data/lib/iot/iot.rb CHANGED
@@ -7,6 +7,7 @@ require 'fileutils'
7
7
  require 'colorize'
8
8
  require 'oga'
9
9
  require 'pty'
10
+ require 'io/console'
10
11
 
11
12
  class InOurTime
12
13
 
@@ -26,67 +27,107 @@ class InOurTime
26
27
 
27
28
  class KeyboardEvents
28
29
 
29
- @mode = :normal
30
+ def initialize
31
+ @mode = :normal
32
+ @event = :no_event
33
+ end
30
34
 
31
35
  def reset
32
- $stdin.flush
36
+ STDIN.flush
33
37
  end
34
38
 
35
- def input
36
- begin
37
- system("stty raw -echo")
38
- str = $stdin.getc
39
- ensure
40
- system("stty -raw echo")
41
- end
39
+ def ke_events
40
+ sleep 0.001
41
+ end
42
42
 
43
- case @mode
44
- when :escape
45
- if str == "["
46
- @mode = :escape_2
47
- else
48
- @mode = :normal
43
+ def read
44
+ ret_val = @event
45
+ # reset
46
+ @event = :no_event
47
+ ret_val
48
+ end
49
+
50
+ def run
51
+ loop do
52
+ str = ''
53
+ loop do
54
+ str = STDIN.getch
55
+ if str == "\e"
56
+ @mode = :escape
57
+ else
58
+ case @mode
59
+ when :escape
60
+ if str == "["
61
+ @mode = :escape_2
62
+ else
63
+ @mode = :normal
64
+ end
65
+ when :escape_2
66
+ @event = :previous if str == "A"
67
+ @event = :next if str == "B"
68
+ @event = :page_forward if str == "C"
69
+ @event = :previous if str == "D"
70
+ @mode = :normal
71
+
72
+ else
73
+ break if @event == :no_event
74
+ end
75
+ end
76
+ ke_events
49
77
  end
50
78
 
51
- when :escape_2
52
- return :previous if str == "A"
53
- return :next if str == "B"
54
- return :page_forward if str == "C"
55
- return :previous if str == "D"
56
- @mode = :normal
79
+ case str
80
+ when "\e"
81
+ @mode = :escape
82
+ when "l",'L'
83
+ @event = :list
84
+ when ' '
85
+ @event = :page_forward
86
+ when "q",'Q', "\u0003", "\u0004"
87
+ @event = :quit
88
+ when 'p', 'P'
89
+ @event = :pause
90
+ when 'f', 'F'
91
+ @event = :forward
92
+ when 'r', 'R'
93
+ @event = :rewind
94
+ when 's', 'S'
95
+ @event = :sort
96
+ when 'x', 'X', "\r"
97
+ @event = :play
98
+ when 'i', 'I'
99
+ @event = :info
100
+ when '?', 'h'
101
+ @event = :help
102
+ else
103
+ @event = :no_event
104
+ end
105
+ ke_events
57
106
  end
107
+ end
108
+ end
58
109
 
59
- case str
60
- when "\e"
61
- @mode = :escape
62
- when "l",'L'
63
- :list
64
- when ' '
65
- :page_forward
66
- when "q",'Q', "\u0003", "\u0004"
67
- :quit
68
- when 'p', 'P'
69
- :pause
70
- when 'f', 'F'
71
- :forward
72
- when 'r', 'R'
73
- :rewind
74
- when 's', 'S'
75
- :stop
76
- when 'x', 'X', "\r"
77
- :play
78
- when 'i', 'I'
79
- :info
80
- when '?', 'h'
81
- :help
82
- else
83
- :unknown
110
+ class Tic
111
+ def initialize
112
+ @flag = false
113
+ end
114
+
115
+ def run
116
+ loop do
117
+ sleep 1
118
+ @flag = true
84
119
  end
85
120
  end
121
+
122
+ def toc
123
+ ret_val, @flag = @flag, false
124
+ ret_val
125
+ end
86
126
  end
87
127
 
88
128
  def initialize
89
129
  @programs, @selected = [], 0
130
+ @lock = Mutex.new
90
131
  setup
91
132
  load_config
92
133
  load_version
@@ -99,18 +140,27 @@ class InOurTime
99
140
  run
100
141
  end
101
142
 
143
+ def do_events
144
+ sleep 0.003
145
+ end
146
+
147
+ def quit code = 0
148
+ system("stty -raw echo")
149
+ exit code
150
+ end
151
+
102
152
  def version_display_wait
103
- sleep 0.01 while Time.now - @start_time < 1
153
+ do_events while Time.now - @start_time < 1
104
154
  end
105
155
 
106
156
  def iot_print x, col = @text_colour
107
- print x.colorize col if @config[:colour]
108
- print x unless @config[:colour]
157
+ STDOUT.print x.colorize col if @config[:colour]
158
+ STDOUT.print x unless @config[:colour]
109
159
  end
110
160
 
111
- def iot_puts x, col = @text_colour
112
- puts x.colorize col if @config[:colour]
113
- puts x unless @config[:colour]
161
+ def iot_puts x = '', col = @text_colour
162
+ iot_print x, col
163
+ iot_print "\n\r"
114
164
  end
115
165
 
116
166
  def now
@@ -134,7 +184,7 @@ class InOurTime
134
184
  clear
135
185
  iot_print("Loading ", @system_colour) unless ARGV[0] == '-v' || ARGV[0] == '--version'
136
186
  iot_puts "In Our Time Player (#{@version})", @system_colour
137
- exit 0 if ARGV[0] == '-v' || ARGV[0] == '--version'
187
+ quit if ARGV[0] == '-v' || ARGV[0] == '--version'
138
188
  end
139
189
 
140
190
  def load_version
@@ -151,13 +201,21 @@ class InOurTime
151
201
  end
152
202
 
153
203
  def do_configs
154
- @line_count = @config[:page_height]
155
204
  theme = @config[:colour_theme]
156
205
  @selection_colour = @config[theme][:selection_colour]
157
206
  @count_sel_colour = @config[theme][:count_sel_colour]
158
207
  @count_colour = @config[theme][:count_colour]
159
208
  @text_colour = @config[theme][:text_colour]
160
209
  @system_colour = @config[theme][:system_colour]
210
+ rows, cols = $stdout.winsize
211
+ while(rows % 10 != 0) ; rows -=1 ; end
212
+ while(cols % 10 != 0) ; cols -=1 ; end
213
+ rows = 10 if rows < 10
214
+ cols = 20 if cols < 20
215
+ @config[:page_height] = rows if(@config[:page_height] == :auto)
216
+ @config[:page_width] = cols if(@config[:page_width] == :auto)
217
+ @line_count = @config[:page_height]
218
+ @sort = @config[:sort]
161
219
  end
162
220
 
163
221
  def load_config
@@ -239,7 +297,7 @@ class InOurTime
239
297
  iot_print '.', @system_colour
240
298
  fetch_uri rss_addresses[count], rss_files[count]
241
299
  end
242
- iot_puts ''
300
+ iot_puts
243
301
  @config[:last_update] = now
244
302
  save_config
245
303
  end
@@ -249,7 +307,7 @@ class InOurTime
249
307
  @programs = @programs.uniq{|pr| pr[:title]}
250
308
  unless @programs.uniq.length == @programs.length
251
309
  print_error_and_delay "Error ensuring Programs unique!"
252
- exit 1
310
+ quit 1
253
311
  end
254
312
  end
255
313
 
@@ -290,7 +348,32 @@ class InOurTime
290
348
  def sort_titles
291
349
  @sorted_titles = []
292
350
  @sorted_titles = @programs.collect { |pr| pr[:title] }
293
- @sorted_titles.sort! unless @config[:sort] == :age
351
+ @sorted_titles.sort! unless @sort == :age
352
+ end
353
+
354
+ def sort_selected title
355
+ @sorted_titles.each_with_index do |st, idx|
356
+ if st == title
357
+ selected = idx
358
+ idx += 1
359
+ while idx % @config[:page_height] != 0
360
+ idx += 1
361
+ end
362
+ return selected, idx
363
+ end
364
+ end
365
+ end
366
+
367
+ def sort
368
+ title = @sorted_titles[@selected]
369
+ @sort = @sort == :age ? :alphabet : :age
370
+ sort_titles
371
+ @selected, @line_count = sort_selected(title)
372
+ redraw
373
+ end
374
+
375
+ def redraw
376
+ display_list :same_page
294
377
  end
295
378
 
296
379
  def date
@@ -318,7 +401,9 @@ class InOurTime
318
401
  end
319
402
 
320
403
  def clear
321
- system 'clear' or system 'cls'
404
+ @lock.synchronize do
405
+ system 'clear' or system 'cls'
406
+ end
322
407
  end
323
408
 
324
409
  def print_error_and_delay message
@@ -369,13 +454,13 @@ class InOurTime
369
454
  end
370
455
 
371
456
  def window_title title = ''
372
- puts "\"\033]0;#{title}\007"
457
+ STDOUT.puts "\"\033]0;#{title}\007"
373
458
  end
374
459
 
375
460
  def reset
376
461
  @pid, @playing, @paused = nil, nil, nil
377
462
  window_title
378
- display_list :same_page
463
+ redraw
379
464
  end
380
465
 
381
466
  def write_player str
@@ -390,16 +475,12 @@ class InOurTime
390
475
  if control_play?
391
476
  @paused = @paused ? false : true
392
477
  write_player " "
393
- display_list :same_page
478
+ redraw
394
479
  end
395
480
  end
396
481
 
397
482
  def control_play?
398
- if @config[:mpg_player] == :mpg123
399
- if @playing
400
- true
401
- end
402
- end
483
+ @playing && (@config[:mpg_player] == :mpg123)
403
484
  end
404
485
 
405
486
  def forward
@@ -495,7 +576,7 @@ class InOurTime
495
576
  def load_help_maybe
496
577
  if ARGV[0] == '-h' || ARGV[0] == '--help' || ARGV[0] == '-?'
497
578
  help
498
- exit 0
579
+ quit
499
580
  end
500
581
  end
501
582
 
@@ -503,25 +584,26 @@ class InOurTime
503
584
  unless @help
504
585
  clear
505
586
  iot_puts " In Our Time Player (#{@version})"
506
- iot_puts " Next - N or Down Key ", @system_colour
507
- iot_puts " Previous - P or Up Key ", @system_colour
508
- iot_puts " Next Page - SPACE ", @system_colour
509
- iot_puts " Play - X or Enter ", @system_colour
510
- iot_puts " Stop - S ", @system_colour
511
- iot_puts " List Page 1 - L ", @system_colour
512
- iot_puts " Info - I ", @system_colour
513
- iot_puts " Help - H ", @system_colour
514
- iot_puts " Quit - Q ", @system_colour
515
- iot_puts " mpg123 Controls: ", @system_colour
516
- iot_puts " Pause/Resume - P ", @system_colour
517
- iot_puts " Forward Skip - F ", @system_colour
518
- iot_puts " Reverse Skip - R ", @system_colour
519
- iot_puts " ", @system_colour
520
- iot_puts "Config: #{CONFIG}" , @system_colour
587
+ iot_puts " Next - N or Down Key ", @system_colour
588
+ iot_puts " Previous - P or Up Key ", @system_colour
589
+ iot_puts " Next Page - SPACE ", @system_colour
590
+ iot_puts " Play/Stop - X or Enter ", @system_colour
591
+ iot_puts " Sort - S ", @system_colour
592
+ iot_puts " List Top - L ", @system_colour
593
+ iot_puts " Info - I ", @system_colour
594
+ iot_puts " Help - H ", @system_colour
595
+ iot_puts " Quit - Q ", @system_colour
596
+ iot_puts
597
+ iot_puts " mpg123 Controls: ", @system_colour
598
+ iot_puts " Pause/Resume - P ", @system_colour
599
+ iot_puts " Forward Skip - F ", @system_colour
600
+ iot_puts " Reverse Skip - R ", @system_colour
601
+ iot_puts
602
+ iot_puts "Config: #{CONFIG}", @system_colour
521
603
  print_playing_maybe
522
604
  @help = true
523
605
  else
524
- display_list :same_page
606
+ redraw
525
607
  @help = nil
526
608
  end
527
609
  end
@@ -627,33 +709,52 @@ class InOurTime
627
709
  prg = select_program @sorted_titles[@selected]
628
710
  print_guests prg
629
711
  else
630
- display_list :same_page
712
+ redraw
631
713
  @info = nil
632
714
  end
633
715
  end
634
716
 
717
+ def check_process
718
+ if(@playing && @pid.is_a?(Fixnum))
719
+ begin
720
+ write_player( "\e")
721
+ if @pid.is_a? Fixnum
722
+ Process.kill 0, @pid
723
+ end
724
+ rescue Errno::ESRCH
725
+ reset
726
+ end
727
+ else
728
+ @pid = nil
729
+ end
730
+ end
731
+
635
732
  def run
733
+ ip = ''
734
+ @tic = Tic.new
735
+ @tic_thread = Thread.new do
736
+ @tic.run
737
+ end
738
+
739
+ @key = KeyboardEvents.new
740
+ key_thread = Thread.new do
741
+ @key.run
742
+ end
743
+ # sleep 0.015
744
+
636
745
  action = :unknown
637
- display_list :same_page
638
- key = KeyboardEvents.new
746
+ redraw
639
747
  loop do
748
+
640
749
  unless action == :unknown
641
- key.reset
750
+ @key.reset
642
751
  end
643
752
 
644
- ip = key.input
645
-
646
- if @pid.is_a? Fixnum
647
- begin
648
- write_player( "\e")
649
- if @pid.is_a? Fixnum
650
- Process.kill 0, @pid
651
- end
652
- rescue Errno::ESRCH
653
- reset
654
- end
655
- else
656
- @pid = nil
753
+ loop do
754
+ ip = @key.read
755
+ break unless ip == :no_event
756
+ check_process if @tic.toc
757
+ do_events
657
758
  end
658
759
 
659
760
  @info = nil unless ip == :info
@@ -678,33 +779,39 @@ class InOurTime
678
779
  @selected -= 1 if @selected > 0
679
780
  if @selected >= @line_count -
680
781
  @config[:page_height]
681
- display_list :same_page
782
+ redraw
682
783
  else
683
784
  display_list :previous_page
684
785
  end
685
786
  when :next
686
787
  @selected += 1
687
788
  if @selected <= @line_count - 1
688
- display_list :same_page
789
+ redraw
689
790
  else
690
791
  display_list :next_page
691
792
  end
692
793
  when :play
693
- kill_audio
694
- title = @sorted_titles[@selected]
695
- pr = select_program title
696
- run_program pr
697
- display_list :same_page
698
- when :stop
699
- kill_audio
794
+ if @playing
795
+ kill_audio
796
+ @playing = nil
797
+ else
798
+ kill_audio
799
+ title = @sorted_titles[@selected]
800
+ pr = select_program title
801
+ run_program pr
802
+ redraw
803
+ end
804
+ when :sort
805
+ sort
700
806
  when :info
701
807
  info
702
808
  when :help
703
809
  help
704
810
  when :quit
705
811
  kill_audio
706
- exit 0
812
+ quit
707
813
  end
814
+ do_events
708
815
  end
709
816
  end
710
817
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: in_our_time
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
4
+ version: 0.3.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Martyn Jago
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-10-02 00:00:00.000000000 Z
11
+ date: 2016-10-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: oga