diff --git a/twander.py b/twander.py index 6c95b30..b987add 100755 --- a/twander.py +++ b/twander.py @@ -4,7 +4,7 @@ PROGNAME = "twander" -RCSID = "$Id: twander.py,v 1.50 2002/11/14 03:40:27 tundra Exp $" +RCSID = "$Id: twander.py,v 1.51 2002/11/14 04:04:46 tundra Exp $" VERSION = RCSID.split()[2] @@ -170,14 +170,6 @@ ] -#----------------------------------------------------------# -# Global Variables & Data Structures # -#----------------------------------------------------------# - - -LASTDIR = [] - - #---------------------------Code Begins Here----------------------------------# @@ -331,31 +323,35 @@ # Bind the relevant widget event handlers ##### - # We'll accept Single-Clicks as a selection - self.DirList.bind(SELECTMOUSE, DirListHandler) - # Bind the relevant root window handlers - # Bind handler for "Up Dir" - root.bind(GOUP, KeyUpDir) - - # Bind handler for "Quit Program" - root.bind(QUITPROG, KeyQuitProg) - - # Bind handler for "Goto Starting Dir" - root.bind(GOSTART, KeyStartDir) - - # Bind handler for "Previous Dir" - root.bind(GOPREV, KeyPrevDir) - # Set up keystroke handler for application # These will be checked against the command # key definitions in the configuration file root.bind(KEYPRESS, KeystrokeHandler) + # Bind handler for "Quit Program" + root.bind(QUITPROG, KeyQuitProg) + + # Bind handler for "Home Dir" + root.bind(GOHOME, KeyHomeDir) + + # Bind handler for "Previous Dir" + root.bind(GOPREV, KeyPrevDir) + + # Bind handler for "Starting Dir" + root.bind(GOSTART, KeyStartDir) + + # Bind handler for "Up Dir" + root.bind(GOUP, KeyUpDir) + # Bind handler for "Item Select" root.bind(SELECTKEY, DirListHandler) + # We'll accept Single-Clicks as a selection + self.DirList.bind(SELECTMOUSE, DirListHandler) + + # Give the listbox focus so arrow keys work self.DirList.focus() # End if method 'twanderUI.__init__()' @@ -405,7 +401,7 @@ selected = "" # Update the titlebar - mainwin.title(PROGNAME + " " + VERSION + " " + ROOTDIR + selected) + mainwin.title(PROGNAME + " " + VERSION + " " + UI.rootdir + selected) # End of method 'twanderUI.UpdateTitle()' @@ -417,7 +413,6 @@ ##### def DirListHandler(event): - global ROOTDIR # Get current selection. If none, just return, otherwise process selected = UI.CurrentSelection() @@ -434,7 +429,7 @@ selected = selected[1:-1] # Build full path name - selected = os.path.join(os.path.abspath(ROOTDIR), selected) + selected = os.path.join(os.path.abspath(UI.rootdir), selected) # Convert ending ".." into canonical path if selected.endswith(".."): @@ -472,11 +467,10 @@ ##### -# Get Directory Of Current ROOTDIR And Load Into UI +# Get Directory Of Current UI.rootdir And Load Into UI ##### def LoadDirList(newdir, save=TRUE): - global LASTDIR, ROOTDIR # Canonicalize the current directory name newdir = os.path.abspath(newdir) @@ -489,24 +483,24 @@ # If there is anything on the stack, see if last element # matches what we're about to put there. - if LASTDIR: - if LASTDIR[-1] == ROOTDIR: + if UI.lastdir: + if UI.lastdir[-1] == UI.rootdir: save = FALSE if save: - LASTDIR.append(ROOTDIR) + UI.lastdir.append(UI.rootdir) # And select new directory to visit - ROOTDIR = newdir + UI.rootdir = newdir # And make sure it ends with a path separator character - if ROOTDIR[-1] != PSEP: - ROOTDIR = ROOTDIR + PSEP + if UI.rootdir[-1] != PSEP: + UI.rootdir = UI.rootdir + PSEP # Clear out the old contents UI.DirList.delete(0,END) # Load new directory contents into UI - for x in BuildDirList(ROOTDIR): + for x in BuildDirList(UI.rootdir): UI.DirList.insert(END, x) # And update the title to reflect changes @@ -519,19 +513,19 @@ # Return Ordered List Of Directories & Files For Current Root ##### -def BuildDirList(ROOTDIR): +def BuildDirList(rootdir): 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)): + for file in os.listdir(rootdir): + if os.path.isdir(os.path.join(rootdir,file)): dList.append(DIR_LDELIM + file + DIR_RDELIM) else: fList.append(file) except: - ErrMsg(eDIRRD % ROOTDIR) + ErrMsg(eDIRRD % rootdir) dList.sort() @@ -540,7 +534,7 @@ # OSs like Win32 like to use '$' in file names which # sorts before "." - if ROOTDIR != os.path.abspath(PSEP): + if rootdir != os.path.abspath(PSEP): dList.insert(0, DIR_LDELIM + ".." + DIR_RDELIM) fList.sort() @@ -564,7 +558,7 @@ if index: number = UI.DirList.index(index) - LoadDirList(ROOTDIR, save=FALSE) + LoadDirList(UI.rootdir, save=FALSE) # Restore current selection and active entry @@ -592,15 +586,25 @@ ##### +# Event Handler: Goto $HOME +##### + +def KeyHomeDir(event): + + LoadDirList(HOME) + +# End of 'KeyHomeDir()' + + +##### # Event Handler: Move up one directory ##### def KeyUpDir(event): - global ROOTDIR # Move up one directory level unless we're already at the root - if ROOTDIR != os.path.abspath(PSEP): - LoadDirList(ROOTDIR + "..") + if UI.rootdir != os.path.abspath(PSEP): + LoadDirList(UI.rootdir + "..") # End of 'KeyUpDir()' @@ -620,12 +624,11 @@ ##### def KeyPrevDir(event): - global LASTDIR # Move to last directory visited, if any - inhibit this from # being placed on the directory traversal stack - if LASTDIR: - LoadDirList(LASTDIR.pop(), save=FALSE) + if UI.lastdir: + LoadDirList(UI.lastdir.pop(), save=FALSE) # No previous directory else: @@ -639,7 +642,7 @@ ##### def KeyStartDir(event): - global ROOTDIR, STARTDIR + global STARTDIR LoadDirList(STARTDIR) @@ -664,7 +667,7 @@ # Replace runtime-determined tokens cmd = cmd.replace(FILENAME, UI.CurrentSelection()) - cmd = cmd.replace(DIRNAME, ROOTDIR) + cmd = cmd.replace(DIRNAME, UI.rootdir) # Actually execute the command os.system(cmd) @@ -717,22 +720,6 @@ if opt == "-y": HEIGHT = val - -# Can only have 0 or 1 arguments -# Make sure any starting directory argument is legit - -if len(args) > 1: - ErrMsg(eTOOMANY) - sys.exit(1) - -if len(args) == 1: - STARTDIR = args[0] - if not os.path.isdir(STARTDIR): - ErrMsg(eBADROOT % STARTDIR) - sys.exit(1) - -ROOTDIR = STARTDIR - # This program requires a config file if not os.path.exists(CONF): @@ -748,11 +735,34 @@ UIroot = Tk() UI = twanderUI(UIroot) -# Canonicalize ROOTDIR -ROOTDIR = os.path.abspath(ROOTDIR) + PSEP +##### +# Setup global UI variables +##### + +# Figure out where to start +# Program can only have 0 or 1 arguments +# Make sure any startdir argument is legit + +if len(args) > 1: + ErrMsg(eTOOMANY) + sys.exit(1) + +if len(args) == 1: + STARTDIR = args[0] + if not os.path.isdir(STARTDIR): + ErrMsg(eBADROOT % STARTDIR) + sys.exit(1) + +UI.rootdir = STARTDIR + +# Initialize directory stack +UI.lastdir = [] + +# Canonicalize UI.rootdir +UI.rootdir = os.path.abspath(UI.rootdir) + PSEP # Initialize the UI directory listing -LoadDirList(ROOTDIR) +LoadDirList(UI.rootdir) # And start the periodic polling of the widget UI.poll()