Class: TerminalPlayer
- Inherits:
-
Object
- Object
- TerminalPlayer
- Defined in:
- lib/terminal_player.rb
Instance Method Summary collapse
- #google(s) ⇒ Object
-
#initialize(options) ⇒ TerminalPlayer
constructor
A new instance of TerminalPlayer.
- #keypress_handler ⇒ Object
- #list_channels ⇒ Object
- #play ⇒ Object
- #refresh_display ⇒ Object
- #update(time, songs, force = false, is_refresh = false) ⇒ Object
Constructor Details
#initialize(options) ⇒ TerminalPlayer
Returns a new instance of TerminalPlayer.
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/terminal_player.rb', line 12 def initialize() @last_log = '' @last_di_fetch = 0 @recent_songs = [] @stop_updating = false @options = if @options[:url]['di.fm'] @site = DI.new(@options) elsif @options[:url]['somafm.com'] @site = Soma.new(@options) elsif @options[:url][':'] @site = Spotiphy.new(@options) else fail "no url" end if @options[:url]['channels.pls'] list_channels puts "\n\n" exit end @site.add_observer(self) end |
Instance Method Details
#google(s) ⇒ Object
88 89 90 |
# File 'lib/terminal_player.rb', line 88 def google(s) `open "https://www.google.com/search?safe=off&q=#{s}"` end |
#keypress_handler ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/terminal_player.rb', line 38 def keypress_handler Thread.new do loop do begin state = `stty -g` `stty raw -echo -icanon isig` str = STDIN.getc ensure `stty #{state}` end ch = str.chr case ch when 'c' @stop_updating = true list_channels @stop_updating = false update(Time.now, @site.songs, true, true) unless @site.is_di_plus when 'n' @site.player.next if @site.is_spotify when 'r' update(Time.now, @site.songs, true, true) when 's' if @options[:spotify_search] s = cleanup(@site.songs.last) `open "spotify:search:#{s}"` end when 'S' google @site.songs.last when '9', '0' # volume @site.player.write ch if @site.is_mplayer? when ' ' # pause/resume @site.player.write ch if @site.is_mplayer? end sleep 0.2 end end end |
#list_channels ⇒ Object
131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/terminal_player.rb', line 131 def list_channels puts "\n\n" channels = @site.get_channels chans = channels.map { |c| c[:name] }.join("\n") if `which column`.empty? puts chans else puts `echo "#{chans}" | column` end puts "\n" end |
#play ⇒ Object
92 93 94 95 96 |
# File 'lib/terminal_player.rb', line 92 def play refresh_display if @site.is_di_plus keypress_handler @site.play end |
#refresh_display ⇒ Object
77 78 79 80 81 82 83 84 85 86 |
# File 'lib/terminal_player.rb', line 77 def refresh_display Thread.new do loop do unless @site.songs.nil? update(Time.now, @site.songs, true, true) end sleep 1 end end end |
#update(time, songs, force = false, is_refresh = false) ⇒ Object
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/terminal_player.rb', line 98 def update(time, songs, force=false, is_refresh=false) return if @stop_updating @stop_updating = true begin if @last_log != songs.last || force unless songs.last.nil? @last_log = song = songs.last cols = `tput cols`.to_i preamble = "[#{@site.name}/#{@site.current_channel}]" if @site.class == DI && @site.is_di_plus extras = get_di_info else extras = time.strftime('%H:%M:%S') end while (1 + preamble.length + extras.length + song.length) > cols song = song[0..-2] end spaces = ' ' * (cols - song.length - preamble.length - extras.length - 1) song = "#{song}#{spaces}#{extras}" print "\n" unless is_refresh print "#{preamble} #{song}\r" unless force || @options[:play_history_path].empty? PlayHistory.write @options[:play_history_path], "#{time.strftime("%H:%M:%S")} #{preamble} #{songs.last}" end end end rescue => e write "update error: #{e}" end @stop_updating = false end |