in_our_time 0.5.0 → 0.6.0

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 6d431c6acebb6c984ae2866189aa2ff50b3d52c8
4
- data.tar.gz: 91ea04fe56e4b78e57fc46e4a42350b9f7abafde
3
+ metadata.gz: 5ebc3b3c85e4482305e7c6126b149228e277165a
4
+ data.tar.gz: eff2a53fb96a38076a283e2c668e09c4d169f090
5
5
  SHA512:
6
- metadata.gz: 12cdc3af80022e5a5d50cb34b98d8f2a58431225dde9625b0f2327780892d968c0764e006eee719c8abdd45b42d97b337e9eccf615e84f2fa0e920a23263e12d
7
- data.tar.gz: a12f8953946ed6708e765f79ed4a9ce1d808f5c63091ef5445f70833678965bfc32e2de3589c5a3cb68ff54ffce9ddf9eca748d476a0b81bfa5d639728ec918b
6
+ metadata.gz: 418b56bec73a51dae797340183fc2f7b7a9e7a98b7378e7ba82dcb6be441a887e90a6cfcced2a156725816348d4abba73d04ab5b7dec333ea9f7be32d18f4a91
7
+ data.tar.gz: afd484a3f52597e77c3b2a0b731a86da0f1c89b05b96e534b6751e4f92c1078eca1717cb71acf527cb9076b1f1304203f5d14ddd840af220b40f65e9434635a0
data/.gitignore CHANGED
@@ -2,3 +2,6 @@
2
2
  /rss/
3
3
  /in_our_time.org
4
4
  /.rubocop_todo.yml
5
+ /.rbenv-gemsets
6
+ /.rubocop.yml
7
+ /Gemfile.lock
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.5.0
1
+ 0.6.0
data/lib/in_our_time.rb CHANGED
@@ -1,3 +1,3 @@
1
- require_relative 'iot/iot.rb'
2
-
1
+ require_relative 'iot/iot'
2
+ require_relative 'iot/levenshtein'
3
3
  InOurTime.new
data/lib/iot/iot.rb CHANGED
@@ -469,9 +469,12 @@ class InOurTime
469
469
  end
470
470
  end
471
471
 
472
+ def use_mpg123?
473
+ @config[:mpg_player] == :mpg123
474
+ end
475
+
472
476
  def player_cmd
473
- case @config[:mpg_player]
474
- when :mpg123
477
+ if use_mpg123?
475
478
  "mpg123 --remote-err -Cqk#{pre_delay}"
476
479
  else
477
480
  "afplay"
@@ -487,6 +490,84 @@ class InOurTime
487
490
  sleep delay
488
491
  end
489
492
 
493
+ def get_search_results search_term
494
+ distances = {}
495
+ @sorted_titles.each do |title|
496
+ title.split(' ').each do |word|
497
+ if distances[title].nil?
498
+ distances[title] = Levenshtein::distance(search_term, word)
499
+ else
500
+ distances[title] =
501
+ distances[title] >
502
+ Levenshtein::distance(search_term, word.downcase) ?
503
+ Levenshtein::distance(search_term, word.downcase) :
504
+ distances[title]
505
+ end
506
+ end
507
+ end
508
+ results = distances.sort_by{ |k, v| v }
509
+ results.shuffle! if search_term == ''
510
+ results
511
+ end
512
+
513
+ def print_search_results results
514
+ puts
515
+ puts "0: Search Again!"
516
+ puts
517
+ 5.times do |count|
518
+ print "#{count + 1}: "
519
+ puts results[count][0]
520
+ end
521
+ puts
522
+ end
523
+
524
+ def display_search_choice results, choice
525
+ @key = KeyboardEvents.new
526
+ begin
527
+ if choice.to_i > 0 && choice.to_i < 6
528
+ list_selected results[choice.to_i - 1][0]
529
+ return
530
+ end
531
+ end
532
+ redraw
533
+ end
534
+
535
+ def get_search_term
536
+ puts "Enter Search Term or just Return for Random:"
537
+ puts
538
+ print "Search Term: "
539
+ gets.chomp
540
+ end
541
+
542
+ def get_search_choice
543
+ print "Enter Choice: "
544
+ temp = gets.chomp
545
+ temp == '0' ? :search_again : temp
546
+ end
547
+
548
+ def search_init
549
+ @key.kill
550
+ sleep 0.2
551
+ STDIN.echo = true
552
+ STDIN.cooked!
553
+ clear
554
+ sleep 0.2
555
+ end
556
+
557
+ def search
558
+ choice = ''
559
+ results = []
560
+ loop do
561
+ search_init
562
+ search_term = get_search_term
563
+ results = get_search_results(search_term)
564
+ print_search_results(results)
565
+ choice = get_search_choice
566
+ break unless choice == :search_again
567
+ end
568
+ display_search_choice results, choice
569
+ end
570
+
490
571
  def run_program prg
