Class: Rubio::Model::Player

Inherits:
Object
  • Object
show all
Defined in:
lib/rubio/model/player.rb

Constant Summary collapse

CURRENTLY_PLAYING_NONE =
'None'
CURRENTLY_PLAYING_LENGTH_PER_LINE =
90

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(backend = 'vlc -I rc', show_currently_playing: true) ⇒ Player

Returns a new instance of Player.



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/rubio/model/player.rb', line 13

def initialize(backend = 'vlc -I rc', show_currently_playing: true)
  raise unless backend.is_a?(String)

  @backend = backend
  @show_currently_playing = show_currently_playing
  @pid = nil
  @thr = nil
  @status = {}
  @history = []
  self.currently_playing = CURRENTLY_PLAYING_NONE
end

Instance Attribute Details

#backendObject

Returns the value of attribute backend.



9
10
11
# File 'lib/rubio/model/player.rb', line 9

def backend
  @backend
end

#currently_playingObject

Returns the value of attribute currently_playing.



10
11
12
# File 'lib/rubio/model/player.rb', line 10

def currently_playing
  @currently_playing
end

#historyObject

Returns the value of attribute history.



9
10
11
# File 'lib/rubio/model/player.rb', line 9

def history
  @history
end

#pidObject

Returns the value of attribute pid.



9
10
11
# File 'lib/rubio/model/player.rb', line 9

def pid
  @pid
end

#show_currently_playingObject (readonly) Also known as: show_currently_playing?

Returns the value of attribute show_currently_playing.



10
11
12
# File 'lib/rubio/model/player.rb', line 10

def show_currently_playing
  @show_currently_playing
end

#statusObject

Returns the value of attribute status.



9
10
11
# File 'lib/rubio/model/player.rb', line 9

def status
  @status
end

#thrObject

Returns the value of attribute thr.



9
10
11
# File 'lib/rubio/model/player.rb', line 9

def thr
  @thr
end

Instance Method Details

#alive?Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/rubio/model/player.rb', line 31

def alive?
  @thr&.alive? || (@io && !@io.closed?)
end

#continuously_fetch_currently_playingObject



63
64
65
66
67
68
69
70
71
72
# File 'lib/rubio/model/player.rb', line 63

def continuously_fetch_currently_playing
  return if @continuously_fetching_currently_playing

  @continuously_fetching_currently_playing = true

  Glimmer::LibUI.timer(1) do
    update_currently_playing
    @continuously_fetching_currently_playing
  end
end

#currently_playing_textObject



80
81
82
83
84
85
86
87
# File 'lib/rubio/model/player.rb', line 80

def currently_playing_text
  currently_playing_info = info
  if currently_playing_info && !currently_playing_info.strip.empty?
    [@playing_station_name, currently_playing_info].join(' - ')
  else
    @playing_station_name
  end
end

#infoObject



89
90
91
92
93
94
95
96
97
# File 'lib/rubio/model/player.rb', line 89

def info
  result = io_command('info')
  now_playing_line = result.lines.find {|l| l.include?('now_playing:')}
  if now_playing_line
    now_playing_line.split('now_playing:').last.chomp.strip
  end
rescue
  nil
end

#play(url, station_name: 'N/A') ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/rubio/model/player.rb', line 39

def play(url, station_name: 'N/A')
  # Do not include spaces in the command line
  # if a space exist :
  #   * sh -c command url # this process with @pid will be killed
  #   * cmmand url        # will not be killd because pid is differennt
  # if no space :
  #   * cmmand url        # will be killed by @pid
  raise if url.match(/\s/)

  @playing_station_name = station_name

  if show_currently_playing? && backend == 'vlc -I rc' && !OS.windows?
    @io = IO.popen("#{backend} \"#{url}\"", 'r+')
    update_currently_playing
    continuously_fetch_currently_playing
  else
    @pid = spawn(*backend.split(' '), url)
    @thr = Process.detach(@pid)
  end

  @status = { thr: @thr, pid: @pid, io: @io }
  @history << @status
end

#stop(thr: nil, pid: nil, io: nil) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/rubio/model/player.rb', line 99

def stop(thr: nil, pid: nil, io: nil)
  thr ||= @thr
  pid ||= @pid
  io  ||= @io
  if thr&.alive?
    r = Process.kill(OS.windows? ? :KILL : :TERM, pid)
  elsif io && !io.closed?
    io.close
    r = io.closed?
    self.currently_playing = CURRENTLY_PLAYING_NONE
  end
  thr = nil
  pid = nil
  io = nil
  r
end

#stop?Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/rubio/model/player.rb', line 35

def stop?
  @thr.nil? || @thr.stop?
end

#stop_allObject



116
117
118
119
# File 'lib/rubio/model/player.rb', line 116

def stop_all
  @continuously_fetching_currently_playing = false
  @history.each { |history| stop(**history) }
end

#update_currently_playingObject



74
75
76
77
78
# File 'lib/rubio/model/player.rb', line 74

def update_currently_playing
  Glimmer::LibUI.queue_main do
    self.currently_playing = currently_playing_text if alive?
  end
end