in_our_time 0.2.1 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (6) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +0 -1
  3. data/VERSION +1 -1
  4. data/config.yml +30 -0
  5. data/lib/iot/iot.rb +111 -78
  6. metadata +3 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 118ea99783afae1f37cbacccac7101f6eee07784
4
- data.tar.gz: 5c7ef1c9c9aff29c1c810453e1cc3ad6852625e9
3
+ metadata.gz: a4d33efed1ec31350eb4993c5be89f4506d0200b
4
+ data.tar.gz: dcada7f6e8bdafe08258d7529785af1fdbbac2ac
5
5
  SHA512:
6
- metadata.gz: 37c71d0c90d5aaf141081e0b18eb2792fa66b7cfb9dd22eb48224621e45a0160fb3a6d0a53174b9948615caf8f33147b9c8ae87b46d0171edec819ba05ee1760
7
- data.tar.gz: da7f11e577a7876950b502be5a5731c92781fb16049d563d4c0dec9c0f2b36923178cdef85866817e6a33d30083c768a7f824744eac76899a48716580d4b08a0
6
+ metadata.gz: ec29eeae228da573d068ef13d71ee7c05632fda8b99a331045afbc2cbf4950c23bd30112a0d4da05205645bec85c515f9177fd4bbbb347b44eacc0e6f194007c
7
+ data.tar.gz: ee0742b483f3ea7859ebbc36e72b48dd60743b6c87acc692628326021575642f2286728aa1563571ebcc022d15e96ba6e51e1b346700339cb04072a60c7f7321
data/.gitignore CHANGED
@@ -1,4 +1,3 @@
1
1
  /audio/
2
2
  /rss/
3
3
  /in_our_time.org
4
- /config.yml
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.1
1
+ 0.3.0
data/config.yml ADDED
@@ -0,0 +1,30 @@
1
+ ---
2
+ :last_update: 1474712963
3
+ :update_interval: 604800
4
+ :colour: true
5
+ :mpg_player: :afplay
6
+ :sort: :age
7
+ :show_count: true
8
+ :page_height: 20
9
+ :page_width: 80
10
+ :colour_theme: :light_theme
11
+ :light_theme:
12
+ :selection_colour:
13
+ :colour: :magenta
14
+ :background: :light_white
15
+ :count_sel_colour:
16
+ :colour: :cyan
17
+ :background: :light_white
18
+ :count_colour: :yellow
19
+ :text_colour: :default
20
+ :system_colour: :yellow
21
+ :dark_theme:
22
+ :selection_colour:
23
+ :colour: :light_yellow
24
+ :background: :light_black
25
+ :count_sel_colour:
26
+ :colur: :blue
27
+ :background: :yellow
28
+ :count_colour: :yellow
29
+ :text_colour: :default
30
+ :system_colour: :yellow
data/lib/iot/iot.rb CHANGED
@@ -10,8 +10,12 @@ require 'oga'
10
10
  class InOurTime
11
11
 
12
12
  ROOT = File.expand_path '~/'
13
- IN_OUR_TIME = File.join ROOT, '.in_our_time'
14
- CONFIG = File.join IN_OUR_TIME, 'config.yml'
13
+ HERE = File.dirname(__FILE__)
14
+ CONFIG_DIR = '.in_our_time'
15
+ CONFIG_NAME = 'config.yml'
16
+ IN_OUR_TIME = File.join ROOT, CONFIG_DIR
17
+ DEFAULT_CONFIG = File.join HERE, '..','..',CONFIG_NAME
18
+ CONFIG = File.join IN_OUR_TIME,CONFIG_NAME
15
19
  UPDATE_INTERVAL = 604800
16
20
  AUDIO_DIRECTORY = 'audio'
17
21
  RSS_DIRECTORY = 'rss'
@@ -20,7 +24,7 @@ class InOurTime
20
24
 
21
25
  class KeyboardEvents
22
26
 
