view vixm/ui.py @ 6:50745af6a63b

Print song lengths in something better than number of milliseconds, added ":lists" command to list all the playlists
author Josef "Jeff" Sipek <jeffpc@josefsipek.net>
date Fri, 18 Aug 2006 20:32:00 -0400
parents 187453f856a7
children f87b969fa973
line wrap: on
line source

# all the user interface related bits

import time, sys
from threading import Thread
import xmms

import playlist, song, util

def run():
	""" this is where we start execution """

	# first, let's create the two playlists
	print "Creating playlists..."
	lists = {}
	lists[playlist.LIST_PRIO]	= playlist.playlist()
	lists[playlist.LIST_DEFAULT]	= playlist.playlist(allowrandom=True)

	# read in the info for all the songs in XMMS's playlist
	print "Loading songs from XMMS's playlist..."
	songs = []
	listlength = xmms.control.get_playlist_length()
	for i in range(0,listlength):
		s = song.song(i)
		lists[playlist.LIST_DEFAULT].enqueue(s)
	
	print "Instanciating ui thread..."
	ui = uiThread(lists)
	ui.start()

	while not ui.shutdown:
		time.sleep(1)

class uiThread(Thread):
	""" This is the main ui thread class, it does all the magic
	necessary to have a vi-like interface """
	def __init__(self, lists):
		Thread.__init__(self)

		self.lists = lists
		self.shutdown = False

	def __enqueue(self, id):
		s = self.lists[playlist.LIST_DEFAULT][id]
		print "Enqueuing song: %d. %s (%s)" % (id, s["title"],
				util.strtime(s["time"]))
		self.lists[playlist.LIST_PRIO].enqueue(s)

	def __cmd(self, txt):
		parts = txt.split()
		cmd = parts[0]

		if cmd == "quit":
			self.shutdown = True
		elif cmd == "list":
			i = 1

			if len(parts) > 1:
				try:
					listid = int(parts[1])
					if listid < 0 or listid >= len(self.lists):
						raise ValueError
				except:
					print "Invalid list number"
					return
			else:
				listid = playlist.LIST_PRIO

			for s in self.lists[listid]:
				print "%d. %s (%s)" % (i, s["title"],
						util.strtime(s["time"]))
				i += 1
		elif cmd == "lists":
			print "#0 LIST_PRIO"
			print "#1 LIST_DEFAULT"
		else:
			print "Invalid command \"%s\"" % (cmd,)

	def run(self):
		while not self.shutdown:
			tmp = sys.stdin.readline().strip()
			
			if tmp.startswith(":"):
				# ':NNN'
				try:
					id = int(tmp[1:])
				except:
					self.__cmd(tmp[1:])
					continue
				self.__enqueue(id)

			elif tmp.startswith("/"):
				# '/ABC'
				print "Searching not yet implemented"
			else:
				print "Unable to parse command \"%s\"" % (tmp,)