Show HN:Lstr——一个用Rust编写的现代交互式树命令
Show HN: Lstr – A modern, interactive tree command written in Rust

原始链接: https://github.com/bgreenwell/lstr

Lstr是一个用Rust编写的高速、极简的目录树查看器,其灵感来自`tree`命令。它提供经典模式和交互式 (TUI) 模式两种方式来浏览文件系统。 Lstr的设计目标是速度,它利用并行目录扫描来最大限度地提高现代硬件的性能。它提供必要的特性,而没有不必要的膨胀,专注于简洁明了的用户体验。 主要特性包括:可选的文件专用图标(需要Nerd字体),显示文件权限和大小,Git集成以显示文件状态,以及尊重`.gitignore`文件的智能过滤。可以控制递归深度,并且可以只显示目录。 交互式TUI模式允许流畅的键盘驱动式浏览,包括展开/折叠目录、在默认编辑器中打开文件以及使用`fzf`进行模糊查找等操作。Shell集成允许将lstr用作可视化的`cd`命令。可以通过调整`RAYON_NUM_THREADS`环境变量来控制性能。

The author of `lstr`, a modern Rust-based `tree` command, introduces version 0.2.0. Inspired by the classic Linux `tree`, `lstr` adds interactivity and Git integration while remaining fast and minimalist. Key features include: an interactive TUI mode for keyboard-driven navigation and file opening; Git status integration with the `-G` flag; and shell integration, enabling `lstr` to be used as a visual `cd` replacement. It also supports file-type icons, sizes, permissions, and respects `.gitignore`. Initial feedback focused on broken icons in the demo GIF (acknowledged and addressed) and the large binary size. The author and commenters pointed out using `--release` and other build optimizations to significantly reduce the binary size. The tool, while potentially larger than core utilities, packs impressive functionality within ~1000 lines of code. The author appreciates the feedback and offers the project on GitHub and Crates.io.
相关文章

原文

Build Status Latest Version License: MIT

A blazingly fast, minimalist directory tree viewer, written in Rust. Inspired by the command line program tree, with a powerful interactive mode.

An interactive overview of lstr's project structure... using lstr.

  • Fast: Runs directory scans in parallel by default to maximize speed on modern hardware.
  • Minimalist: Provides essential features without the bloat. The core experience is clean and uncluttered.
  • Interactive: An optional TUI mode for fluid, keyboard-driven exploration.
  • High-performance: Scans directories in parallel to be as fast as possible.
  • Classic and interactive modes: Use lstr for a classic tree-like view, or launch lstr interactive for a fully interactive TUI.
  • Rich information display (optional):
    • Display file-specific icons with --icons (requires a Nerd Font).
    • Show file permissions with -p.
    • Show file sizes with -s.
    • Git Integration: Show file statuses (Modified, New, Untracked, etc.) directly in the tree with the -G flag.
  • Smart filtering:
    • Respects your .gitignore files with the -g flag.
    • Control recursion depth (-L) or show only directories (-d).

You need the Rust toolchain installed on your system to build lstr.

  1. Clone the repository:
    git clone https://github.com/bgreenwell/lstr.git
    cd lstr
  2. Build and install using Cargo:
    # This compiles in release mode and copies the binary to ~/.cargo/bin
    cargo install --path .
lstr [OPTIONS] [PATH]
lstr interactive [OPTIONS] [PATH]

Note that PATH defaults to the current directory (.) if not specified.

Option Description
-a, --all List all files and directories, including hidden ones.
--color <WHEN> Specify when to use color output (always, auto, never).
-d, --dirs-only List directories only, ignoring all files.
-g, --gitignore Respect .gitignore and other standard ignore files.
-G, --git-status Show git status for files and directories.
--icons Display file-specific icons; requires a Nerd Font.
-L, --level <LEVEL> Maximum depth to descend.
-p, --permissions Display file permissions (Unix-like systems only).
-s, --size Display the size of files.
--expand-level <LEVEL> Interactive mode only: Initial depth to expand the interactive tree.

Launch the TUI with lstr interactive [OPTIONS] [PATH].

Key(s) Action
/ k Move selection up.
/ j Move selection down.
Enter Context-aware action:
- If on a file: Open it in the default editor ($EDITOR).
- If on a directory: Toggle expand/collapse.
q / Esc Quit the application normally.
Ctrl+s Shell integration: Quits and prints the selected path to stdout.

1. List the contents of the current directory

2. Explore a project interactively, ignoring gitignored files

lstr interactive -g --icons

3. Display a directory with file sizes and permissions (classic view)

4. See the git status of all files in a project

5. Start an interactive session with all data displayed

lstr interactive -gG --icons -s -p

Piping and shell interaction

The classic view mode is designed to work well with other command-line tools via pipes (|).

Interactive fuzzy finding with fzf

This is a powerful way to instantly find any file in a large project.

fzf will take the tree from lstr and provide an interactive search prompt to filter it.

Paging large trees with less or bat

If a directory is too large to fit on one screen, pipe the output to a pager.

# Using less (the -R flag preserves color)
lstr -L 10 | less -R

# Using bat (a modern pager that understands colors)
lstr --icons | bat

Changing directories with lstr

You can use lstr as a visual cd command. Add the following function to your shell's startup file (e.g., ~/.bashrc, ~/.zshrc):

# A function to visually change directories with lstr
lcd() {
    # Run lstr and capture the selected path into a variable.
    # The TUI will draw on stderr, and the final path will be on stdout.
    local selected_dir
    selected_dir="$(lstr interactive -g --icons)"

    # If the user selected a path (and didn't just quit), `cd` into it.
    # Check if the selection is a directory.
    if [[ -n "$selected_dir" && -d "$selected_dir" ]]; then
        cd "$selected_dir"
    fi
}

After adding this and starting a new shell session (or running source ~/.bashrc), you can simply run:

This will launch the lstr interactive UI. Navigate to the directory you want, press Ctrl+s, and your shell's current directory will instantly change.

Performance and concurrency

By default, lstr uses a parallel directory walker to maximize speed on multi-core systems. This parallelism is managed by the excellent rayon thread pool, which is used internally by lstr's directory traversal engine.

For advanced use cases, such as benchmarking or limiting CPU usage, you can control the number of threads by setting the RAYON_NUM_THREADS environment variable before running the command.

To force single-threaded (serial) execution:

RAYON_NUM_THREADS=1 lstr .

The philosophy and functionality of lstr are heavily inspired by the excellent C-based tree command line program. This project is an attempt to recreate that classic utility in modern, safe Rust.

联系我们 contact @ memedata.com