491
572
  unless prg[:have_locally]
492
573
  retries = 0
@@ -561,7 +642,7 @@ class InOurTime
561
642
  end
562
643
 
563
644
  def control_play?
564
- @playing && (@config[:mpg_player] == :mpg123)
645
+ @playing && use_mpg123?
565
646
  end
566
647
 
567
648
  def forward
@@ -572,6 +653,12 @@ class InOurTime
572
653
  write_player ";" if control_play?
573
654
  end
574
655
 
656
+ def instructions
657
+ iot_print "Type", @system_colour
658
+ iot_print " h ", :light_green
659
+ iot_print "for instructions", @system_colour
660
+ end
661
+
575
662
  def print_playing_maybe
576
663
  if @playing
577
664
  iot_print("Playing: ", @count_colour) unless @paused
@@ -579,7 +666,7 @@ class InOurTime
579
666
  iot_puts @playing, @selection_colour
580
667
  elsif @started.nil?
581
668
  @started = true
582
- iot_print "? or h for instructions", @text_colour
669
+ instructions
583
670
  end
584
671
  end
585
672
 
@@ -654,40 +741,78 @@ class InOurTime
654
741
  def load_help_maybe
655
742
  return unless help_option?
656
743
  help
657
- quit
744
+ puts
745
+ exit 0
658
746
  end
659
747
 
660
748
  def help_screen
661
749
  [] <<
662
750
  " In Our Time Player (#{@version})" <<
663
751
  " " <<
664
- " Play/Stop - X or Enter " <<
752
+ " Play/Stop - X / Enter " <<
665
753
  " Next/Prev - Up / Down " <<
666
754
  " Next/Prev Page - Right / Left " <<
667
755
  " Sort - S " <<
756
+ " Search - ? " <<
668
757
  " Theme Toggle - T " <<
669
758
  " List Top/End - L " <<
670
759
  " Update - U " <<
671
760
  " Info - I " <<
672
761
  " Help - H " <<
673
762
  " Quit - Q " <<
674
- " mpg123 Control " <<
675
- " Pause/Resume - P / SPACEBAR " <<
763
+ " mpg123 Control - " <<
764
+ " Pause/Resume - P / Spacebar " <<
676
765
  " Forward Skip - F " <<
677
766
  " Reverse Skip - R " <<
678
767
  " " <<
679
768
  "Config: #{CONFIG} "
680
769
  end
681
770
 
771
+ def help_partial x,y; help_screen[x..y] end
772
+ 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
775
+
776
+ def help_mpg
777
+ scr = help_partial(13,16)
778
+ scr[0].rstrip! << ' (enabled) ' if use_mpg123?
779
+ scr[0].rstrip! << ' (disabled) ' unless use_mpg123?
780
+ scr
781
+ end
782
+
783
+ def help_colour scr
784
+ case scr
785
+ when :mpg
786
+ return @selection_colour unless use_mpg123?
787
+ @count_colour if use_mpg123?
788
+ when :cfg
789
+ @system_colour
790
+ else
791
+ @text_colour
792
+ end
793
+ end
794
+
795
+ def rstrip_maybe scr
796
+ self.rstrip unless scr == :mpg
797
+ end
798
+
799
+ def help_render scr
800
+ txt = send "help_#{scr}"
801
+ iot_puts txt.map{|x| x}.join("\n\r"), help_colour(scr)
802
+ end
803
+
682
804
  def print_help
683
- iot_puts help_screen.map{|x|x.rstrip}.join("\n\r"), @system_colour
805
+ help_render :title
806
+ help_render :main
807
+ help_render :mpg
808
+ help_render :cfg
684
809
  end
685
810
 
686
811
  def help
687
812
  unless @help
688
813
  clear_content
689
814
  print_help
690
- print_playing_maybe
815
+ print_playing_maybe unless help_option?
691
816
  @help = true
692
817
  render
693
818
  else
@@ -697,7 +822,7 @@ class InOurTime
697
822
  end
698
823
 
699
824
  def reformat info
700
- ['With','Guests','Producer','Contributors'].map do | x|
825
+ ['With','Guests','Producer','Contributors'].map do |x|
701
826
  [' ', ':'].map do |y|
702
827
  [x, x.upcase].map do |z|
703
828
  info.gsub!(z + y, "\n" + z + y)
