In recent days I need to generate a directory free from the window command prompt. As I known there is a DOS tool called tree could do this. But it is not quite fit for my requirement. Here is an example which the graphic generated by built-in tree command in window.
D:\PersonalTools>tree D:\emacs-24.5 /A
卷 WareHouse 的文件夹 PATH 列表
卷序列号为 64CA-A0C3D:\EMACS-24.5
+—admin
| +—charsets
| | \—mapfiles
| +—coccinelle
| +—grammars
| +—notes
| +—nt
| \—unidata
+—build-aux
| \—snippet
+—doc
| +—emacs
| +—lispintro
| +—lispref
| +—man
| \—misc
+—etc
| +—charsets
| +—e
| +—forms
| +—gnus
| +—images
| | +—custom
| | +—ezimage
| | +—gnus
| | +—gud
| | +—icons
| | | +—allout-widgets
| | | | +—dark-bg
| | | | \—light-bg
| | | \—hicolor
| | | +—128×128
| | | | \—apps
| | | +—16×16
| | | | \—apps
| | | +—24×24
| | | | \—apps
| | | +—32×32
| | | | \—apps
| | | +—48×48
| | | | \—apps
| | | \—scalable
| | | +—apps
| | | \—mimetypes
| | +—low-color
| | +—mpc
| | +—newsticker
| | +—smilies
| | | +—grayscale
| | | \—medium
| | \—tree-widget
| | +—default
| | \—folder
| +—nxml
| +—org
| +—refcards
| +—schema
| +—srecode
| +—themes
| \—tutorials
+—info
+—leim
| +—CXTERM-DIC
| +—MISC-DIC
| \—SKK-DIC
+—lib
+—lib-src
+—lisp
| +—calc
| +—calendar
| +—cedet
| | +—ede
| | +—semantic
| | | +—analyze
| | | +—bovine
| | | +—decorate
| | | +—symref
| | | \—wisent
| | \—srecode
| +—emacs-lisp
| +—emacs-parallel
| +—emulation
| +—erc
| +—eshell
| +—gnus
| +—international
| +—language
| +—leim
| | +—ja-dic
| | \—quail
| +—mh-e
| +—net
| +—nxml
| +—obsolete
| +—org
| +—play
| +—progmodes
| +—term
| +—textmodes
| +—url
| \—vc
+—lwlib
+—m4
+—msdos
+—nextstep
| +—Cocoa
| | \—Emacs.base
| | \—Contents
| | \—Resources
| +—GNUstep
| | \—Emacs.base
| | \—Resources
| \—templates
+—nt
| +—icons
| \—inc
| +—arpa
| +—netinet
| \—sys
+—oldXMenu
+—site-lisp
\—src
\—bitmaps
A quite complicate file structure for emacs 24.5. There are several limitations for this command.
- It can’t control the iteration depth
- When display with files it doesn’t generated the dash lines which is not clearly to indicate the file structure
- If we could give a depth parameter I also want to display the sub items as ellipsis to indicate there is something under but which is omitted
So I use python to write a custom version to enhance this command called treex. It support generated the same structure as the tree command but also provide additional control switch. The source code is already pushed to my personal repository in GitHub. Feel free to check it out.
The help information is
usage: treex.py [-h] [-f] [-d DEPTH] [-m] [-e] path
A Cmd Line Tool for Graphically displays the folder structure of a drive or
pathpositional arguments:
pathoptional arguments:
-h, –help show this help message and exit
-f, –file Display the names of the files in each folder
-d DEPTH, –depth DEPTH
Display the depth number
-m, –mark Mark the item is file or directory
-e, –ellipsis Mark the next sub-item with ellipsisCreate by Edwin, Shang(Shang, Erxin) License under MIT. Version 1.0.0
Try to use the treex to generate a depth 2 directory structure
D:\PersonalTools>c:\Python27\python.exe treex.py D:\emacs-24.5 -d 2
Folder path listing
D:\emacs-24.5
+—admin
| +—charsets
| +—coccinelle
| +—grammars
| +—notes
| +—nt
| \—unidata
+—build-aux
| \—snippet
+—doc
| +—emacs
| +—lispintro
| +—lispref
| +—man
| \—misc
+—etc
| +—charsets
| +—e
| +—forms
| +—gnus
| +—images
| +—nxml
| +—org
| +—refcards
| +—schema
| +—srecode
| +—themes
| \—tutorials
+—info
+—leim
| +—CXTERM-DIC
| +—MISC-DIC
| \—SKK-DIC
+—lib
+—lib-src
+—lisp
| +—calc
| +—calendar
| +—cedet
| +—emacs-lisp
| +—emacs-parallel
| +—emulation
| +—erc
| +—eshell
| +—gnus
| +—international
| +—language
| +—leim
| +—mh-e
| +—net
| +—nxml
| +—obsolete
| +—org
| +—play
| +—progmodes
| +—term
| +—textmodes
| +—url
| \—vc
+—lwlib
+—m4
+—msdos
+—nextstep
| +—Cocoa
| +—GNUstep
| \—templates
+—nt
| +—icons
| \—inc
+—oldXMenu
+—site-lisp
\—src
\—bitmaps
Here is another result for generate with ellipsis and marks
D:\PersonalTools>c:\Python27\python.exe treex.py D:\emacs-24.5 -d 2 -e -m
Folder path listing
D:\emacs-24.5
[d]+—admin
[d]| +—charsets
[d]| | \—…
[d]| +—coccinelle
[d]| +—grammars
[d]| +—notes
[d]| +—nt
[d]| \—unidata
[d]+—build-aux
[d]| \—snippet
[d]+—doc
[d]| +—emacs
[d]| +—lispintro
[d]| +—lispref
[d]| +—man
[d]| \—misc
[d]+—etc
[d]| +—charsets
[d]| +—e
[d]| +—forms
[d]| +—gnus
[d]| +—images
[d]| | +—…
[d]| +—nxml
[d]| +—org
[d]| +—refcards
[d]| +—schema
[d]| +—srecode
[d]| +—themes
[d]| \—tutorials
[d]+—info
[d]+—leim
[d]| +—CXTERM-DIC
[d]| +—MISC-DIC
[d]| \—SKK-DIC
[d]+—lib
[d]+—lib-src
[d]+—lisp
[d]| +—calc
[d]| +—calendar
[d]| +—cedet
[d]| | +—…
[d]| +—emacs-lisp
[d]| +—emacs-parallel
[d]| +—emulation
[d]| +—erc
[d]| +—eshell
[d]| +—gnus
[d]| +—international
[d]| +—language
[d]| +—leim
[d]| | +—…
[d]| +—mail
[d]| +—mh-e
[d]| +—net
[d]| +—nxml
[d]| +—obsolete
[d]| +—org
[d]| +—play
[d]| +—progmodes
[d]| +—term
[d]| +—textmodes
[d]| +—url
[d]| \—vc
[d]+—lwlib
[d]+—m4
[d]+—msdos
[d]+—nextstep
[d]| +—Cocoa
[d]| | \—…
[d]| +—GNUstep
[d]| | \—…
[d]| \—templates
[d]+—nt
[d]| +—icons
[d]| \—inc
[d]| +—…
[d]+—oldXMenu
[d]+—site-lisp
[d]\—src
[d] \—bitmaps
The key technical point is the depth first traversal algorithms, and I implement it with nonrecursive mode to reduce the stack usage.
The other key point is how to generate the graphic. There is a pattern between the second line and the previous line. So I use it. The implementation could be found from the source.
That’s all. Happy coding.