Give more sense to shuffle-* and *-selected modes (#8)

This commit is contained in:
Nguyễn Gia Phong 2018-01-30 17:55:27 +07:00
parent f8185d1b31
commit be0eaeadb7
4 changed files with 46 additions and 31 deletions

18
comp
View File

@ -16,7 +16,7 @@
#
# Copyright (C) 2017 Nguyễn Gia Phong <vn.mcsinyx@gmail.com>
__version__ = '0.4.4'
__version__ = '0.4.5'
import curses
import json
@ -127,8 +127,7 @@ class Comp(Omp):
def setno(self, *keys):
"""Set all keys of each entry in entries to False."""
for entry in self.entries:
for key in keys:
entry[key] = False
for key in keys: entry[key] = False
def play(self, force=False):
"""Play the next track."""
@ -217,19 +216,6 @@ class Comp(Omp):
def __enter__(self): return self
def idx(self, entry=None):
"""Return the index of the current entry."""
if entry is None:
return self.start + self.y - 1
return self.entries.index(entry)
def current(self):
"""Return the current entry."""
try:
return self.entries[self.idx()]
except:
return {}
def read_input(self, prompt):
"""Print the prompt string at the bottom of the screen then read
from standard input.

View File

@ -17,6 +17,7 @@
# Copyright (C) 2017 Nguyễn Gia Phong <vn.mcsinyx@gmail.com>
import json
from bisect import bisect_left as bisect
from collections import deque
from gettext import bindtextdomain, gettext as _, textdomain
from itertools import cycle
@ -38,6 +39,15 @@ bindtextdomain('omp', resource_filename('omp', 'locale'))
textdomain('omp')
def shuffle_init(a):
"""Return in iterator which yield random elements from a,
and always begin with its first element.
"""
if a:
yield a[0]
while True: yield choice(a)
class Omp(object):
"""Omni Media Player meta object.
@ -119,29 +129,48 @@ class Omp(object):
_("Failed to cycle {} '{}'").format(direction, name),
error=True)
def update_play_list(self, pick):
"""Update the list of entries to be played."""
if pick == 'current':
self.play_list = [self.current()]
elif pick == 'all':
self.play_list = deque(self.entries)
self.play_list.rotate(-self.idx())
else:
self.play_list = [i for i in self.entries if i.get('selected')]
def idx(self, entry=None):
"""Return the index of the current entry."""
if entry is None:
return self.start + self.y - 1
return self.entries.index(entry)
def current(self):
"""Return the current entry."""
try:
return self.entries[self.idx()]
except:
return {}
def update_playlist(self):
"""Update the playlist to be used by play function."""
action, pick = self.mode.split('-')
self.update_play_list(pick)
if pick == 'current':
self.play_list = deque([self.current()])
elif pick == 'all':
self.play_list = deque(self.entries)
self.play_list.rotate(-self.idx())
elif pick == 'selected':
self.play_list = deque([entry for entry in self.entries
if entry.get('selected')])
indexes = [i for i, entry in enumerate(self.entries)
if entry.get('selected')]
idx = indexes[bisect(indexes, self.idx())]
self.play_list.rotate(-self.play_list.index(self.entries[idx]))
if action == 'play':
self.playlist = iter(self.play_list)
elif action == 'repeat':
self.playlist = cycle(self.play_list)
else:
self.playlist = iter(lambda: choice(self.play_list), None)
elif action == 'shuffle':
self.playlist = shuffle_init(self.play_list)
if self.playing < -1: self.played = self.played[:self.playing+1]
def next(self, force=False, backward=False):
"""Go forward/backward in the playlist.
If forced, this will also unpause the player.
"""
self.play_backward = backward
if self.mp.idle_active:
self.play(force)

View File

@ -7,7 +7,7 @@ with open('README.rst') as f:
setup(
name='comp',
version='0.4.4',
version='0.4.5',
description=('Curses Omni Media Player'),
long_description=long_description,
url='https://github.com/McSinyx/comp',

View File

@ -140,7 +140,7 @@
"title": "Imagine Dragons - Believer"
},
{
"duration": "00:00:00",
"duration": "00:03:52",
"error": false,
"filename": "https://youtu.be/A-Rn0iQEpc8",
"selected": false,
@ -168,7 +168,7 @@
"title": "SUSE. Yes Please. (Maroon 5 - Sugar parody)"
},
{
"duration": "00:00:00",
"duration": "00:03:30",
"error": false,
"filename": "https://youtu.be/oHNKTlz1lps",
"selected": true,