Raxol.Commands.FileSystem (Raxol v2.0.1)

View Source

Virtual file system for terminal navigation and content display.

Provides familiar file system commands for navigating terminal buffers:

  • ls - List directory contents
  • cat - Display file content
  • cd - Change current directory
  • pwd - Print working directory
  • mkdir - Create directory

Example

fs = FileSystem.new()
fs = FileSystem.mkdir(fs, "/documents")
fs = FileSystem.create_file(fs, "/documents/readme.txt", "Hello World")

{:ok, contents, _fs} = FileSystem.ls(fs, "/documents")
# => ["readme.txt"]

{:ok, content, _fs} = FileSystem.cat(fs, "/documents/readme.txt")
# => "Hello World"

Virtual Filesystem

The filesystem is a tree structure stored in memory:

  • Directories are maps with entries
  • Files have content and metadata
  • Paths are Unix-style (absolute or relative)

Summary

Functions

Display file contents.

Change current directory.

Create a new file with content.

Check if path exists.

Format file contents as a buffer.

Format directory listing as a buffer.

List directory contents.

Create a new directory.

Create a new virtual filesystem.

Print working directory.

Remove a file or empty directory.

Get file or directory metadata.

Get directory tree as nested structure.

Types

dir_entry()

@type dir_entry() :: %{
  type: :directory,
  entries: %{required(String.t()) => file_entry() | dir_entry()},
  metadata: metadata()
}

entry_type()

@type entry_type() :: :file | :directory

file_entry()

@type file_entry() :: %{type: :file, content: String.t(), metadata: metadata()}

metadata()

@type metadata() :: %{
  type: entry_type(),
  created: DateTime.t(),
  modified: DateTime.t(),
  size: non_neg_integer()
}

path()

@type path() :: String.t()

t()

@type t() :: %Raxol.Commands.FileSystem{
  cwd: path(),
  history: [path()],
  root: dir_entry()
}

Functions

cat(fs, path)

@spec cat(t(), path()) :: {:ok, String.t(), t()} | {:error, String.t()}

Display file contents.

cd(fs, path \\ nil)

@spec cd(t(), path() | nil) :: {:ok, t()} | {:error, String.t()}

Change current directory.

create_file(fs, path, content)

@spec create_file(t(), path(), String.t()) :: {:ok, t()} | {:error, String.t()}

Create a new file with content.

exists?(fs, path)

@spec exists?(t(), path()) :: boolean()

Check if path exists.

format_cat(content, width \\ 80, height \\ 24)

Format file contents as a buffer.

format_ls(entries, fs, path)

@spec format_ls([String.t()], t(), path()) :: Raxol.Core.Buffer.t()

Format directory listing as a buffer.

ls(fs, path \\ nil)

@spec ls(t(), path() | nil) :: {:ok, [String.t()], t()} | {:error, String.t()}

List directory contents.

mkdir(fs, path)

@spec mkdir(t(), path()) :: {:ok, t()} | {:error, String.t()}

Create a new directory.

new()

@spec new() :: t()

Create a new virtual filesystem.

pwd(fs)

@spec pwd(t()) :: {:ok, path(), t()}

Print working directory.

rm(fs, path)

@spec rm(t(), path()) :: {:ok, t()} | {:error, String.t()}

Remove a file or empty directory.

stat(fs, path)

@spec stat(t(), path()) :: {:ok, metadata(), t()} | {:error, String.t()}

Get file or directory metadata.

tree(fs, path \\ nil, max_depth \\ 3)

@spec tree(t(), path() | nil, non_neg_integer()) ::
  {:ok, list(), t()} | {:error, String.t()}

Get directory tree as nested structure.