More work with command line

git-svn-id: file:///home/anna/Desktop/ocarina-legacy/mithos/ocarina@2 1daee41c-8060-4895-b1f0-2197c00d777a
This commit is contained in:
bjschuma 2009-05-27 23:16:56 +00:00
parent e03e553a51
commit ed7469a646
4 changed files with 169 additions and 39 deletions

View File

@ -13,9 +13,11 @@ class CLine(threading.Thread):
self.input = "" self.input = ""
self.pos = (0,0) # REMEMBER: (y,x) self.pos = (0,0) # REMEMBER: (y,x)
self.start() self.start()
self.cmnds = dict()
# Reverse ncurses stuff # Reverse ncurses stuff (returns screen to normal)
# THIS MUST BE CALLED BEFORE EXITING!!!
def quit(self): def quit(self):
curses.nocbreak() curses.nocbreak()
curses.echo() curses.echo()
@ -25,7 +27,9 @@ class CLine(threading.Thread):
# Print the prompt # Print the prompt
def prompt(self): def prompt(self):
self.stdscr.addstr(self.pos[0],0,">>> ") (y,x) = self.stdscr.getyx()
self.stdscr.addstr(y,0,">>> ")
self.stdscr.refresh()
# Run the command line # Run the command line
@ -33,12 +37,12 @@ class CLine(threading.Thread):
try: try:
self.prompt() self.prompt()
while(True): while(True):
#self.prompt() # Enter key pressed
self.pos=self.stdscr.getyx()
c = self.stdscr.getch() c = self.stdscr.getch()
if c==10: if c==10:
if self.enter()==False: self.enter()
return elif c==127:
self.backspace()
else: else:
self.input+=curses.keyname(c) self.input+=curses.keyname(c)
self.changeline() self.changeline()
@ -49,13 +53,37 @@ class CLine(threading.Thread):
# Enter key pressed # Enter key pressed
# Returns true if we keep going # Returns true if we keep going
def enter(self): def enter(self):
self.input = self.input.strip().lower()
if self.input=="": if self.input=="":
return True return
elif self.input=="quit": elif self.input in self.cmnds.keys():
self.quit() self.cmnds[self.input][0]()
return False self.advanceLine()
else: self.prompt()
self.input = "" self.input = ""
elif self.input == "help":
self.printHelp()
self.input = ""
# Backspace key pressed
def backspace(self):
(y,x) = self.stdscr.getyx()
if self.input != "":
self.stdscr.move(y,x-1)
self.stdscr.delch()
self.input=self.input[0:len(self.input)-1]
# Advance cursor to next line
def advanceLine(self):
(y,x) = self.stdscr.getyx()
if y==self.maxSize[0]-1:
self.stdscr.move(0,0)
self.stdscr.deleteln()
else:
y+=1
self.stdscr.move(y,0)
# Change the current line to reflect self.input # Change the current line to reflect self.input
@ -65,4 +93,36 @@ class CLine(threading.Thread):
self.stdscr.move(y,0) self.stdscr.move(y,0)
self.prompt() self.prompt()
self.stdscr.addstr(self.input) self.stdscr.addstr(self.input)
#self.stdscr.move(y,x+10)
# Print lines to the screen
def printLines(self,lines):
#self.advanceLine()
for line in lines:
self.printLine(line)
self.advanceLine()
self.prompt()
def printLine(self,line):
(y,x) = self.stdscr.getyx()
self.advanceLine()
(y,x) = self.stdscr.getyx()
self.stdscr.addstr(line)
#self.stdscr.move(y+1,x)
self.stdscr.refresh()
# Associate a command with a function
def register(self,command,func,help):
self.cmnds[command]=(func,help)
def printHelp(self):
lines = []
keys = self.cmnds.keys()
keys.sort()
for cmnd in keys:
lines+=[cmnd+"\t"+self.cmnds[cmnd][1]]
self.printLines(lines)

View File

@ -1,7 +1,6 @@
class Duration: class Duration:
def __init__(self): def __init__(self):
self.set = False
self.hour=0 self.hour=0
self.min=0 self.min=0
self.sec=0 self.sec=0
@ -26,5 +25,30 @@ class Duration:
# Write time to screen # Write time to screen
def disp(self): def toStr(self):
print self.hour,self.min,self.sec time = ""
if self.hour > 0:
time+=str(self.hour)+":"
if self.min > 0:
min = str(self.min)
if self.min < 10:
min = "0"+min
time+=min
else:
time+="00"
time+=":"
if self.sec > 0:
sec = str(self.sec)
if self.sec < 10:
sec = "0"+sec
time += sec
else:
time+="00"
return time
def disp(self,prnt):
time = self.toStr()
prnt([time])

