Skip to content

Commit

Permalink
Statusline introduction and assorted fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
alyosha committed Sep 9, 2020
1 parent f8f8304 commit 1c2714b
Show file tree
Hide file tree
Showing 6 changed files with 119 additions and 16 deletions.
11 changes: 8 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
## vimedia
Control your media players from inside Vim, if you're really
that lazy 😏
that lazy

![vimedia statusline](statusline.png)

In addition to displaying track/artist information in your statusline, you can also navigate
through your media libraries, control volume, etc. using the following commands.

## Usage
- `:Play` => Begin playback from active media player
Expand Down Expand Up @@ -31,7 +36,7 @@ from within Vim to get a list of options (remember the players need to be runnin

## Coming soon
- Seek forward/backwards
- Optional status bar
- Make status bar optional

Suggestions/contributions welcome.

Expand All @@ -40,4 +45,4 @@ Using your preferred plugin manager or if all else fails:

`git clone https://github.com/alyosha/vimedia ~/.vim/bundle/vimedia`

You may need to install a newer version of Vim if yours wasn't built with python3.
You may need to install a newer version of Vim if yours wasn't built with python3.
60 changes: 54 additions & 6 deletions plugin/vimedia.vim
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,12 @@ endif
"" Point to location of python code
let s:plugin_root_dir = fnamemodify(resolve(expand('<sfile>:p')), ':h')

"" Set the users default media player if present
let s:selected_player_suffix = $DEFAULT_VIMEDIA_PLAYER
fu! s:init_player_config()
let s:selected_player_suffix = $DEFAULT_VIMEDIA_PLAYER
let s:selected_player_configured = 0
endfu

call s:init_player_config()

python3 << EOF
import sys
Expand All @@ -27,6 +31,49 @@ import util
vmd = vimedia.Vimedia()
EOF

" *************************************************************************** "
" *********************** Background Functions ************************** "
" *************************************************************************** "

let s:current_track_name = "N/A"
let s:current_artist_name = "N/A"

fu! s:Refresh(timer)
if s:selected_player_configured == 0
return
endif

python3 vmd.selected_player.refresh_now_playing()

if s:current_track_name == "N/A" || s:current_artist_name == "N/A"
return
endif

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

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

fu! s:CheckPlayerLiveness(timer)
if s:selected_player_suffix != "" && !s:selected_player_configured
python3 vmd = vimedia.Vimedia()
endif
endfu

"" Check each second to get currently playing track/artist name.
let timer = timer_start(1000, function('s:Refresh'), {'repeat':-1})

"" Check every ten seconds to auto-detect default player in case it
"" becomes active at some point after the Vim window has been opened.
let timer = timer_start(10000, function('s:CheckPlayerLiveness'), {'repeat':-1})

" *************************************************************************** "
" ************************* Base Functionality ************************** "
" *************************************************************************** "

let s:interaction_type_select_player = "select_player_interaction"
let s:interaction_type_toggle_volume = "toggle_volume_interaction"

Expand Down Expand Up @@ -69,7 +116,7 @@ endfu

fu! s:ActivePlayer() abort
if s:selected_player_suffix != ""
echom s:selected_player_suffix
echom s:selected_player_configured == 1 ? s:selected_player_suffix : s:selected_player_suffix . " selected but not active"
else
echom "No media player configured"
endif
Expand All @@ -85,7 +132,8 @@ endfu

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

fu! s:PresentOptions(interaction_type) abort
Expand Down Expand Up @@ -125,8 +173,8 @@ fu! s:ToggleVolume() abort
endfu

fu! s:CheckPlayer(fn, ...) abort
if s:selected_player_suffix == ""
echom "Please select a media player"
if s:selected_player_configured == 0
echom "Please select an active media player"
return
endif
call a:fn()
Expand Down
57 changes: 50 additions & 7 deletions python/player.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from mpris import Mpris
from util import to_vim_string
from time import sleep
import vim
import dbus
Expand All @@ -22,18 +23,26 @@ def pause(self):
self.iface.Pause()

def next(self):
self.iface.Next()
try:
self.iface.Next()
except:
return

def previous(self):
start_track = self.get_title()
self.iface.Previous()
try:
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:
self.iface.Previous()
return
try:
self.iface.Previous()
except:
return

def restart(self):
start_track = self.get_title()
Expand All @@ -46,21 +55,55 @@ def restart(self):
return
self.iface.Next()

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

if metadata != None:
try:
artist = metadata["xesam:artist"][0]
except:
return ""

return str(artist)

def get_title(self):
metadata = self.get_metadata()
return str(metadata["xesam:title"])

if metadata != None:
try:
title = metadata["xesam:title"]
except:
return ""

return str(title)

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

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

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

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')
vim.command('let s:previous_volume = ' + str(previous_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')
Expand Down
4 changes: 4 additions & 0 deletions python/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,7 @@ def normalize_player_name(dbus_name):
return CHROMIUM_PATTERN

return name


def to_vim_string(val):
return '\"' + val + '\"'
3 changes: 3 additions & 0 deletions python/vimedia.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ def __init__(self):
self.base = Base(selected_player_name)
self.selected_player = Player(selected_player_name)

if self.selected_player:
vim.command('let s:selected_player_configured = ' + str(1))

def pause_all(self, exclude_selected):
selected_player_name = get_selected_player_name()

Expand Down
Binary file added 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 1c2714b

Please sign in to comment.