diff --git a/twander.py b/twander.py index 901c8f5..545711a 100755 --- a/twander.py +++ b/twander.py @@ -4,7 +4,7 @@ PROGNAME = "twander" -RCSID = "$Id: twander.py,v 1.41 2002/11/11 21:00:09 tundra Exp $" +RCSID = "$Id: twander.py,v 1.42 2002/11/12 00:01:18 tundra Exp $" VERSION = RCSID.split()[2] @@ -162,8 +162,10 @@ #----------------------------------------------------------# +CURSELECTION = 0 FILESELECTED = "" - +LASTDIR = [] +STARTDIR = "" #---------------------------Code Begins Here----------------------------------# @@ -223,11 +225,29 @@ # Get Directory Of Current ROOTDIR And Load Into UI ##### -def LoadDirList(): - global ROOTDIR +def LoadDirList(newdir, save=TRUE): + global LASTDIR, ROOTDIR # Canonicalize the current directory name - ROOTDIR = os.path.abspath(ROOTDIR) + newdir = os.path.abspath(newdir) + + # Push last directory visited onto the visited stack + # Do not do this if we've been told not to OR if + # what we're about to save is the same as the top + # of the stack + + # 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: + save = FALSE + if save: + LASTDIR.append(ROOTDIR) + + + # And select new directory to visit + ROOTDIR = newdir # And make sure it ends with a path separator character if ROOTDIR[-1] != PSEP: @@ -340,6 +360,54 @@ # End of 'WrnMsg()' +##### +# Process Current Selection +##### + +def ProcessSelection(): + global CURSELECTION, FILESELECTED, ROOTDIR + + selected = UI.DirList.get(str(CURSELECTION)) + + # 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 + + # Strip off delimiters to get real name + selected = selected[1:-1] + + # Build full path name + selected = os.path.join(os.path.abspath(ROOTDIR), selected) + + # Convert ending ".." into canonical path + if selected.endswith(".."): + selected = PSEP.join(selected.split(PSEP)[:-2]) + + # Need to end the directory string with a path + # separator character so that subsequent navigation + # will work when we hit the root directory of the file + # system. In the case of Unix, this means that + # if we ended up at the root directory, we'll just + # get "/". In the case of Win32, we will get + # "DRIVE:/". + + selected += PSEP + + # Load UI with new directory + LoadDirList(selected) + + # File was selected. Save its full name for later use. + else: + FILESELECTED = os.path.join(ROOTDIR, selected) + + UI.DirList.pack() + +# End of 'Process Selection()' + #----------------------------------------------------------# # GUI Classes And Handlers # @@ -385,8 +453,8 @@ # Bind the relevant root window handlers - # Set up keystroke handler for application - root.bind('', KeystrokeHandler) + # Bind handler for Backspace key + root.bind('', KeyBackspace) # Bind handler for Esc key root.bind('', KeyEsc) @@ -394,6 +462,20 @@ # Bind handler for Home key root.bind('', KeyHome) + # Bind handler for Left Arrow key + root.bind('', KeyLeft) + + # Bind handler for Up Arrow key + root.bind('', KeyUp) + + # Bind handler for Down Arrow key + root.bind('', KeyDwn) + + # Set up keystroke handler for application + # These will be checked against the command + # key definitions in the configuration file + root.bind('', KeystrokeHandler) + # Make sure the top level window has input focus root.focus() @@ -405,69 +487,93 @@ ##### def DirListHandler(event): - global FILESELECTED, ROOTDIR - - selected = UI.DirList.get(UI.DirList.nearest(event.y)) - - # 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: + global CURSELECTION - FILESELECTED = "" # Didnt' select a file - - # Strip off delimiters to get real name - selected = selected[1:-1] - - # Build full path name - selected = os.path.join(os.path.abspath(ROOTDIR), selected) - - # Convert ending ".." into canonical path - if selected.endswith(".."): - selected = PSEP.join(selected.split(PSEP)[:-2]) - - # Need to end the directory string with a path - # separator character so that subsequent navigation - # will work when we hit the root directory of the file - # system. In the case of Unix, this means that - # if we ended up at the root directory, we'll just - # get "/". In the case of Win32, we will get - # "DRIVE:/". - - selected += PSEP - - # Save new root and load its contents into the UI - ROOTDIR = selected - LoadDirList() - - # File was selected. Save its full name for later use. - else: - FILESELECTED = os.path.join(ROOTDIR, selected) + CURSELECTION = int(UI.DirList.nearest(event.y)) + ProcessSelection() # End of 'DirListHandler()' ##### +# Event Handler For Backspace Key - Move up one directory +##### + +def KeyBackspace(event): + global ROOTDIR + + # Move up one directory level unless we're already at the root + if ROOTDIR != os.path.abspath(PSEP): + LoadDirList(ROOTDIR + "..") + + +# End of 'KeyBackspace()' + + +##### # Event Handler For Esc Key - Program Quit ##### def KeyEsc(event): - sys.exit() # End of 'KeyEsc()' ##### +# Event Handler For Left Arrow Key - Move To Previous Directory +##### + +def KeyLeft(event): + global LASTDIR + + # Move to last directory visited, if any - inhibit this from + # being place on the directory traversal stack + if LASTDIR: + LoadDirList(LASTDIR.pop(), save=FALSE) + + # No previous directory + else: + pass + +# End of 'KeyLeft()' + + +##### +# Event Handler For Down Arrow Key - Move Down One Selection +##### + +def KeyDwn(event): + global CURSELECTION + + pass + + +# End of 'KeyDwn()' + + +##### +# Event Handler For Up Arrow Key - Move Up One Selection +##### + +def KeyUp(event): + global CURSELECTION + + if CURSELECTION > 0: + CURSELECTION -= 1 + ProcessSelection() + +# End of 'KeyDwn()' + + +##### # Event Handler For Home Key - Go Back to Initial Directory ##### def KeyHome(event): global ROOTDIR, STARTDIR - ROOTDIR = STARTDIR - LoadDirList() + LoadDirList(STARTDIR) # End of 'KeyHome()' @@ -573,12 +679,14 @@ UIroot = Tk() UI = twanderUI(UIroot) +# Canonicalize ROOTDIR +ROOTDIR = os.path.abspath(ROOTDIR) + PSEP # Save the initial starting directory in case we want to return later STARTDIR = ROOTDIR # Initialize the UI directory listing -LoadDirList() +LoadDirList(ROOTDIR) # Run the program interface UIroot.mainloop()