Skip to content

Commit

Permalink
Add playback ticker to statusline
Browse files Browse the repository at this point in the history
  • Loading branch information
alyosha committed Sep 10, 2020
1 parent 97163e0 commit 2b58e84
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 37 deletions.
12 changes: 11 additions & 1 deletion plugin/vimedia.vim
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ EOF
fu! s:init_now_playing_config()
let s:current_track_name = "N/A"
let s:current_artist_name = "N/A"
let s:ticker_microseconds = 0
endfu

call s:init_now_playing_config()
Expand All @@ -55,12 +56,21 @@ fu! s:Refresh(timer)

set statusline=
set statusline+=\%{NowPlayingText()}
set statusline+=%=
set statusline+=\%{PlaybackTicker()}
endfu

fu! NowPlayingText()
return s:current_track_name . " - " . s:current_artist_name
endfu

fu! PlaybackTicker()
let l:pos_seconds = s:ticker_microseconds / 1000000
let l:min = l:pos_seconds / 60
let l:sec = l:pos_seconds - (l:min * 60)
return l:min . ":" . (l:sec > 9 ? l:sec : ("0" . l:sec))
endfu

fu! s:CheckPlayerLiveness(timer)
if s:selected_player_suffix != "" && !s:selected_player_configured
python3 vmd = vimedia.Vimedia()
Expand Down Expand Up @@ -136,7 +146,7 @@ endfu

fu! s:Quit() abort
python3 vmd.base.quit()
s:init_player_config()
call s:init_player_config()
set statusline=
endfu

Expand Down
72 changes: 37 additions & 35 deletions python/player.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,18 @@ class Player(Mpris):
def __init__(self, name):
super().__init__(name)

def refresh_now_playing(self):
title = self.get_title()
artist = self.get_artist()
pos = self.get_position()

if title == "" or artist == "" or pos == 0:
return

vim.command('let s:current_track_name = ' + to_vim_string(title))
vim.command('let s:current_artist_name = ' + to_vim_string(artist))
vim.command('let s:ticker_microseconds = ' + str(pos))

def play(self):
try:
self.iface.Play()
Expand All @@ -34,9 +46,6 @@ def previous(self):
self.iface.Previous()
except:
return
# Many players will simply restart the song if the position
# is already past a certain point. We have a dedicated method
# supporting restart, so make sure to actually go back here.
sleep(0.5) # Need to sleep a bit to ensure new metadata has arrived.
if self.get_title() == start_track:
try:
Expand All @@ -46,15 +55,31 @@ def previous(self):

def restart(self):
start_track = self.get_title()
# Not all implementations of MediaPlayer are perfect. Chromium
# for example lacks trackid metadata, making SetPosition impossible.
# For this reason we hack temporarily to prioritize universal support.
self.iface.Previous()
sleep(0.5) # Need to sleep a bit or the Next call will be ignored.
if self.get_title() == start_track:
return
self.iface.Next()

def set_volume(self, value):
self.set_property('Volume', value)

def adjust_volume(self, value):
previous_volume = self.get_property('Volume')
self.set_property('Volume', previous_volume + value)

# If these are equal, volume must not be configurable for the player.
if self.get_property('Volume') != previous_volume:
vim.command('let s:previous_volume = ' + str(previous_volume))

def shuffle(self):
try:
previous_status = self.get_property('Shuffle')
self.set_property('Shuffle', not previous_status)
print("Shuffle status: " + ("on" if previous_status == 0 else "off"))
except:
print(self.name + " has not implemented Shuffle yet")

def get_artist(self):
metadata = self.get_metadata()

Expand All @@ -77,37 +102,14 @@ def get_title(self):

return str(title)

def get_metadata(self):
def get_position(self):
try:
return self.get_property('Metadata')
return int(self.get_property('Position'))
except:
return None

def refresh_now_playing(self):
title = self.get_title()
artist = self.get_artist()

if title == "" or artist == "":
return
return 0

vim.command('let s:current_track_name = ' + to_vim_string(title))
vim.command('let s:current_artist_name = ' + to_vim_string(artist))

def set_volume(self, value):
self.set_property('Volume', value)

def adjust_volume(self, value):
previous_volume = self.get_property('Volume')
self.set_property('Volume', previous_volume + value)

# If these are equal, volume must not be configurable for the player.
if self.get_property('Volume') != previous_volume:
vim.command('let s:previous_volume = ' + str(previous_volume))

def shuffle(self):
def get_metadata(self):
try:
previous_status = self.get_property('Shuffle')
self.set_property('Shuffle', not previous_status)
print("Shuffle status: " + ("on" if previous_status == 0 else "off"))
return self.get_property('Metadata')
except:
print(self.name + " has not implemented Shuffle yet")
return None
2 changes: 1 addition & 1 deletion python/vimedia.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from player import Player
from base import Base
from util import get_selected_player_name, get_active_player_names
from util import get_selected_player_name, get_active_player_names, to_vim_string
import vim
import dbus

Expand Down
Binary file modified statusline.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 2b58e84

Please sign in to comment.