Welcome, guest | Sign In | My Account | Store | Cart

Start this script from a terminal to change the terminal itself to a new directory, using a graphical tree view.

Python, 86 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
"""
author: anton.vredegoor@gmail.com
last edit: Oct 21, 2010

Python directory changer.

This script displays a directory tree. By single clicking on a '+' or '-' sign
one can expand/collapse an item. Double clicking on an item itself, 
if that item is a directory, results in the script printing its location to 
standard output, and then exiting.

The idea is to create a script to start a graphical directory browser
from a terminal window and switch the terminal it is started from 
to the new directory.

Since a script cannot change the environment of its parent we need a
little trick to make this work.

Create an alias (for example in .bashrc or in .bash_aliases) like this:

alias pycd='python ~/pycd.py > ~/pycd.out && . ~/pycd.out'

(This alias expects pycd.py to be in the user's home directory,  so put 
it there or adapt the alias)

Now typing 'pycd' in a terminal (without the quotes) opens the browser
and redirects the script's output to the file 'pycd.out', and then (after the 
script has exited) executes the cd command in that file, in the shell. 

The result is, we end up in the desired directory.

The script needs the TreeWidget from idle, so one should
install idle:

sudo aptitude install idle

(or equivalent)

If necessary, adjust the sys.path.append line, to make it point 
to your idlelib location.

"""

import sys
import os
from string import replace
from Tkinter import *

sys.path.append(r'/usr/lib/python2.6/idlelib')

from TreeWidget import FileTreeItem, TreeNode, ScrolledCanvas

class MyFileTreeItem(FileTreeItem):
    
    def GetSubList(self):
        try:
            names = os.listdir(self.path)
        except os.error:
            return []
        names.sort(lambda a, b: cmp(os.path.normcase(a).lower(), os.path.normcase(b).lower()))
        sublist = []
        for name in names:
            item = MyFileTreeItem(os.path.join(self.path, name))
            sublist.append(item)
        return sublist

    def OnDoubleClick(self):
        if self.IsExpandable():
            sys.stdout.write('cd %s' %(replace(self.path,' ','\ ')))
            sys.exit()

def test():
    root = Tk()
    sys.exitfunc = root.quit
    root.configure(bd=0, bg="yellow")
    root.title("terminal directory changer")
    root.focus_set()
    sc = ScrolledCanvas(root, bg="white", highlightthickness=0, takefocus=1)
    sc.frame.pack(expand=1, fill="both")
    item = MyFileTreeItem('/')
    node = TreeNode(sc.canvas, None, item)
    node.expand()
    root.mainloop()

if __name__=='__main__':
    test()

Sometimes it is easier to change a directory than to open a new terminal, but one does not exactly know the target directory's name.

This script is meant to combine browsing for the name and changing the directory with as little overhead as possible.

It needs some editing of a file containing aliases, so the setup is not very smooth. And it is probably not used very often. But occasionally, it is exactly the right solution. Or, more often, the solution that is easiest to remember ;-)