Show HN: Leaves – 一款基于文本界面的磁盘占用树状图可视化工具
Show HN: Leaves – A text-UI disk usage treemap visualizer

原始链接: https://github.com/patonw/leaves

**Leaves** 是一款受 WinDirStat 和 QDirStat 启发的文本模式磁盘空间分析工具。它专为无法使用图形用户界面(如远程 shell 连接)的环境设计,通过嵌套矩形的层级树状图来直观展示磁盘使用情况。 该工具使用字符级渲染来近似显示文件和目录的大小,矩形面积与所占空间成正比。尽管相比图形化工具较为粗糙,但在处理大型文件系统时依然响应迅速。其功能包括可配置的忽略规则(支持 `.gitignore`)、可自定义的配色方案以及按文件类型分组的“X 射线”模式。用户可以通过键盘或鼠标在层级结构中导航,工具还会智能汇总深层目录结构以保持视觉清晰。 **技术细节:** * **实现:** 使用 Rust 构建,可通过 `cargo` 或 Nix 安装。 * **用法:** 支持多种命令行标志,用于控制深度(`--max-depth`)和进行过滤。 * **配置:** 可通过 `settings.toml` 或环境变量(如 `LEAVES_COLORS`、`LEAVES_DARK_MODE`)进行自定义。 * **性能:** 经过优化,可处理数百万个文件。但在扫描根目录时,用户应排除虚拟文件系统(如 `/proc` 或 `/dev`)以避免结果不准确。

**Leaves** 是一款全新的高性能终端用户界面(TUI)工具,旨在通过二维树状图可视化磁盘使用情况。该工具由 patonw 开发,将 WinDirStat 或 KDirStat 等图形界面工具的直观空间布局引入了服务器和容器环境,弥补了 `du` 或 `ncdu` 等传统列表式工具的不足。 Leaves 基于 Rust 构建并支持多线程,能够高效处理数以百万计的文件。除了简单的空间占用可视化,它还允许用户按文件扩展名对目录进行分区或查看摘要,从而更轻松地识别导致磁盘臃肿的元凶,例如被遗忘的缓存文件或不必要的文档。 该工具在 Hacker News 社区反响热烈,用户指出其与流行的 SpaceSniffer 工具十分相似,并建议了未来增加直接文件管理操作等功能。您可以在 GitHub 上找到该项目。
相关文章

原文

A text-mode disk usage visualization utility

nixpkgs

Leaves is a disk usage analyzer inspired by WinDirStat and QDirStat.

It shows files and directories in a hierarchy of nested rectangles. The area of a rectangle is proportional to its size. A 200 MB file will have twice the size as a sibling with 100 MB. The parent directory will have about 3 times the area of the smaller file.

However, due to the limited resolution of working at a character level, this is a fairly coarse approximation compared to a graphical tool. On the other hand, this will work over remote shell connections when graphical desktop environments are not available or impractical.

You can download a pre-built binary for a select number of platforms from the releases page. If one is not available for your system, follow this section to build from source.

A Rust toolchain is required to build this project.

The standard way to install Rust is via the rustup script:

curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh

You will likely need to install curl and basic build tools like gcc, make, ld, etc. via the build-essentials/base-devel metapackage or your distribution's equivalent.

Nix is an independent package manager and build tool focusing on repeatable, declarative builds.

You can install nix with:

curl --proto '=https' --tlsv1.2 -L https://nixos.org/nix/install | sh -s -- --daemon

Then, to load the build environment from this project directory use nix-shell:

Use the cargo build tool to compile the application:

Cargo can also install the application for your current user:

However, you will need to ensure your environment is configured to discover executables under the installation destination.

To run from source without installing use cargo:

$ cargo run --release -- [option flags] DIR_TO_SCAN

To invoke an installed copy of leaves with the target directory:

Without a target path leaves will scan the current directory.

By default, hidden directories will be ignored. Additionally, anything matching patterns in .gitignore or .ignore files in the hierarchy will not be counted. You can disable all ignore rules with -A. Other options will selectively disable specific ignore sources.

Additionally, you can pass globs (or negations prefixed with !) to refine the selection.

$ leaves --help
Usage: leaves [OPTIONS] [PATH] [OVERRIDES]...

Arguments:
  [PATH]
          Scanning root path
          
          [default: .]

  [OVERRIDES]...
          Git-style override globs. '!' prefix negates glob

Options:
  -d, --max-depth <MAX_DEPTH>
          Maximum depth of tree to keep in memory.
          
          Subtrees below this depth are replaced with summary nodes. Does not affect scan depth.
          
          [default: 5]

  -x, --xray
          Group files by type at the top-level, then split each region by directory

  -A, --include-all
          Don't *automatically* skip any files. Only overrides will be used

  -H, --include-hidden
          Don't skip hidden files and folders

  -I, --include-ignored
          Don't skip .ignore'd files

  -G, --include-gitignored
          Don't skip .gitignore'd files and folders

  -E, --include-gitexcluded
          Don't skip files and folders listed in .git/info/exclude

  -h, --help
          Print help (see a summary with '-h')

  -V, --version
          Print version

