diff --git a/twander.py b/twander.py index 2af252c..568cfef 100755 --- a/twander.py +++ b/twander.py @@ -4,7 +4,7 @@ PROGNAME = "twander" -RCSID = "$Id: twander.py,v 1.45 2002/11/12 10:02:13 tundra Exp $" +RCSID = "$Id: twander.py,v 1.46 2002/11/13 00:41:51 tundra Exp $" VERSION = RCSID.split()[2] @@ -162,7 +162,6 @@ #----------------------------------------------------------# -INDEX = 0 FILESELECTED = "" LASTDIR = [] STARTDIR = "" @@ -187,6 +186,7 @@ dList, fList = [], [] + # Walk the directory separate subdirs and files try: for file in os.listdir(ROOTDIR): if os.path.isdir(os.path.join(ROOTDIR,file)): @@ -253,6 +253,17 @@ if ROOTDIR[-1] != PSEP: ROOTDIR = ROOTDIR + PSEP + # Update the widget + UpdateDirList() + +# End of 'LoadDirList(): + + +##### +# Update/Redraw Directory Listing +##### + +def UpdateDirList(): # Update the window title UIroot.title(PROGNAME + " " + VERSION + " " + ROOTDIR) @@ -264,8 +275,7 @@ for x in BuildDirList(ROOTDIR): UI.DirList.insert(END, x) -# End of 'LoadDirList(): - +# End of 'UpdateDirList()' ##### @@ -361,21 +371,46 @@ ##### +# Determine if a file or directory selected. +# Returns TRUE for directory, FALSE for file. +# Side-Effect: global FILESELECTED set to filename or "" +##### + +def IsDirSelected(selected): + global FILESELECTED + + FILESELECTED = "" + + if selected[0] == DIR_LDELIM and selected[-1] == DIR_RDELIM: + FILESELECTED = "" # Directory selected + return TRUE + else: + FILESELECTED = selected # File was selected + return FALSE + +# End of 'IsDirSelected()' + + +##### # Process Current Selection ##### -def ProcessSelection(): - global INDEX, FILESELECTED, ROOTDIR - - selected = UI.DirList.get(INDEX) +def DirListHandler(event): + global FILESELECTED, ROOTDIR + + index = event.widget.curselection() + + # If nothing is currently selected, we have no futher work + if not index: + return + + selected = UI.DirList.get(index) # If selection is a directory, move there and list contents. # We examine this by checking the string for the directory # delimiter characters previously inserted in BuildDirList() - if selected[0] == DIR_LDELIM and selected[-1] == DIR_RDELIM: - - FILESELECTED = "" # Didn't select a file + if IsDirSelected(selected): # Strip off delimiters to get real name selected = selected[1:-1] @@ -404,14 +439,13 @@ # This guarantees a selection in the new # directory context, so subsequent commands # won't try to operate on an item selected in a - # previoius directory + # previous directory - INDEX = ('0',) - UI.DirList.selection_set(INDEX) + UI.DirList.selection_set(('0',)) # File was selected. Save its full name for later use. else: - FILESELECTED = os.path.join(ROOTDIR, selected) + pass # End of 'Process Selection()' @@ -455,66 +489,82 @@ self.vSB.pack(side=RIGHT, fill=Y) self.DirList.pack(side=LEFT, fill=BOTH, expand=1) + ##### # Bind the relevant widget event handlers - - # We'll accept Single- or Double-Clicks as a selection + ##### + + # We'll accept Single-Clicks as a selection self.DirList.bind('', DirListHandler) - # Bind the relevant root window handlers - # Bind handler for Backspace key - root.bind('', KeyBackspace) + # Bind handler for "Up Dir" + root.bind('', KeyUpDir) - # Bind handler for Esc key - root.bind('', KeyEsc) + # Bind handler for "Quit Program" + root.bind('', KeyQuitProg) - # Bind handler for Home key - root.bind('', KeyHome) + # Bind handler for "Goto Starting Dir" + root.bind('', KeyStartDir) - # Bind handler for Left Arrow key -# root.bind('<-Left>', KeyLeft) + # Bind handler for "Previous Dir" + root.bind('', KeyPrevDir) # Set up keystroke handler for application # These will be checked against the command # key definitions in the configuration file root.bind('', KeystrokeHandler) - # Bind the space key for selection + # Bind handler for "Item Select" root.bind('', DirListHandler) + # Bind handler for "Refresh Dir Listing" + root.bind('', RefreshDirList) + self.DirList.focus() +# End if class '__init__' + + + ##### + # Support periodic polling to make sure widget stays + # in sync with reality of current directory. + ##### + + def poll(self): + + RefreshDirList() + self.DirList.after(2000, self.poll) + + + # End of class definition, 'twanderUI' ##### -# Event Handler For Directory Listbox +# Refresh contents of directory listing to stay in sync with reality ##### -def DirListHandler(event): - global INDEX +def RefreshDirList(*args): - # We use the 'nearest' method below to guarantee that INDEX will always - # a legitimate value - i.e., *Something* will always be selected. - # If we just used widget.get method this would not be the case - # whenever we rapidly clicked into a directory with only one entry (".."), - # because any click in a blank area of the screen would return (). + number = 0 -# INDEX = event.widget.nearest(event.y) - INDEX = event.widget.curselection() + index = UI.DirList.curselection() + if index: + number = UI.DirList.index(index) - if INDEX: - ProcessSelection() + LoadDirList(ROOTDIR, save=FALSE) + UI.DirList.select_set(number) + UI.DirList.see(number) -# End of 'DirListHandler()' +# End of 'RefreshDirList() ##### # Event Handler For Backspace Key - Move up one directory ##### -def KeyBackspace(event): +def KeyUpDir(event): global ROOTDIR # Move up one directory level unless we're already at the root @@ -522,24 +572,24 @@ LoadDirList(ROOTDIR + "..") -# End of 'KeyBackspace()' +# End of 'KeyUpDir()' ##### # Event Handler For Esc Key - Program Quit ##### -def KeyEsc(event): +def KeyQuitProg(event): sys.exit() -# End of 'KeyEsc()' +# End of 'KeyQuitProg()' ##### # Event Handler For Left Arrow Key - Move To Previous Directory ##### -def KeyLeft(event): +def KeyPrevDir(event): global LASTDIR # Move to last directory visited, if any - inhibit this from @@ -551,19 +601,19 @@ else: pass -# End of 'KeyLeft()' +# End of 'KeyPrevDir()' ##### # Event Handler For Home Key - Go Back to Initial Directory ##### -def KeyHome(event): +def KeyStartDir(event): global ROOTDIR, STARTDIR LoadDirList(STARTDIR) -# End of 'KeyHome()' +# End of 'KeyStartDir()' ##### @@ -676,6 +726,9 @@ # Initialize the UI directory listing LoadDirList(ROOTDIR) +# And start the periodic polling of the widget +UI.poll() + # Run the program interface UIroot.mainloop()