乐趣区

中文环境下更好的命令行tree工具

说明

tree 是一款开源工具,支持 Linux、Windows 等多种操作系统平台,在 Linux 中可以直接 sudo apt install tree 安装。在 Windows 平台该命令发布在 Cygwin 环境中,可以通过 Cygwin 的安装程序,在选择 Package 时选中并安装到 Cygwin 的 bin 路径下。

修改内容:

由于在 Windows 平台这个 tree 命令只能在 Cygwin 环境下运行,而且在 GBK 中文环境下显示的效果较差。所以 Fork 了之后做了一些修改:

  1. 在 Windows 环境下 tree 脱离 Cygwin 环境可以独立运行(例如将 tree 命令复制到 Git bash bin 目录使用)
  2. 支持 GBK(CP936)字符更好的显示目录文件的树形结构

可执行程序点击链接下载: https://github.com/efreykongcn/tree/blob/master/tree.exe
GitHub 源码地址:https://github.com/efreykongcn/tree

使用 Windows 编译环境:
  • 编译器:MINGW-W64 8.1.0 GCC
  • 开发工具:VS Code & C/C++ Extension

运行效果示例:

下面分别展示在 Windows 平台原生 tree 命令、Windows 自带的 tree.com 命令、修改后的命令执行后的效果。

1. Cygwin 环境原来的 tree 命令效果

在 Cygwin 的终端(Terminal)中运行 tree 命令,会以可视化的树形结构展现指定文件夹下面的所有目录和文件。在 GBK 字符集环境下(Windows 10 中文默认的字符集),该程序只能以标准 ASCII 字符来显示目录文件的树形结构。Cygwin 终端运行 tree 命令运行效果如下:

$ tree
.
|-- CHANGES
|-- color.c
|-- doc
|   |-- tree.1
|   |-- tree.1.fr
|   `-- xml.dtd
|-- file.c
|-- hash.c
|-- html.c
|-- INSTALL
|-- json.c
|-- LICENSE
|-- Makefile
|-- mingw-w64
|   |-- include
|   |   |-- grp.h
|   |   |-- langinfo.h
|   |   |-- pwd.h
|   |   |-- stat.h
|   |   |-- types.h
|   |   `-- unistd.h
|   |-- mingw-w64-grp.c
|   |-- mingw-w64-langinfo.c
|   |-- mingw-w64-pwd.c
|   `-- mingw-w64-unistd.c
|-- README
|-- README.md
|-- strverscmp.c
|-- TODO
|-- tree.c
|-- tree.exe
|-- tree.h
|-- unix.c
`-- xml.c

3 directories, 31 files
2. Windows 自带的 tree.com 命令效果

Windows 自带了 tree.com 命令,不过显示的效果和功能都比较弱鸡,在 PowerShell 中使用 tree.com /f(默认只显示目录,加/f 参数显示文件)命令看下运行效果:

Folder PATH listing for volume Windows
Volume serial number is 6AA8-4173
C:.
│  CHANGES
│  color.c
│  file.c
│  hash.c
│  html.c
│  INSTALL
│  json.c
│  LICENSE
│  Makefile
│  README
│  README.md
│  strverscmp.c
│  TODO
│  tree.c
│  tree.exe
│  tree.h
│  unix.c
│  xml.c
│
├─.vscode
│      c_cpp_properties.json
│      launch.json
│      tasks.json
│
├─doc
│      tree.1
│      tree.1.fr
│      xml.dtd
│
└─mingw-w64
    │  mingw-w64-grp.c
    │  mingw-w64-langinfo.c
    │  mingw-w64-pwd.c
    │  mingw-w64-unistd.c
    │
    └─include
            grp.h
            langinfo.h
            pwd.h
            stat.h
            types.h
            unistd.h


3. 修改后的 tree 命令效果

这次修改是源于在使用 Git Bash 时,发现 Git bash 中没有在 windows 环境中实现 tree 命令。因此打算将这个开源的 tree 命令移植到 Git bash 环境中。
出于可以在脱离 Cygwin 环境独立使 tree 命令,以及在 Windows 中文环境加强显示效果的目的,做了本次修改。修改后的 tree,在 PowerShell 中运行 tree.exe 命令后效果如下:

$ tree
.
├── CHANGES
├── color.c
├── doc
│   ├── tree.1
│   ├── tree.1.fr
│   └── xml.dtd
├── file.c
├── hash.c
├── html.c
├── INSTALL
├── json.c
├── LICENSE
├── Makefile
├── mingw-w64
│   ├── include
│   │   ├── grp.h
│   │   ├── langinfo.h
│   │   ├── pwd.h
│   │   ├── stat.h
│   │   ├── types.h
│   │   └── unistd.h
│   ├── mingw-w64-grp.c
│   ├── mingw-w64-langinfo.c
│   ├── mingw-w64-pwd.c
│   └── mingw-w64-unistd.c
├── README
├── README.md
├── strverscmp.c
├── TODO
├── tree.c
├── tree.exe
├── tree.h
├── unix.c
└── xml.c

3 directories, 31 files

查看修改后的版本信息:


$ tree --version
tree v1.8.0 (c) 1996 - 2018 by Steve Baker, Thomas Moore, Francesc Rocher, Florian Sesser, Kyosuke Tokoro
MINGW-W64 & GBK support added by Efrey Kong
退出移动版