23
- @arrow = 0
27
+ @mode = :normal
24
28
 
25
29
  def reset
26
30
  $stdin.flush
@@ -34,25 +38,25 @@ class InOurTime
34
38
  system("stty -raw echo")
35
39
  end
36
40
 
37
- case @arrow
38
- when 1
41
+ case @mode
42
+ when :escape
39
43
  if str == "["
40
- @arrow = 2
44
+ @mode = :escape_2
41
45
  else
42
- @arrow = 0
46
+ @mode = :normal
43
47
  end
44
48
 
45
- when 2
49
+ when :escape_2
46
50
  return :previous if str == "A"
47
51
  return :next if str == "B"
48
52
  return :page_forward if str == "C"
49
53
  return :previous if str == "D"
50
- @arrow = 0
54
+ @mode = :normal
51
55
  end
52
56
 
53
57
  case str
54
58
  when "\e"
55
- @arrow = 1
59
+ @mode = :escape
56
60
  when "l",'L'
57
61
  :list
58
62
  when ' '
@@ -117,38 +121,12 @@ class InOurTime
117
121
  now - @config[:update_interval] > @config[:last_update]
118
122
  end
119
123
 
120
- def new_config
121
- {:last_update => now - UPDATE_INTERVAL - 1,
122
- :update_interval => UPDATE_INTERVAL,
123
- :colour => true,
124
- :mpg_player => :afplay,
125
- :sort => :age,
126
- :show_count => true,
127
- :page_height => PAGE_HEIGHT,
128
- :page_width => PAGE_WIDTH,
129
- :colour_theme => :light_theme,
130
- :light_theme => {
131
- :selection_colour => {:colour => :magenta, :background => :light_white},
132
- :count_sel_colour => {:colour => :cyan, :background => :light_white},
133
- :count_colour => :yellow,
134
- :text_colour => :default,
135
- :system_colour => :yellow
136
- },
137
- :dark_theme => {
138
- :selection_colour => {:colour => :light_yellow, :background => :light_black},
139
- :count_sel_colour => {:colur => :blue, :background => :yellow},
140
- :count_colour => :yellow,
141
- :text_colour => :default,
142
- :system_colour => :yellow
143
- }
144
- }
124
+ def create_config
125
+ @config = YAML::load_file(DEFAULT_CONFIG)
126
+ save_config
145
127
  end
146
128
 
147
- def load_config
148
- unless File.exist? CONFIG
149
- save_config new_config
150
- end
151
- @config = YAML::load_file(CONFIG)
129
+ def do_configs
152
130
  @line_count = @config[:page_height]
153
131
  theme = @config[:colour_theme]
154
132
  @selection_colour = @config[theme][:selection_colour]
@@ -158,8 +136,14 @@ class InOurTime
158
136
  @system_colour = @config[theme][:system_colour]
159
137
  end
160
138
 
161
- def save_config cfg = @config
162
- File.open(CONFIG, 'w') { |f| f.write cfg.to_yaml}
139
+ def load_config
140
+ create_config unless File.exist? CONFIG
141
+ @config = YAML::load_file(CONFIG)
142
+ do_configs
143
+ end
144
+
145
+ def save_config
146
+ File.open(CONFIG, 'w') { |f| f.write @config.to_yaml}
163
147
  end
164
148
 
165
149
  def rss_addresses
@@ -208,7 +192,7 @@ class InOurTime
208
192
  end
209
193
  program[:have_locally] = true
210
194
  else
211
- iot_puts 'audio download failed. Retrying...', @system_colour
195
+ iot_puts 'Download failed. Retrying...', @system_colour
212
196
  end
213
197
  end
214
198
 
@@ -226,7 +210,7 @@ class InOurTime
226
210
 
227
211
  def check_remote
228
212
  if update_remote?
229
- iot_print "checking rss feeds ", @system_colour
213
+ iot_print "Checking rss feeds ", @system_colour
230
214
  local_rss.length.times do |count|
