files_tree

recursively builds and displays directory structures in ASCII tree format.

filesystem
file-tree
directory
tree
ascii-tree
cross-platform
utility
moon add xingwangzhe/files_tree@0.1.3
Download zip
Version
0.1.3
License
Apache-2.0
Last updated
10 months ago
Downloads
20

Dependencies

README

#文件树库 / File Tree Library

一个 MoonBit 文件树库,用于递归构建和以 ASCII 树形格式显示目录结构。

#主要功能 / Main Features

  • 递归遍历目录结构 / Recursively traverse directory structures
  • 生成美观的 ASCII 树形图 / Generate beautiful ASCII tree diagrams
  • 支持字符串输出和直接打印 / Support both string output and direct printing
  • 支持跨平台路径格式 / Support cross-platform path formats

#基本用法 / Basic Usage

///|
fn example() -> Unit raise @fs.IOError {
// 构建文件树 / Build file tree
let tree = build_tree(".")

// 直接打印 / Print directly
print_tree(tree)

// 或获取字符串 / Or get string representation
let tree_str = tree_to_string(tree)
println(tree_str)
}

#示例输出 / Example Output

./ ├── src/ │ ├── main.mbt │ └── utils.mbt ├── README.md └── README.mbt.md

#重要说明 / Important Notes

#路径解析机制 / Path Resolution Mechanism

相对路径是相对于程序运行时的工作目录(CWD),而不是源代码文件所在的目录。

Relative paths are resolved relative to the Current Working Directory (CWD) where the program runs, NOT relative to the source code file location.

这是因为底层实现:
  • build_tree() 调用 @fs.is_dir()@fs.read_dir()
  • 这些函数通过 FFI 调用 C 标准库函数(stat(), opendir() 等)
  • 路径解析由操作系统内核完成,遵循 POSIX 标准
  • 相对路径基准点是进程的当前工作目录,由 shell 在启动程序时设置

This is because of the underlying implementation:
  • build_tree() calls @fs.is_dir() and @fs.read_dir()
  • These functions use FFI to call C standard library functions (stat(), opendir(), etc.)
  • Path resolution is handled by the OS kernel, following POSIX standards
  • The base directory for relative paths is the process's current working directory, set by the shell when the program starts

示例 / Example:

如果从 /home/user/project/ 目录运行程序: If running the program from /home/user/project/:

build_tree(".") → 遍历 /home/user/project/ Traverses /home/user/project/ build_tree("./src") → 遍历 /home/user/project/src/ Traverses /home/user/project/src/ build_tree("../") → 遍历 /home/user/ Traverses /home/user/

建议 / Recommendations:

  1. 在项目根目录运行测试和程序 / Run tests and programs from the project root directory
  2. 使用相对路径时要注意当前工作目录 / Be aware of the current working directory when using relative paths
  3. 如需使用绝对路径可直接传入完整路径 / Use absolute paths if you need path independence

#
FileNode

pub enum FileNode {
File(String)
Directory(String, Array[FileNode])
}

File tree node representation 文件树节点表示

This enum represents either a file or a directory in the file system tree. 此枚举表示文件系统树中的文件或目录。

Variants

  • File(String): Represents a file with its name
  • Directory(String, Array[FileNode]): Represents a directory with its name and child nodes
  • File(String): 表示具有名称的文件
  • Directory(String, Array[FileNode]): 表示具有名称和子节点列表的目录
impl Show for FileNode

#
build_tree

fn build_tree(path : String) -> FileNode raise
IOError

Recursively build file tree from path 从路径递归构建文件树

Parameters

参数

  • path: The path string to traverse
  • path: 要遍历的路径字符串

Returns

返回值

  • FileNode: The file tree node
  • FileNode: 文件树节点

Errors

错误

  • Raises @fs.IOError if file system operations fail
  • 如果文件系统操作失败则抛出 @fs.IOError
fn print_tree(node : FileNode) -> Unit

Print the complete ASCII file tree 打印完整的 ASCII 文件树

Parameters

参数

  • node: The root node of the file tree
  • node: 文件树的根节点

Examples

示例

let tree = build_tree(".")
print_tree(tree)
// Output:
// .
// ├── src/
// │ └── main.mbt
// └── README.md

#
tree_to_string

fn tree_to_string(node : FileNode) -> String

Convert the complete file tree to a multi-line string 将完整的文件树转换为多行字符串

Parameters

参数

  • node: The root node of the file tree
  • node: 文件树的根节点

Returns

返回值

  • String: A multi-line string representation of the file tree
  • String: 文件树的多行字符串表示

Examples

示例

let tree = build_tree(".")
let tree_str = tree_to_string(tree)
println(tree_str)
// Output:
// .
// ├── src/
// │ └── main.mbt
// └── README.md