View File

@ -4,16 +4,60 @@ import thread
from song import Song from song import Song
from cline import CLine from cline import CLine
from duration import Duration
#import cmnds
def main(argv): class main:
commands = CLine() def __init__(self,argv):
if len(argv) == 0:
print "python ocarina.py /path/to/song/"
self.commands = CLine()
self.registerCmnds()
s = Song(argv[0]) lines = ["test","testing","final test"]
s.play() self.commands.printLines(lines)
self.song = Song(argv[0],self.quit,self.commands.printLines)
self.song.play()
# Start main loop as a thread so we can get bus calls and use command line
gobject.threads_init()
mainloop = gobject.MainLoop()
thread.start_new_thread(mainloop.run,())
def quit(self):
self.commands.quit()
print "Quitting..."
sys.exit(0)
def registerCmnds(self):
self.commands.register("quit",self.quit,"Exit ocarina")
self.commands.register("exit",self.quit,"Exit ocarina")
self.commands.register("foo",self.foo,"a test function")
self.commands.register("play",self.play,"Play current song")
self.commands.register("pause",self.pause,"Pause current song")
self.commands.register("time",self.time,"Display running time")
def foo(self):
self.commands.printLine("foo...")
# Start main loop as a thread
mainloop = gobject.MainLoop() def play(self):
thread.start_new_thread(mainloop.run,()) self.song.play()
def pause(self):
self.song.pause()
def time(self):
cur = self.song.curTime()
tot = self.song.length
self.commands.printLine(cur.toStr()+" / "+tot.toStr())
if __name__=='__main__':main(sys.argv[1:]) if __name__=='__main__':main(sys.argv[1:])

View File

@ -1,12 +1,15 @@
import pygst,sys,time,gobject import sys
import time
import pygst
pygst.require("0.10") pygst.require("0.10")
import gst import gst
import gst
from duration import Duration from duration import Duration
class Song(): class Song():
def __init__(self,file): def __init__(self,file,exitFunc,prnt):
self.quit=exitFunc
self.prnt=prnt
# initialize player pipeline # initialize player pipeline
self.player = gst.Pipeline("player") self.player = gst.Pipeline("player")
bin = gst.element_factory_make("playbin",None) bin = gst.element_factory_make("playbin",None)
@ -24,35 +27,32 @@ class Song():
# Initialize stuff for finding duration # Initialize stuff for finding duration
self.time_format = gst.Format(gst.FORMAT_TIME) self.time_format = gst.Format(gst.FORMAT_TIME)
self.length = None self.length = None
# Start mainloop
gobject.threads_init()
# Called on bus messages # Called on bus messages
def onMessage(self,bus,message): def onMessage(self,bus,message):
t = message.type t = message.type
if t == gst.MESSAGE_EOS: if t == gst.MESSAGE_EOS:
print "End of stream" #print "End of stream"
sys.exit(0) self.prnt("End of stream")
self.quit()
elif t == gst.MESSAGE_ERROR: elif t == gst.MESSAGE_ERROR:
print "Error" err, debug = message.parse_error()
sys.exit(0) self.prnt("Error: %s" % err,debug)
#print "Error: %s" % err, debug
self.quit()
elif t == gst.MESSAGE_TAG: elif t == gst.MESSAGE_TAG:
print "Tags:" #print "Tags:"
# Probably should convert this to some other data structure # Probably should convert this to some other data structure
# For easier use later # For easier use later
taglist = message.parse_tag() taglist = message.parse_tag()
for key in taglist.keys():
print key,taglist[key]
return return
# Change state to "playing" # Change state to "playing"
def play(self): def play(self):
self.player.set_state(gst.STATE_PLAYING) self.player.set_state(gst.STATE_PLAYING)
# Start main loop and find duration # Start main loop and find duration (if this hasn't been done yet)
# If this hasn't been done yet
if self.length == None: if self.length == None:
time.sleep(0.2) time.sleep(0.2)
self.duration() self.duration()
@ -68,10 +68,12 @@ class Song():
self.length = Duration() self.length = Duration()
length = self.player.query_duration(self.time_format,None)[0] length = self.player.query_duration(self.time_format,None)[0]
self.length.setTime(length) self.length.setTime(length)
self.length.disp() #self.length.disp(self.prnt)
# Print out current running time # Print out current running time
def curTime(self): def curTime(self):
length = self.player.query_position(self.time_format,None)[0] length = self.player.query_position(self.time_format,None)[0]
return Duration().setTime(length) dur = Duration()
dur.setTime(length)
return dur