231
215
  iot_print '.', @system_colour
232
216
  fetch_uri rss_addresses[count], rss_files[count]
@@ -240,7 +224,7 @@ class InOurTime
240
224
  def uniquify_programs
241
225
  @programs = @programs.uniq{|pr| pr[:title]}
242
226
  unless @programs.uniq.length == @programs.length
243
- iot_puts "Error ensuring Programs unique!", :red
227
+ print_error_and_delay "Error ensuring Programs unique!"
244
228
  exit 1
245
229
  end
246
230
  end
@@ -314,12 +298,28 @@ class InOurTime
314
298
  end
315
299
  end
316
300
 
301
+ def clear
302
+ system 'clear' or system 'cls'
303
+ end
304
+
305
+ def print_error_and_delay message
306
+ iot_puts message, :red
307
+ sleep 2
308
+ end
309
+
317
310
  def run_program prg
318
311
  unless prg[:have_locally]
319
312
  retries = 0
320
- iot_puts "fetching #{prg[:title]}", @system_colour
313
+ clear
314
+ iot_puts "Fetching #{prg[:title]}", @system_colour
321
315
  10.times do
322
- res = Net::HTTP.get_response(URI.parse(prg[:link]))
316
+ begin
317
+ res = Net::HTTP.get_response(URI.parse(prg[:link]))
318
+ rescue SocketError => e
319
+ print_error_and_delay "Error: Failed to connect to Internet! (#{e.class})"
320
+ @no_play = true
321
+ break
322
+ end
323
323
  case res
324
324
  when Net::HTTPFound
325
325
  iot_puts 'redirecting...', @system_colour
@@ -328,22 +328,26 @@ class InOurTime
328
328
  break if download_audio(prg, redirect)
329
329
  sleep 2
330
330
  else
331
- iot_puts 'Error! Expected to be redirected!', :red
332
- exit 1
331
+ print_error_and_delay 'Error! Failed to be redirected!'
332
+ @no_play = true
333
+ break
333
334
  end
334
335
  retries += 1
335
336
  end
336
337
  if retries >= 10
337
- iot_puts "Max retries downloading #{prg[:title]}", :red
338
- exit 1
338
+ print_error_and_delay "Max retries downloading #{prg[:title]}"
339
+ @no_play = true
339
340
  end
340
341
  end
341
- @play = Thread.new do
342
- @playing = prg[:title]
343
- system player_cmd + ' ' +
344
- filename_from_title(@playing)
345
- @playing = nil
342
+ unless @no_play
343
+ @play = Thread.new do
344
+ @playing = prg[:title]
345
+ system player_cmd + ' ' +
346
+ filename_from_title(@playing)
347
+ @playing, @no_play = nil, nil
348
+ end
346
349
  end
350
+ @no_play = nil
347
351
  end
348
352
 
349
353
  def print_playing_maybe
@@ -385,7 +389,7 @@ class InOurTime
385
389
  iot_print "> " if(idx == @selected) unless @config[:colour]
386
390
  show_count_maybe idx
387
391
  iot_puts @sorted_titles[idx], @selection_colour if (idx == @selected)
388
- iot_puts @sorted_titles[idx], @text_colour unless(idx == @selected)
392
+ iot_puts @sorted_titles[idx], @text_colour unless(idx == @selected)
389
393
  end
390
394
  end
391
395
  else
@@ -401,7 +405,7 @@ class InOurTime
401
405
  end
402
406
 
403
407
  def display_list action
404
- system 'clear'
408
+ clear
405
409
  case action
406
410
  when :draw_page
407
411
  draw_page
@@ -421,7 +425,7 @@ class InOurTime
421
425
 
422
426
  def help
423
427
  unless @help
424
- system 'clear'
428
+ clear
425
429
  iot_puts " In Our Time Player (Help) "
426
430
  iot_puts " "
427
431
  iot_puts " Next - N (down arrow) "