@@ -888,7 +1013,7 @@ class InOurTime
888
1013
  case ip
889
1014
  when :pause, :forward, :rewind, :list_key, :page_forward, :page_back,
890
1015
  :previous, :next, :play, :sort_key, :theme_toggle, :update_key,
891
- :info, :help, :quit_key
1016
+ :info, :help, :quit_key, :search
892
1017
  self.send ip
893
1018
  end
894
1019
  end
@@ -67,36 +67,40 @@ class KeyboardEvents
67
67
  end
68
68
 
69
69
  def match_event str
70
- case str
71
- when "\e"
72
- @mode = :escape
73
- when "l",'L'
74
- @event = :list_key
75
- when "u",'U'
76
- @event = :update_key
77
- when ' '
78
- @event = :pause
79
- when "q",'Q', "\u0003", "\u0004"
80
- @event = :quit_key
81
- when 'p', 'P'
82
- @event = :pause
83
- when 'f', 'F'
84
- @event = :forward
85
- when 'r', 'R'
86
- @event = :rewind
87
- when 's', 'S'
88
- @event = :sort_key
89
- when 't', 'T'
90
- @event = :theme_toggle
91
- when 'x', 'X', "\r"
92
- @event = :play
93
- when 'i', 'I'
94
- @event = :info
95
- when '?', 'h', 'H'
96
- @event = :help
97
- else
98
- return @event = :no_event
99
- end
70
+ @event =
71
+ case str
72
+ when "\e"
73
+ @mode = :escape
74
+ :no_event
75
+ when "l",'L'
76
+ :list_key
77
+ when "u",'U'
78
+ :update_key
79
+ when ' '
80
+ :pause
81
+ when "q",'Q', "\u0003", "\u0004"
82
+ :quit_key
83
+ when 'p', 'P'
84
+ :pause
85
+ when 'f', 'F'
86
+ :forward
87
+ when 'r', 'R'
88
+ :rewind
89
+ when 's', 'S'
90
+ :sort_key
91
+ when 't', 'T'
92
+ :theme_toggle
93
+ when 'x', 'X', "\r"
94
+ :play
95
+ when 'i', 'I'
96
+ :info
97
+ when 'h', 'H'
98
+ :help
99
+ when '?'
100
+ :search
101
+ else
102
+ :no_event
103
+ end
100
104
  end
101
105
  end
102
106
 
@@ -0,0 +1,55 @@
1
+ module Levenshtein # :nodoc:
2
+ # This code is based directly on the Text gem implementation
3
+ # Copyright (c) 2006-2013 Paul Battley, Michael Neumann, Tim Fletcher.
4
+ #
5
+ # Returns a value representing the "cost" of transforming str1 into str2
6
+ def distance(str1, str2)
7
+ n = str1.length
8
+ m = str2.length
9
+ return m if n.zero?
10
+ return n if m.zero?
11
+
12
+ d = (0..m).to_a
13
+ x = nil
14
+
15
+ # to avoid duplicating an enumerable object, create it outside of the loop
16
+ str2_codepoints = str2.codepoints
17
+
18
+ str1.each_codepoint.with_index(1) do |char1, i|
19
+ j = 0
20
+ while j < m
21
+ cost = (char1 == str2_codepoints[j]) ? 0 : 1
22
+ x = min3(
23
+ d[j+1] + 1, # insertion
24
+ i + 1, # deletion
25
+ d[j] + cost # substitution
26
+ )
27
+ d[j] = i
28
+ i = x
29
+
30
+ j += 1
31
+ end
32
+ d[m] = x
33
+ end
34
+
35
+ x
36
+ end
37
+ module_function :distance
38
+
39
+ private
40
+
41
+ # detects the minimum value out of three arguments. This method is
42
+ # faster than `[a, b, c].min` and puts less GC pressure.
43
+ # See https://github.com/yuki24/did_you_mean/pull/1 for a performance
44
+ # benchmark.
45
+ def min3(a, b, c)
46
+ if a < b && a < c
47
+ a
48
+ elsif b < c
49
+ b
50
+ else
51
+ c
52
+ end
53
+ end
54
+ module_function :min3
55
+ 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.5.0
4
+ version: 0.6.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-11-11 00:00:00.000000000 Z
11
+ date: 2016-11-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: oga
@@ -73,6 +73,7 @@ files:
73
73
  - lib/in_our_time.rb
74
74
  - lib/iot/iot.rb
75
75
  - lib/iot/keyboard_events.rb
76
+ - lib/iot/levenshtein.rb
76
77
  homepage: https://github.com/mjago/In_Our_Time
77
78
  licenses:
78
79
  - MIT