normal view

After leaves scans the target directory it will enter the main view.

The title bar contains the path of the current view, along with its total size and file count.

On the bottom are shortcut keys for altering the treemap.

The sidebar contains a standard collapsible explorer tree. File/directory details will be displayed in the box underneath the tree. Up/down/left/right keys will navigate this tree. The space key will open/close the selected directory. You can also use the mouse to interact with the explorer.

The central panel shows the files and directories as rectangles. The area of each rectangle is roughly proportional to the disk size. However, because each border takes two characters wide/tall at minimum, deeply nested items will appear smaller than similar top-level items.

Files are colored by extension with a yellow/orange/brown scheme. Directories are colored by name with a cooler scheme (viridis) comprised primarily of blues and greens. The palettes can be swapped with the LEAVES_COLORS=swap environment variable.

Note

Both palettes overlap on yellow, but directory colors tend towards more vibrant shades.

Coloring directories by name allows you to visually compare subdirectories in a hierarchy. For example, you can easily differentiate between debug vs. release or src vs. test directories in different projects.

Similarly, coloring files by extension lets you quickly spot .lock or .json files across directories, for instance.

You can also use the mouse to select files and directories in the explorer. The selected item and its ancestors will be highlighted by thicker borders. The selection is synchronized with the explorer tree, bidirectionally.

Using the Enter key will focus on the selected directory, replacing the contents of the explorer and treemap. Use the Backspace key to go to the parent directory. The previously viewed directory is selected whe navigating back up the hierarchy.

You will not be able to navigate beyond the initial target directory the application was launched with. To view files outside the current hierarchy, quit the app and restart with a new target.

With the limitations of working with block characters, it is usually not helpful to view millions of files across thousands of directories. This can be visually overwhelming. With each file represented by a handful of characters, little relevant information can be conveyed.

leaves will summarize directories below a certain depth, by grouping files of the same extension into a single rectangle with an area proportional to their cumulative size. You can control this depth during launch with --max-depth.

During run time, the selected directory can be expanded or deflated on demand. Deflating a directory will replace its child rectangles with file type summaries.

Expanding a directory will rescan the contents of that directory and more detailed children up to the run time depth. Beyond that depth, files will be grouped into summary nodes, yet again.

The run time depth can be set with the + and - keys.

deflated

In addition to partitioning the top level of the view by directory, using x-ray mode will show groups for each file type. Each group will then be divided into directories.

You can enter x-ray mode from any view with the x key. In x-ray mode you can focus any group or subdirectory, changing the view with Enter and Backspace as before. To return to normal mode, press the x key again.

Important

you will not be able to navigate to the parent of the x-rayed directory without returning to normal mode.

x-ray view

Leaves reads from $USER_CONFIG_DIR/leaves/setings.toml. The exact location depends on your platform. For most Linux users this is under ~/.config/. You can override most settings using environment variables prefixed by LEAVES_.

setting value description environment
dark_mode true/false renders elements with lighter colors for enhanced visibility on dark terminals LEAVES_DARK_MODE
color_shift 0.0 to 1.0 Lightens or darkens colors for better visibility LEAVES_COLOR_SHIFT
colors theme name Name of a theme for file/directory gradients LEAVES_COLORS
dir_style thick/plain Renders directory borders with stronger lines LEAVES_DIR_STYLE

There are four built in themes:

  • fall: warm colors for files & extensions, cool colors for directories
  • spring: cool colors for extensions and warm colors for directories
  • greys: shades of grey for everything
  • mono: black and white

Additionally, you can create new themes in the configuration file using sections named themes.YOUR_THEME. Each section needs dirs and files which are arrays of HTML color codes. These can be named colors, hex RGB or rgb/hsl triples.

...
colors = "custom"

[themes.custom]
dirs = [
  "#4ac16d",
  "#1fa187",
  "#277f8e",
  "#365c8d",
  "#365c8d",
  "#440154",
  "hsl(120, 100%, 25%)",
]
files = [
  "#f5db4c",
  "#fcae12",
  "#f78410",
  "#e65d2f",
  "#cb4149",
]

While leaves prunes directories at a relatively shallow depth, you can override it to load every file into memory. Files too small to display with rectangles will be represented by dots. The interface can handle millions of files while being generally responsive. However, there will be some delay when switching modes.

When scanning the root / directory, be sure to exclude virtual file systems like /dev, /proc, etc. otherwise you may end up with nonsensical results. Either place an .ignore file in the root, update ~/.config/git/ignore or use negative overrides:

leaves -A -d 3 / '!/proc' '!/tmp' '!/run' '!/sys' '!/dev' '!/mnt' '!/nix'

leaves will not follow symbolic links since they don't incur additional disk usage inside the directory.

No attempt is made to detect hard links, so they will be double counted.

联系我们 contact @ memedata.com