From ed7469a64690175309561090c47f1667a5c1e3ae Mon Sep 17 00:00:00 2001 From: bjschuma Date: Wed, 27 May 2009 23:16:56 +0000 Subject: [PATCH] More work with command line git-svn-id: file:///home/anna/Desktop/ocarina-legacy/mithos/ocarina@2 1daee41c-8060-4895-b1f0-2197c00d777a --- trunk/cline.py | 84 ++++++++++++++++++++++++++++++++++++++++------- trunk/duration.py | 30 +++++++++++++++-- trunk/ocarina.py | 58 ++++++++++++++++++++++++++++---- trunk/song.py | 36 ++++++++++---------- 4 files changed, 169 insertions(+), 39 deletions(-) diff --git a/trunk/cline.py b/trunk/cline.py index 82d63b84..2f524ccf 100644 --- a/trunk/cline.py +++ b/trunk/cline.py @@ -13,9 +13,11 @@ class CLine(threading.Thread): self.input = "" self.pos = (0,0) # REMEMBER: (y,x) self.start() + self.cmnds = dict() - # Reverse ncurses stuff + # Reverse ncurses stuff (returns screen to normal) + # THIS MUST BE CALLED BEFORE EXITING!!! def quit(self): curses.nocbreak() curses.echo() @@ -25,7 +27,9 @@ class CLine(threading.Thread): # Print the prompt 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 @@ -33,12 +37,12 @@ class CLine(threading.Thread): try: self.prompt() while(True): - #self.prompt() - self.pos=self.stdscr.getyx() + # Enter key pressed c = self.stdscr.getch() if c==10: - if self.enter()==False: - return + self.enter() + elif c==127: + self.backspace() else: self.input+=curses.keyname(c) self.changeline() @@ -49,13 +53,37 @@ class CLine(threading.Thread): # Enter key pressed # Returns true if we keep going def enter(self): + self.input = self.input.strip().lower() if self.input=="": - return True - elif self.input=="quit": - self.quit() - return False - else: + return + elif self.input in self.cmnds.keys(): + self.cmnds[self.input][0]() + self.advanceLine() + self.prompt() 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 @@ -65,4 +93,36 @@ class CLine(threading.Thread): self.stdscr.move(y,0) self.prompt() 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) diff --git a/trunk/duration.py b/trunk/duration.py index e1619d77..0a435783 100644 --- a/trunk/duration.py +++ b/trunk/duration.py @@ -1,7 +1,6 @@ class Duration: def __init__(self): - self.set = False self.hour=0 self.min=0 self.sec=0 @@ -26,5 +25,30 @@ class Duration: # Write time to screen - def disp(self): - print self.hour,self.min,self.sec + def toStr(self): + 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]) diff --git a/trunk/ocarina.py b/trunk/ocarina.py index ea081024..74eeab43 100644 --- a/trunk/ocarina.py +++ b/trunk/ocarina.py @@ -4,16 +4,60 @@ import thread from song import Song from cline import CLine +from duration import Duration +#import cmnds -def main(argv): - commands = CLine() +class main: + def __init__(self,argv): + if len(argv) == 0: + print "python ocarina.py /path/to/song/" + self.commands = CLine() + self.registerCmnds() - s = Song(argv[0]) - s.play() + lines = ["test","testing","final test"] + 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() - thread.start_new_thread(mainloop.run,()) + + def play(self): + 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:]) diff --git a/trunk/song.py b/trunk/song.py index 39a8b60d..dbdc8f7e 100644 --- a/trunk/song.py +++ b/trunk/song.py @@ -1,12 +1,15 @@ -import pygst,sys,time,gobject +import sys +import time +import pygst pygst.require("0.10") import gst -import gst from duration import Duration class Song(): - def __init__(self,file): + def __init__(self,file,exitFunc,prnt): + self.quit=exitFunc + self.prnt=prnt # initialize player pipeline self.player = gst.Pipeline("player") bin = gst.element_factory_make("playbin",None) @@ -24,35 +27,32 @@ class Song(): # Initialize stuff for finding duration self.time_format = gst.Format(gst.FORMAT_TIME) self.length = None - - # Start mainloop - gobject.threads_init() # Called on bus messages def onMessage(self,bus,message): t = message.type if t == gst.MESSAGE_EOS: - print "End of stream" - sys.exit(0) + #print "End of stream" + self.prnt("End of stream") + self.quit() elif t == gst.MESSAGE_ERROR: - print "Error" - sys.exit(0) + err, debug = message.parse_error() + self.prnt("Error: %s" % err,debug) + #print "Error: %s" % err, debug + self.quit() elif t == gst.MESSAGE_TAG: - print "Tags:" + #print "Tags:" # Probably should convert this to some other data structure # For easier use later taglist = message.parse_tag() - for key in taglist.keys(): - print key,taglist[key] return # Change state to "playing" def play(self): self.player.set_state(gst.STATE_PLAYING) - # Start main loop and find duration - # If this hasn't been done yet + # Start main loop and find duration (if this hasn't been done yet) if self.length == None: time.sleep(0.2) self.duration() @@ -68,10 +68,12 @@ class Song(): self.length = Duration() length = self.player.query_duration(self.time_format,None)[0] self.length.setTime(length) - self.length.disp() + #self.length.disp(self.prnt) # Print out current running time def curTime(self): length = self.player.query_position(self.time_format,None)[0] - return Duration().setTime(length) + dur = Duration() + dur.setTime(length) + return dur