@@ -436,7 +440,7 @@ class InOurTime
436
440
  iot_puts " TL;DR "
437
441
  iot_puts "Select: up/down arrows "
438
442
  iot_puts "Play: enter "
439
- iot_puts "Config: ~/.in_our_time/config.yml"
443
+ iot_puts "Config: #{CONFIG} "
440
444
  18.upto(@config[:page_height] - 1) {iot_puts ''}
441
445
  print_playing_maybe
442
446
  @help = true
@@ -495,28 +499,57 @@ class InOurTime
495
499
  pages
496
500
  end
497
501
 
502
+ def print_subtitle prg
503
+ clear
504
+ justify(prg[:subtitle].gsub(/\s+/, ' '))[0].map{|x| iot_puts x}
505
+ print_program_details prg
506
+ @info = 1
507
+ @page_count = 1
508
+ end
509
+
510
+ def print_program_details prg
511
+ iot_puts "\nDate Broadcast: #{prg[:date]}"
512
+ iot_puts "Duration: #{prg[:duration].to_i/60} mins"
513
+ iot_puts "Availability: " +
514
+ (prg[:have_locally] ? "Downloaded" : "Requires Download")
515
+ end
516
+
517
+ def print_info prg
518
+ info = prg[:summary].gsub(/\s+/, ' ')
519
+ clear
520
+ count = 1
521
+ justify(reformat(info))[0].each do |x|
522
+ if (count > (@page_count - 1) * @config[:page_height]) &&
523
+ (count <= @page_count * @config[:page_height])
524
+ iot_puts x
525
+ end
526
+ count += 1
527
+ end
528
+ if count <= @page_count * @config[:page_height] + 1
529
+ @info = justify(reformat(info))[1] == [] ? -1 : 2
530
+ else
531
+ @page_count += 1
532
+ end
533
+ end
534
+
535
+ def print_guests prg
536
+ info = prg[:summary].gsub(/\s+/, ' ')
537
+ clear
538
+ justify(reformat(info))[1].map{|x| iot_puts x}
539
+ @info = -1
540
+ end
541
+
498
542
  def info
499
- if @info.nil?
543
+ case @info
544
+ when nil
500
545
  prg = select_program @sorted_titles[@selected]
501
- system 'clear'
502
- justify(prg[:subtitle].gsub(/\s+/, ' '))[0].map{|x| iot_puts x}
503
- iot_puts "\nDate Broadcast: #{prg[:date]}"
504
- iot_puts "Duration: #{prg[:duration].to_i/60} mins"
505
- iot_puts "Availability: " +
506
- (prg[:have_locally] ? "Downloaded" : "Requires Download")
507
- @info = 1
508
- elsif @info == 1
546
+ print_subtitle prg
547
+ when 1
509
548
  prg = select_program @sorted_titles[@selected]
510
- info = prg[:summary].gsub(/\s+/, ' ')
511
- system 'clear'
512
- justify(reformat(info))[0].map{|x| iot_puts x}
513
- @info = justify(reformat(info))[1] == [] ? -1 : 2
514
- elsif @info == 2
549
+ print_info prg
550
+ when 2
515
551
  prg = select_program @sorted_titles[@selected]
516
- info = prg[:summary].gsub(/\s+/, ' ')
517
- system 'clear'
518
- justify(reformat(info))[1].map{|x| iot_puts x}
519
- @info = -1
552
+ print_guests prg
520
553
  else
521
554
  display_list :same_page
522
555
  @info = nil
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.2.1
4
+ version: 0.3.0
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-09-29 00:00:00.000000000 Z
11
+ date: 2016-10-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: oga
@@ -66,6 +66,7 @@ files:
66
66
  - Rakefile
67
67
  - VERSION
68
68
  - bin/iot
69
+ - config.yml
69
70
  - dark_theme.png
70
71
  - in_our_time.gemspec
71
72
  - lib/in_our_time.rb